如何在Java中通过UDP接收FFMpeg-ts流?

问题描述 投票:1回答:3

我有一个来自FFMpeg的实时视频流,我很难用我自己定制的Java应用程序来查看该视频流。在有人告诉我使用VLC或类似的东西之前,我确实想使用我自己的应用程序。我试图读取的流是一个H.264编码的Mpeg-ts流,通过UDP传输。我知道如何解码H.264帧,但我只是想知道如何接收Mpeg-ts流。

java ffmpeg udp mpeg2-ts
3个回答
1
投票

乔恩-基特尔 提供了一个很好的答案,反正在问题评论中提到了UNICAST通信,所以Jon的代码在这种情况下是无法使用的,下面提供了一个简化的单播接收器(归功于 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();
    }
  }
}

这个客户端将监听本地主机地址1234端口上的UDP流,并将每个接收到的数据包写入testStream.ts文件。如果数据流是H.264编码的Mpeg-ts,输出文件可以在任何时刻用播放器打开,以重现该时刻之前捕获的视频流。


1
投票

为了接收组播流,你需要创建一个组播客户端,它有一个缓冲区来存储视频数据,并使用一个可以加入和监听组播流的socket。

两个属性是组播地址(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

编译并运行多类客户端,该客户端使用的是 多播插座 类来加入组播组并监听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();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.