当前位置:网站首页>使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
使用FFmpeg命令行进行UDP、RTP推流(H264、TS),ffplay接收
2022-07-02 12:16:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
我们在开发网络程序时经常用到UDP或RTP来发送和接收流媒体,而开发程序完毕需要搭建一个环境测试,这时候可能你需要一个推流端或接收端。对于推流端,我们可以借助FFmpeg工具轻松完成该功能,只需要敲一条命令后就可以实现发流,并且支持多种网络协议(UDP/RTP/RTSP/RTMP)。而接收端我们可以使用ffplay,这个程序也是在FFmpeg工具包的Bin目录里面。大家可以根据自己需要使用这两个工具进行推流或接收,下面就以传输协议UDP、RTP为基础,介绍几种最常见的推流场景下两个工具的用法。
1. 使用RTP发送H264
FFmpeg的推流命令:
ffmpeg -re -i d:\videos\1080P.264 -vcodec copy -f rtp rtp://127.0.0.1:1234该命令行实现读取一个H264文件,以源文件固有帧率发送数据(加上-re参数),输出流协议为rtp。
ffplay接收端的命令:
ffplay -protocol_whitelist "file,udp,rtp" -i rtp://127.0.0.1:1234注意:ffplay的RTP协议默认是不启用的,需要添加-protocol_whitelist参数把RTP协议加入白名单。但是,执行了上面的命令可能会出错,因为ffplay使用RTP协议接收需要一个SDP文件,该文件定义了输入流的格式信息以及接收端的IP和端口号。
我们可以让FFmpeg在执行推流输出时导出一个SDP,命令行如下:
ffmpeg -re -i d:\videos\1080P.264 -vcodec copy -f rtp rtp://127.0.0.1:1234>test_rtp_h264.sdp下面是一个SDP文件的例子:
SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
a=tool:libavformat 56.15.102
m=video 1234 RTP/AVP 96
a=rtpmap:96 H264/90000(SDP文件的信息说明:传输协议:RTP/AVP, 接收端IP:127.0.0.1, 接收端口号:1234 , PayloadType:96 ,视频格式:H264 )
以上面SDP文件为输入让ffplay打开流,命令行是:
ffplay -protocol_whitelist "file,udp,rtp" -i test_rtp_h264.sdp2. 使用UDP发送TS流
FFmpeg的推流命令:
ffmpeg -re -i d:\videos\1080P.264 -vcodec copy -f mpegts udp://127.0.0.1:1234ffplay的接收命令:
ffplay -protocol_whitelist "file,udp,rtp" -i udp://127.0.0.1:12343. 使用RTP发送TS流
很多人以为这种情况跟上面差不多,使用如下的推流命令(错误):
ffmpeg -re -i d:\videos\1080P.264 -vcodec copy -f mpegts rtp://127.0.0.1:1234但是实际上不行,我开始也使用该条命令,试过好多次老是失败,失败的原因是:发送的包缺少RTP头,输出的协议有错误。实际上,正确的命令是:
ffmpeg -re -i d:\videos\1080P.264 -vcodec copy -f rtp_mpegts rtp://127.0.0.1:1234ffplay接收的命令:
ffplay -protocol_whitelist "file,udp,rtp" -i rtp://127.0.0.1:1234或者打开一个SDP文件,SDP文件对TS流的写法:
SDP:
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Name
c=IN IP4 127.0.0.1
t=0 0
m=video 1234 RTP/AVP 33
a=rtpmap:33 MP2T/90000(注意:FFmpeg发送TS流的RTP PayloadType不是96而是33)
还有一个问题:早期的ffmpeg版本是不支持rtp_mpegts输出协议的,你可以使用命令ffmpeg -formats查看ffmpeg是否支持这种输出协议。我使用FFmpeg的版本是ffmpeg-20180209是支持这种协议的。
4. 使用RTP分别发送音频流和视频流
FFmpeg命令:
ffmpeg -re -i <media_file> -an -vcodec copy -f rtp rtp://<IP>:5004 -vn -acodec copy -f rtp rtp://<IP>:5005 > test.sdpFFplay接收的SDP文件:
SDP:
v=2
m=video 5004 RTP/AVP 96
a=rtpmap:96 H264
t=0 0
a=framerate:25
c=IN IP4 192.168.0.100
m=audio 5005 RTP/AVP 97
a=rtpmap:97 PCM/8000/1
a=framerate:25
c=IN IP4 192.168.0.100————————————————————————————————–
上面介绍的是用命令行的方法
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147723.html原文链接:https://javaforall.cn
边栏推荐
- [leetcode] 344 reverse string
- [leetcode] 417 - Pacific Atlantic current problem
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
- ssh/scp 使不提示 All activities are monitored and reported.
- Force deduction solution summary 2029 stone game IX
- 夏季高考文化成绩一分一段表
- 6090. 极大极小游戏
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- MySQL -- Index Optimization -- order by
- I made an istio workshop. This is the first introduction
猜你喜欢

Two traversal sequences are known to construct binary trees

怎样从微信返回的json字符串中截取某个key的值?

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

03.golang初步使用

【LeetCode】1254-统计封闭岛屿的数量

Leetcode skimming - remove duplicate letters 316 medium

2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)

LeetCode刷题——递增的三元子序列#334#Medium

党史纪实主题公益数字文创产品正式上线

03. Preliminary use of golang
随机推荐
6090. 极大极小游戏
损失函数与正负样本分配:YOLO系列
LeetCode刷题——去除重复字母#316#Medium
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
PTA ladder game exercise set l2-001 inter city emergency rescue
提前批院校名称
已知兩種遍曆序列構造二叉樹
【LeetCode】189-轮转数组
2279. Maximum number of backpacks filled with stones
6096. 咒语和药水的成功对数
College entrance examination admission score line climbing
[leetcode] 417 - Pacific Atlantic current problem
自定义异常
Loss function and positive and negative sample allocation: Yolo series
I made an istio workshop. This is the first introduction
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
PTA 天梯赛习题集 L2-001 城市间紧急救援
Markdown tutorial
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice