视频的发布可以采用ffmpeg和nginx+rtmp实现。
ffmepg负责推流到nginx,nginx负责将流发布出去。
发布配置
nginx需要加入nginx-rtmp-module这个模块以进行rtmp视频的发布。安装好该模块后,nginx要配置如下:
rtmp {
server {
listen 1935;
application live {
live on;
}
application hls {
live on;
hls on;
hls_path temp/hls;
hls_fragment 8s;
}
}
}
http {
server {
listen 8080;
location / {
root html;
}
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root html;
}
location /hls {
#server hls fragments
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias temp/hls;
expires -1;
}
}
}推流
推流命令如下:
ffmpeg -re -i ./ccnn.mp4 -vcodec copy -acodec copy -f flv rtmp://localhost/hls/test ffmpeg -re -i ./ccnn.mp4 -vcodec copy -acodec copy -f flv rtmp://localhost/live/test
RTMP
配好以后,推流可以使用下面的地址:
rtmp://192.168.31.185/live/movie
HLS
配好以后,推流可以使用下面的地址:
rtmp://192.168.31.185/hls/movie
movie 关键字可以任何替换。
播放
对于观众端来说,可以有几种播放方式:
(1) 用 rtmp:
rtmp://192.168.31.185/hls/movie
(2) 用 hls 播放:
http://192.168.31.185:8080/hls/movie.m3u8
这样就可以看到主播端推出来的流。注意,如果使用 http 方式,则是监听的 8080 端口,这个是在配置文件里写的。
转发
(1)对于rtmp,用nginx进行转发时,要采用tcp模块。
nginx中需要加入nginx_tcp_proxy_module,并重新编译、安装。
目前nginx1.14.2可以加入该模块。该模块的下载地址为:
https://github.com/yaoweibin/nginx_tcp_proxy_module
安装tcp模块后,nginx.conf中的配置如下:
tcp {
upstream videoPublisher {
# simple round-robin
server 192.168.3.100:1935;#需要代理的端口
#check interval=3000 rise=2 fall=5timeout=1000;
#check interval=3000 rise=2 fall=5timeout=1000
#check interval=3000 rise=2 fall=5timeout=1000
#check_http_send "GET /HTTP/1.0\r\n\r\n";
#check_http_expect_alive http_2xxhttp_3xx;
}
server {
listen 8888; #代理8888端口
proxy_pass videoPublisher;
}
}(2)对于hls的发布方式,转发方式和普通的http转发相同。
如配置如下:
location /hls {
proxy_pass http://192.168.3.100:8080;
}