ffmpeg rtmp hls推流

安装Nginx的扩展rtmp模块

1. 下载扩展包

1
2
3
4
5
6
7
8
$ cd /soft
$ wget https://github.com/arut/nginx-rtmp-module/archive/v1.1.10.tar.gz
//解压扩展包
$ tar -zxvf v1.1.10.tar.gz
#为nginx创建扩展模块目录
$ mkdir /usr/local/nginx/extend_module
#将解压后的nginx-rtmp-module目录移动到nginx扩展模块目录下
$ mv nginx-rtmp-module-1.1.10/ /usr/local/nginx/extend_module/nginx-rtmp-module

2. 查看nginx配置参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ /usr/local/nginx/sbin/nginx -V
#复制configure arguments:后的所有参数! 如下:
nginx version: nginx/1.13.10
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
#进入nginx 安装包目录,添加模块
$cd /soft/nginx-1.13.10
#复制刚才的nginx configure参数 --add-module=rtmp扩展包目录如下:
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=/usr/local/nginx/extend_module/nginx-rtmp-module
make && make install
#重启
$ /usr/local/nginx/sbin/nginx -s reload
#再次查看是否已加载, 出现nginx-rtmp-module表示成功
$ /usr/local/nginx/sbin/nginx -V

3. 配置rtmp(防火墙需要开放1935端口)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ vim /usr/local/nginx/conf/nginx.conf 
#在尾部添加
rtmp {
server {
listen 1935; #监听的端口
chunk_size 4000;

application hls { #rtmp推流请求路径
hls on; #开启on ,关闭off
hls_path /data/video/hls/test; #视频流m3u8文件夹
hls_fragment 3s; #每个块的时长3秒,默认5秒
live on; #开启实时
record off; #不记录数据
}
}
}

4. 配置nginx 服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ vim /usr/local/nginx/conf/vhosts/test.conf
#加入hls支持
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
#或 application/x-mpegURL
video/mp2t ts;
}
alias /data/video/hls/test/; #视频流文件目录(自己创建)
expires -1;
add_header Cache-Control no-cache;
}
#重启nginx
$ /usr/local/nginx/sbin/nginx -s reload

5. FFmepg推流

1
2
3
4
5
6
7
8
9
10
11
#安装ffmepg
$ yum install ffmpeg
#1. 基于mp4视频文件推流, -acodec copy 表示默认声音编码
$ ffmpeg -re -i /data/video/hls/tt.mp4 -vcodec copy -acodec copy -f flv rtmp://192.168.50.87:1935/hls/test

#2. 用可用rtmp直播源推流,可用直播源:
rtmp://live.hkstv.hk.lxdns.com/live/hks2
rtmp://live.hkstv.hk.lxdns.com/live/hks2
rtmp://live.hkstv.hk.lxdns.com/live/hks2

$ ffmpeg -re -i rtmp://live.hkstv.hk.lxdns.com/live/hks1 -vcodec copy -acodec copy -f flv rtmp://192.168.50.87:1935/hls/test

6. 视频播放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>videojs-contrib-hls embed</title>
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
<script src="https://unpkg.com/video.js/dist/video.js"></script>
<script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
</head>
<body>
<h1>Video.js Example Embed</h1>
<video id="my_video_1" class="video-js vjs-default-skin" controls width="640"
data-setup='{}'>
<source src="/hls/test.m3u8" type="application/x-mpegURL">
</video>
</body>
</html>