【Linux】Ngnix + Flv | RTMP | 搭建视频服务器
作者:互联网
一、场景说明
1、通过Nginx + Flv模块搭建视频服务器
2、通过ffmpeg拉去摄像头视频,推送到视频服务器
3、通过flv.js拉取视频流(http方式播放视频)
二、操作步骤
1、安装Nginx,参考:安装Nginx-方案二
2、下载Nginx-Flv模块
1)码云地址
2)云盘地址,密码:6326
3、Nginx-Flv模块上传服务器
1)进入到Nginx安装目录
cd /usr/local/nginx/
2)上传nginx-http-flv-module-master.zip并解压,并重命名为nginx-http-flv-module
3)zip解压
unzip nginx-http-flv-module-master.zip
4)重命名
mv nginx-http-flv-module-master nginx-http-flv-module
4、进入到Nginx安装包目录,重新编译
1)进入Nginx安装目录
cd /opt/nginx/nginx-1.18.0
2)重新编译,编译增加Nginx-Flv模块
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_gzip_static_module --with-pcre --with-stream --add-module=/usr/local/nginx/nginx-http-flv-module
3)重新编译,执行编译并安装
make && make install
5、配置Nginx
1)进入Nginx安装目录
cd /usr/local/nginx/conf/
2)编辑nginx.conf
vim nginx.conf
3)增加内容-rtmp
内容:
rtmp_auto_push on; rtmp_auto_push_reconnect 1s; rtmp_socket_dir /tmp; rtmp{ out_queue 4096; out_cork 8; max_streams 128; timeout 15s; drop_idle_publisher 15s; log_interval 5s; log_size 1m; server{ listen 1935; application myapp{ live on; gop_cache on; record off; } application hls{ live on; hls on; hls_path /usr/local/nginx/html/hls; } application dash{ live on; dash on; dash_path /usr/local/nginx/html/dash; } } }
说明1: 与http同级别
说明2:端口1935,若冲突,自行修改
说明3:application myapp,myapp即应用名,即推流地址为: rtmp://${ip}:1935/myapp/${stream_name}
说明4: ${stream_name},即数据流名,可理解为数据流ID
4)增加内容-http
内容:
server { listen 19351; location /live{ flv_live on; chunked_transfer_encoding on; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; } location /hls{ types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /usr/local/nginx/html/hls; add_header 'Cache-Control' 'no-cache'; } location /dash { root /usr/local/nginx/html/dash; add_header 'Cache-Control' 'no-cache'; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-http-flv-module; } location /control { rtmp_control all; #configuration of control module of rtmp } }
说明1:放到http内,与server同级
说明2:端口19351
说明3- 拉流地址为:
http://${ip}:19351/live?port=1935&app=${appName}&stream=${streamName} # ${appName},即应用名,eg:myapp # ${streamName},即数据流名,eg: live_stream # eg: http://${ip}:19351/live?port=1935&app=myapp&stream=live_stream
5)完整nginx.conf
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; user root; events { worker_connections 1024; } rtmp_auto_push on; rtmp_auto_push_reconnect 1s; rtmp_socket_dir /tmp; rtmp{ out_queue 4096; out_cork 8; max_streams 128; timeout 15s; drop_idle_publisher 15s; log_interval 5s; log_size 1m; server{ listen 1935; application myapp{ live on; gop_cache on; record off; } application hls{ live on; hls on; hls_path /usr/local/nginx/html/hls; } application dash{ live on; dash on; dash_path /usr/local/nginx/html/dash; } } } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 19351; location /live{ flv_live on; chunked_transfer_encoding on; add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Credentials' 'true'; } location /hls{ types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /usr/local/nginx/html/hls; add_header 'Cache-Control' 'no-cache'; } location /dash { root /usr/local/nginx/html/dash; add_header 'Cache-Control' 'no-cache'; } location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/nginx/nginx-http-flv-module; } location /control { rtmp_control all; #configuration of control module of rtmp } } server { listen 8081; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
6、重启Nginx
1)停止Nginx
systemctl stop nginx
2)启动Nginx
systemctl start nginx
三、测试-推流
1、下载ffmpeg,Windows版
1)直接下载
2)直接下载若失效,请到官网下载
3)云盘备用,密码:6326
2、设置环境变量
1)若设置,Path添加:${前缀}\ffmpeg-2021-07-25-git-a2a7547b2f-full_build\bin
2)若不设置,ffmpeg指令将cmd切换到ffmpeg的bin目录
3、检测ffmpeg
1)打开cmd
2)查看设备
ffmpeg -list_devices true -f dshow -i dummy
示例图:
说明1:计划用自带摄像头进行测试,如果是远程摄像头,原理差不多
3)打开摄像头,测试摄像头是否可用
ffplay -f dshow -i video="EasyCamera"
说明: 执行后,会打开窗体,显示摄像内容
示例图:
推流指令备份-视频文件(此指令备份后续用,测试时不用执行)
ffmpeg -re -i demo.wmv -f flv rtmp://127.0.0.1:1935/live/123
4)推流-摄像头
ffmpeg -y -rtbufsize 100M -f dshow -i video="EasyCamera" -vcodec libx264 -preset:v ultrafast -tune:v zerolatency -f flv rtmp://ip:1935/myapp/live
说明1: 更换摄像头名称,我的名称是“EasyCamera”,你的不一定
说明2: 修改rtmp的服务器地址,修改你的IP即可
说明3: 如果报错的话,还请自行度娘
说明4: 执行此命令前,先关闭“3)打开摄像头,测试摄像头是否可用”的摄像窗体,不然摄像头被占用,推流失败
说明5: 如果遇到 “I/O error”,检查推流地址是否正确;正确示例:
rtmp://ip:1935/myapp/live # myapp, 应用名;nginx.conf中,rtmp中需配置application myapp # live,数据流名,无需配置;推流时,用live,拉流时,也用live即可
推流成功-示例图:
四、测试-拉流
1、下载flv.min.js,密码:6326
2、下载flv-index.html,密码: 6326
3、修改flv-index.html
1)编辑flv-index.html
2)修改flv地址,62行
3)url修改为:
http://ip:19351/live?port=1935&app=myapp&stream=live
app,即应用名
stream,即数据流名,与推流时保持一致
说明1: ip跟端口,记得换成自己服务器的IP跟端口
4、运行flv-index.html
五、其他说明
1、拉流测试,也可以通过三方软件,进行二次确认,比如: VLC
1)VLC,官网下载
2)VLC,云盘下载,密码:6326
3)VLC使用示例图:
2、暂时OK,有问题,还请思考后再度娘
标签:http,Ngnix,Flv,nginx,live,html,rtmp,flv,RTMP 来源: https://blog.csdn.net/myloverisxin/article/details/119181586