2014-01-29 188 views
1

我有來自FFMpeg的實時視頻流,而且我很難用我自己的自定義Java應用程序查看該流。在有人告訴我使用VLC或類似的東西之前,我想使用我自己的應用程序。我試圖讀取的流是通過UDP流式傳輸的H.264編碼的Mpeg-ts流。我知道如何解碼H.264幀,但我只是想知道如何接收Mpeg-ts流。如何在Java中使用FFMpeg通過UDP接收Mpeg-ts流

+0

組播或單播? – szatmary

+0

對不起,我忘了提及那個,單播。 –

+1

然後,發送數據的設備必須配置爲發送到特定的IP /端口。只需打開端口並讀取數據包。 – szatmary

回答

1

Jon Kittel提供了一個很好的答案,反正在問題的評論中提到單播通信,喬恩的代碼是不是在這種情況下使用。 下面提供了一個簡化的單播接收機(原始示例記爲P. Tellenbach)。

import java.io.FileOutputStream; 
import java.net.DatagramPacket; 
import java.net.DatagramSocket; 

public class DatagramServer 
{ 
    private final static int PACKETSIZE = 2048 ; 

    public static void main(String args[]) 
    { 
    // Check the arguments 
    //  if(args.length != 1) 
    //  { 
    //   System.out.println("usage: DatagramServer port") ; 
    //   return ; 
    //  } 

     try 
     { 
     // Convert the argument to ensure that is it valid 
     int port = 1234; //Integer.parseInt(args[0]) ; 
     FileOutputStream output = new FileOutputStream("testStream.ts", true); 

     // Construct the socket 
     DatagramSocket socket = new DatagramSocket(port) ; 

     System.out.println("The server is ready...") ; 

     DatagramPacket packet = new DatagramPacket(new byte[PACKETSIZE], PACKETSIZE) ; 
     for(;;) 
     { 
      // Receive a packet (blocking) 
      socket.receive(packet); 
      try { 
       output.write(packet.getData()); 
      } finally { 
       output.close(); 
      } 
     } 
    }catch (IOException exception) { 
      exception.printStackTrace(); 
    } 
    } 
} 

此客戶端將偵聽本地主機地址UDP流,端口1234,會寫接收分組到testStream.ts文件。 如果數據流是H.264編碼的Mpeg-ts,輸出文件可以在任何時候用播放器打開,以重現在該時刻捕獲的視頻流。

1

爲了接收組播流,您將需要創建一個多播客戶端,該客戶端具有一個用於存儲視頻數據的緩衝區,並使用可以加入和偵聽多播流的套接字。

這兩個屬性是多播地址(239.1.1.1)和端口(49410)。

使用ffmpeg開始將mp4視頻文件流式傳輸到多播地址和端口。

ffmpeg -i .\mars.mp4 -c:v libx264 -c:a libmp3lame -f mpegts udp://239.1.1.1:49410 

編譯並運行客戶機多類,它使用MulticastSocket類加入組播組,監聽UDP流數據包。我們將緩衝區傳遞給DatagramPacket對象,並且當套接字接收到UDP數據包時,緩衝區將填充mpeg-ts數據。然後,您可以將緩衝區複製到應用程序的另一部分以解碼數據。

import java.io.IOException; 
import java.net.DatagramPacket; 
import java.net.InetAddress; 
import java.net.MulticastSocket; 
import java.net.UnknownHostException; 

public class Client { 

    final static String INET_ADDR = "239.1.1.1"; 
    final static int PORT = 49410; 

    public static void main(String[] args) throws UnknownHostException { 
     // Get the multicast address that we are going to connect to. 
     InetAddress address = InetAddress.getByName(INET_ADDR); 

     // Create a buffer of bytes, which will be used to store 
     // the incoming bytes containing the information from the streaming server 
     byte[] buffer = new byte[256]; 

     // Create a new Multicast socket so we can join the multicast group 
     try (MulticastSocket clientSocket = new MulticastSocket(PORT)){ 
      //Joint the Multicast group. 
      clientSocket.joinGroup(address); 

      // do an infinite loop 
      while (true) { 
       // Receive the information and print it. 
       DatagramPacket msgPacket = new DatagramPacket(buffer, buffer.length); 
       clientSocket.receive(msgPacket); 

       String data = new String(buffer, 0, buffer.length); 

       System.out.println("Data -> " + data); 
      } 
     } catch (IOException exception) { 
      exception.printStackTrace(); 
     } 
    } 
}