FFmpeg录制和发布UDP/TCP流
1、TCP与UDP参数说明
ffmpeg --help full
tcp AVOptions:
-listen <int> ED....... Listen for incoming connections (from 0 to 2) (default 0)
-timeout <int> ED....... set timeout (in microseconds) of socket I/O operations (from -1 to INT_MAX) (default -1)
-listen_timeout <int> ED....... Connection awaiting timeout (in milliseconds) (from -1 to INT_MAX) (default -1)
-send_buffer_size <int> ED....... Socket send buffer size (in bytes) (from -1 to INT_MAX) (default -1)
-recv_buffer_size <int> ED....... Socket receive buffer size (in bytes) (from -1 to INT_MAX) (default -1)
-tcp_nodelay <boolean> ED....... Use TCP_NODELAY to disable nagle's algorithm (default false)
-tcp_mss <int> ED....... Maximum segment size for outgoing TCP packets (from -1 to INT_MAX) (default -1)
udp AVOptions:
-buffer_size <int> ED....... System data size (in bytes) (from -1 to INT_MAX) (default -1)
-bitrate <int64> E........ Bits to send per second (from 0 to I64_MAX) (default 0)
-burst_bits <int64> E........ Max length of bursts in bits (when using bitrate) (from 0 to I64_MAX) (default 0)
-localport <int> ED....... Local port (from -1 to INT_MAX) (default -1)
-local_port <int> ED....... Local port (from -1 to INT_MAX) (default -1)
-localaddr <string> ED....... Local address
-udplite_coverage <int> ED....... choose UDPLite head size which should be validated by checksum (from 0 to INT_MAX) (default 0)
-pkt_size <int> ED....... Maximum UDP packet size (from -1 to INT_MAX) (default 1472)
-reuse <boolean> ED....... explicitly allow reusing UDP sockets (default auto)
-reuse_socket <boolean> ED....... explicitly allow reusing UDP sockets (default auto)
-broadcast <boolean> E........ explicitly allow or disallow broadcast destination (default false)
-ttl <int> E........ Time to live (multicast only) (from 0 to INT_MAX) (default 16)
-connect <boolean> ED....... set if connect() should be called on socket (default false)
-fifo_size <int> .D....... set the UDP receiving circular buffer size, expressed as a number of packets with size of 188 bytes (from 0 to INT_MAX) (default 28672)
-overrun_nonfatal <boolean> .D....... survive in case of UDP receiving circular buffer overrun (default false)
-timeout <int> .D....... set raise error timeout (only in read mode) (from 0 to INT_MAX) (default 0)
-sources <string> ED....... Source list
-block <string> ED....... Block list
2、TCP监听接收流
ffmpeg -listen 1 -f flv -i tcp://127.0.0.1:1234/live/stream -c copy -f flv output.flv
执行完命令后,FFmpeg会进入端口监听模式,等待客户端连接到本地的1234端口。
3、TCP请求发布流
ffmpeg -re -i input.mp4 -c copy -f flv tcp://127.0.0.1:1234/live/stream
如上,请求端口1234,指定输出格式为FLV格式。
4、监听端口超时listen_timeout
time ./ffmpeg -listen_timeout 5000 -listen 1 -f flv -i tcp://127.0.0.1:1234/live/stream -c copy -f flv output.flv
设置5秒钟超时时间,超时则退出监听。
5、TCP拉流超时参数timeout
time ./ffmpeg -timeout 20000000 -i tcp://192.168.100.179:1935/live/stream -c copy -f flv output.flv
设置超时时间为20秒,连接TCP拉取端口1935的数据。
6、TCP传输buffer大小设置send_buffer_size/recv_buffer_size
ffmpeg -re -i input.mp4 -c copy -send_buffer_size 265 -f flv tcp://192.168.100.179:1234/live/stream
执行该命令,因为发送的buffer大小变成了265,数据发送的频率变大,并且次数变多,网络开销也大,因此输出速度会变慢。
7、绑定本地UDP端口localport
ffmpeg -re -i input.mp4 -c copy localport 23456 -f flv udp://192.168.100.179:1234/live/stream
如上,使用localport参数设置监听本地端口。
网友评论