FFmpeg5入门教程10.24:搭建UDP/TCP/HTTP(S)/RTP/RTMP/RTSP推流服务器

索引地址:系列教程索引地址

上一篇:FFmpeg5入门教程10.23:音视频mp3和h264混合(muxer)编码为mp4

本文介绍基于FFmpeg的推流服务器的搭建方法。

UDP/TCP

不需要推流服务器,直接推流就可以了。

HTTP(S)

待定

RTP

RTP推流只支持ts格式源视频格式。

如果没有此格式文件可以用FFmpeg工具生成。

1
ffmpeg -i Sample.mkv -c copy -vbsf h264_mp4toannexb 1.ts

可以用ffplay测试看生成是否正常。

然后使用命令推流:

1
ffmpeg -re -i 1.ts -vcodec copy -f rtp_mpegts rtp://127.0.0.1:1234

进行收流测试:

1
ffplay rtp://127.0.0.1:1234

RTMP

RTMP需要有服务端

nginx

NGINX+RTMP推流服务器是最常见的。

RTMP推流需要nginx-mod-rtmp插件,这就需要从源码编译。

先到官网下载nginx源码

然后到Github下载插件源码。

编译:

1
2
3
./configure --add-module=/home/jackey/Downloads/nginx-rtmp-module-master
make -j12
sudo make install

我的编译安装默认位置为:/usr/local/nginx。

修改配置文件/usr/local/nginx/conf/nginx.conf

在其中添加:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
rtmp {
server {
listen 1935;
chunk_size 4000;
application vod {
play /usr/local/nginx/html/vod/;#点播媒体存放目录
}
application live {
live on;
}
application hls {
live on;
hls on;
hls_path /usr/local/nginx/html/hls;#视频流存放地址
hls_fragment 5s;
hls_playlist_length 15s;
hls_continuous on; #连续模式。
hls_cleanup on; #对多余的切片进行删除。
hls_nested on; #嵌套模式。
}
}
}

在http{}之前或者之后添加都可以。

然后运行nginx:

1
2
cd /usr/local/nginx/sbin/
sudo ./nginx

如果有错会提示并退出。

推流:

1
2
ffmpeg -re -stream_loop -1 -i /home/jackey/Videos/Sample.mkv -vcodec libx264 -vprofile baseline -f flv rtmp://192.168.1.101:1935/hls/test
ffmpeg -re -stream_loop -1 -i /home/jackey/Videos/Sample.mkv -vcodec libx264 -vprofile baseline -f flv rtmp://192.168.1.101:1935/live/test

两条命令任意一个都可以。

然后使用ffplay测试收流:

1
ffplay rtmp://192.168.1.101:1935/live/test

RTSP

live555

一款开源rtsp服务器。

首先从官网下载可执行文件,自己编译也可以。

首先在需要推流的视频源文件的文件夹中运行命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ live555MediaServer
LIVE555 Media Server
version 1.09 (LIVE555 Streaming Media library version 2021.04.06).
Play streams from this server using the URL
rtsp://192.168.1.101:8554/<filename>
where <filename> is a file present in the current directory.
Each file's type is inferred from its name suffix:
".264" => a H.264 Video Elementary Stream file
".265" => a H.265 Video Elementary Stream file
".aac" => an AAC Audio (ADTS format) file
".ac3" => an AC-3 Audio file
".amr" => an AMR Audio file
".dv" => a DV Video file
".m4e" => a MPEG-4 Video Elementary Stream file
".mkv" => a Matroska audio+video+(optional)subtitles file
".mp3" => a MPEG-1 or 2 Audio file
".mpg" => a MPEG-1 or 2 Program Stream (audio+video) file
".ogg" or ".ogv" or ".opus" => an Ogg audio and/or video file
".ts" => a MPEG Transport Stream file
(a ".tsx" index file - if present - provides server 'trick play' support)
".vob" => a VOB (MPEG-2 video with AC-3 audio) file
".wav" => a WAV Audio file
".webm" => a WebM audio(Vorbis)+video(VP8) file
See http://www.live555.com/mediaServer/ for additional documentation.
(We use port 8000 for optional RTSP-over-HTTP tunneling).)

可以通过输出看到,文件夹中有什么视频就可以通过流地址直接访问(支持的视频格式也给出来了)。

取流:

1
ffplay rtsp://192.168.1.101:8000/1.ts

easydarwin

easydarwin是国内一家公司仿照苹果的darwin用GO写的推流服务器,使用时实时性比live555好一点。

Windows和Linux平台有直接编译好的可执行文件,当然也可以自己编译。ARM平台我也使用过,详见jetson nano搭建EasyDarwin+FFmpeg+x264+RTSP推流服务器

下载好软件后使用管理员权限启动:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ sudo ./start.sh
[EasyDarwin] 2021/05/01 19:12:18 main.go:186: git commit code:0351aed
[EasyDarwin] 2021/05/01 19:12:18 main.go:187: build date:2019-01-14 11:49:59
_____ ____ _
| ____| __ _ ___ _ _ | _ \ __ _ _ __ __ __ (_) _ __
| _| / _` | / __| | | | | | | | | / _` | | '__| \ \ /\ / / | | | '_ \
| |___ | (_| | \__ \ | |_| | | |_| | | (_| | | | \ V V / | | | | | |
|_____| \__,_| |___/ \__, | |____/ \__,_| |_| \_/\_/ |_| |_| |_|
|___/
[EasyDarwin] 2021/05/01 19:12:18 main.go:216: EasyDarwin_Service install ...
[EasyDarwin] 2021/05/01 19:12:18 main.go:221: EasyDarwin_Service install ok
[EasyDarwin] 2021/05/01 19:12:18 main.go:186: git commit code:0351aed
[EasyDarwin] 2021/05/01 19:12:18 main.go:187: build date:2019-01-14 11:49:59
[EasyDarwin] 2021/05/01 19:12:18 main.go:216: EasyDarwin_Service start ...
[EasyDarwin] 2021/05/01 19:12:18 main.go:221: EasyDarwin_Service start ok

然后就可以推rtsp格式的流了。

推流命令:

1
ffmpeg -re -stream_loop -1 -i /home/jackey/Videos/Sample.mkv -rtsp_transport udp -f rtsp rtsp://127.0.0.1/test

收流测试:

1
ffplay rtsp://127.0.0.1/test

最重的是推流地址有效,能够使用此地址向外推流,否则后续的开发会有问题。

下一篇:FFmpeg5入门教程10.25:本地文件推流


FFmpeg5入门教程10.24:搭建UDP/TCP/HTTP(S)/RTP/RTMP/RTSP推流服务器
https://feater.top/ffmpeg/ffmpeg-setup-udp-tcp-https-rtp-rtmp-rtsp-stream-server/
作者
JackeyLea
发布于
2021年5月1日
许可协议