(1)下载第三方扩展模块nginx-rtmp-module
# mkdir module && cd module
# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip
# unzip master.zip
# ls nginx-rtmp-module-master/
(2)编译安装nginx(说明:此处由于我这边已有lnmp运行项目的环境,直接动态添加的 nginx-rtmp-module模块)
# nginx -V
nginx version: nginx/1.17.5
built by gcc 9.2.0 (GCC)
built with OpenSSL 1.1.1d 10 Sep 2019
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/bin/nginx --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --user=http --group=http --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --http-client-body-temp-path=/var/lib/nginx/client-body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-cc-opt='-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-pcre-jit --with-stream --with-stream_geoip_module --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads
# yum -y install pcre-devel openssl openssl-devel //安装依赖
# wget http://nginx.org/download/nginx-1.17.5.tar.gz //下载nginx包
# tar xf nginx-1.17.5.tar.gz
# sudo ./configure --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --sbin-path=/usr/bin/nginx --pid-path=/run/nginx.pid --lock-path=/run/lock/nginx.lock --user=http --group=http --http-log-path=/var/log/nginx/access.log --error-log-path=stderr --http-client-body-temp-path=/var/lib/nginx/client-body --http-proxy-temp-path=/var/lib/nginx/proxy --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-cc-opt='-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now --with-compat --with-debug --with-file-aio --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-pcre-jit --with-stream --with-stream_geoip_module --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --add-module=/home/taurus/nginx-rtmp-module-master //编译安装nginx,并指定上面下载的模块路径
# make
# systemctl stop nginx.service
# cp /usr/bin/nginx /usr/bin/nginx/nginx.bak
# cp ./objs/nginx /usr/bin/nginx
# nginx -V //查看是否安装成功
[error] 如果出现类似ngx_rtmp_eval this statement may fall through [-Werror=implicit-fallthrough=]的错误
# sudo vim [nginx安装包目录]/objs/Makefile // 在文件中删除-Werrori
# sudo make
(3)修改nginx配置文件,添加如下内容并重新载入配置文件
# vim nginx.conf
rtmp {
server {
listen 1945; #监听的端口号
application myapp { #自定义的名字
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls;
hls_fragment 1s;
hls_playlist_length 3s;
}
}
}
# systemctl restart nginx
(4)安装ffmpeg(我的系统是manjaro,所以使用pacman安装)
# sudo pacman -S ffmpeg
(5)启用ffmpeg进行推流
# ffmpeg -i rtsp://192.168.1.175:554/11 -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -f flv rtmp://192.168.1.11:1935/myapp/23
-i 要处理视频文件的路径,此处地址是一个监控摄像头
rtmp://192.168.1.11:1935/myrtmp/1 说明:rtmp://IP:PORT/ myrtmp指nginx配置文件中自定义的,1指输出文件的名字
-f 强迫采用flv格式
别的参数参考上面那位老兄的
(6)打开VLC 媒体——>流——>网络 即可播放
(7)Web播放直播 使用EasyPlayer.js, 地址:https://github.com/tsingsee/EasyPlayer.js
<!DOCTYPE html>
<html>
<head>
<title>liveplayer</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta
content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no"
name="viewport"
/>
<script type="text/javascript" src="/media/EasyPlayer.js/element/EasyPlayer-element.min.js"></script>
</head>
<body>
<div style="width:500px;">
<easy-player
video-url="rtmp://192.168.1.116:1935/myapp/23"
live="true"
stretch="true"
></easy-player>
</div>
<div style="width:500px;">
<easy-player
video-url="rtmp://192.168.1.116:1935/myapp/24"
live="true"
stretch="true"
></easy-player>
</div>
</body>
</html>
测试完成,继续研究后台管理动态添加rtsp流转rtmp。
附: 海康威视RTSP流媒体格式
(1)举例
rtsp://admin:worthsen123456@192.168.1.66:554/H.264/ch1/main/av_stream
(2)格式
rtsp://[username]:[password]@[ip]:[port]/[codec]/[channel]/[subtype]/av_stream
(3)说明
username: 用户名。例如admin。
password: 密码。例如worthsen123456。
ip: 为设备IP。例如 192.168.1.66。
port: 端口号默认为554,若为默认可不填写。
codec:有h264、MPEG-4、mpeg4这几种。
channel: 通道号,起始为1。例如通道1,则为ch1。
subtype: 码流类型,主码流为main,辅码流为sub。
大华RTSP流媒体格式
(1)举例
rtsp://admin:admin@192.168.1.66:554/cam/realmonitor?channel=2&subtype=1
(2)格式
rtsp://username:password@ip:port/cam/realmonitor?channel=1&subtype=0
(3)说明
username: 用户名。例如admin。
password: 密码。例如admin。
ip: 为设备IP。例如 192.168.1.66。
port: 端口号默认为554,若为默认可不填写。
channel: 通道号,起始为1。例如通道2,则为channel=2。
subtype: 码流类型,主码流为0(即subtype=0),辅码流为1(即subtype=1)。