当前位置:网站首页>使用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
边栏推荐
- 【Salesforce】如何确认你的Salesforce版本?
- How to find a sense of career direction
- [leetcode] 977 square of ordered array
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
- . Net again! Happy 20th birthday
- 【LeetCode】417-太平洋大西洋水流问题
- There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
- 6091. 划分数组使最大差为 K
- Yolo format data set processing (XML to txt)
- Bing. Site Internet
猜你喜欢

How to avoid 7 common problems in mobile and network availability testing

MySQL calculate n-day retention rate
![[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)](/img/cf/38e4035c3b318814672f21c8a42618.jpg)
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)

PostgresSQL 流复制 主备切换 主库无读写宕机场景

Build your own semantic segmentation platform deeplabv3+

【LeetCode】1905-统计子岛屿

Yolo format data set processing (XML to txt)

6.12 企业内部upp平台(Unified Process Platform)的关键一刻

彻底弄懂浏览器强缓存和协商缓存

终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
随机推荐
SQL stored procedure
For the problem that Folium map cannot be displayed, the temporary solution is as follows
【LeetCode】189-轮转数组
beforeEach
04. Some thoughts on enterprise application construction after entering cloud native
02.面向容器化后,必须面对golang
【网络安全】网络资产收集
6091. 划分数组使最大差为 K
【Salesforce】如何确认你的Salesforce版本?
Markdown tutorial
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
【Leetcode】167-两数之和II -输入有序数组
5. Practice: jctree implements the annotation processor at compile time
Download blender on Alibaba cloud image station
【LeetCode】1140-石子游戏II
LeetCode刷题——奇偶链表#328#Medium
[salesforce] how to confirm your salesforce version?
. Net again! Happy 20th birthday
Redux - detailed explanation
Leetcode skimming -- sum of two integers 371 medium