前言
当今,流媒体服务已成为互联网中不可或缺的重要组成部分。HLS(HTTP Live Streaming)作为一种流媒体传输协议,被广泛应用于视频直播、点播等应用场景中。而Nginx作为一款高性能的Web服务器,也可以被用于构建HLS转流服务。
在这篇博客中,我们将介绍:
如何使用Nginx配置HLS转流服务,并提供一些Ffmpeg的相关脚本,这些脚本可用于自动化管理转流服务
并且,先前已经给出流媒体服务的搭建了:
利用Nginx搭建流媒体服务【centos/windows】_nginx流媒体服务器配置-CSDN博客
nginx配置
这里以windows系统为例,linux也没啥区别,我将hls转流片段留在如下路径
【C:/Users/Administrator/FISH/nginx/www/hls】
#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;events {worker_connections 1024;
}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 80;server_name localhost;add_header Access-Control-Allow-Origin *;#charset koi8-r;#access_log logs/host.access.log main;# The location is determined by the root config.# If your m3u8 path is saved in C:/Users/Administrator/FISH/nginx/www/hls,# you can also choose the location [/www/hls] with the root [C:/Users/Administrator/FISH/nginx].location /hls {types {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root C:/Users/Administrator/FISH/nginx/www;expires -1;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Headers X-Requested-With;add_header Access-Control-Allow-Methods GET,POST,OPTIONS;} 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;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}# 服务代理# 将本地服务从别的端口和地址弄出来server {listen 8000;server_name proxy_hls;add_header Access-Control-Allow-Origin *;# 自定义路径location /hls/m3u8 {proxy_pass http://127.0.0.1/hls;}}# another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}# 使用rtmp插件,构建流媒体服务,对多余的切片进行删除和优化视频
rtmp {server {listen 19351;publish_time_fix on;# 允许播放和接收视频流application myapp {live on; #stream on live allowallow publish all; # control access privilegeallow play all; # control access privilege}application hls {live on;hls on; #这个参数把直播服务器改造成实时回放服务器。hls_path C:/Users/Administrator/FISH/nginx/www/hls; #切片视频文件存放位置。wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。hls_fragment 10s; #每个视频切片的时长。hls_playlist_length 30s; #总共可以回看的事件,这里设置的是1分钟。hls_continuous on; #连续模式。hls_cleanup on; #对多余的切片进行删除。hls_nested on; #嵌套模式。}}
}
转流服务管理脚本
1、转流服务脚本
@echo off:loop
ffmpeg -re -rtsp_transport tcp -i rtsp://admin:52996@172.6.6.6:554 -vf "scale=640:480" -r 15 -c:a aac -strict -2 -f hls -hls_list_size 3 -hls_time 15 -hls_flags delete_segments -segment_list_flags +delete_segments C:/Users/Administrator/FISH/nginx/www/hls/004.m3u8
IF %ERRORLEVEL% neq 0 (echo "ffmpeg命令执行失败"timeout /t 4goto loop
)
goto loop
这是一个bat脚本,目的是将【rtsp://admin:52996@172.6.6.6:554】的rtsp视频流按照【640:480】的比例转流为m3u8视频,并以【004.m3u8】的命名存储在【C:/Users/Administrator/FISH/nginx/www/hls】目录中,为了防止转流服务异常中断,这里设置了循环,在每次失败后将在四秒后重新启动。
注:摄像头视频RTSP流默认为554,但不排除有用8080或其他端口、或者http协议播放的
2、定时杀死转流服务
因为在转流过程中老是会出现不可预料的问题,导致ffmpeg转流服务进程持续运行,但就是类似卡死一样,处于挂起状态,这里给出一种杀死转流服务的脚本(可与【1】配套使用),请联系实际使用,仅供参考
@echo off
taskkill /F /IM ffmpeg.exe
3、清理过多的ts文件
考虑到有些服务器装不了rtmp模块,无法自动清理ts文件。这里给出脚本,用于清理存在超过24小时的文件。
@echo offset "hls_dir=C:\Users\Administrator\FISH\nginx\www\hls"
set "max_age_hours=24"forfiles /P "%hls_dir%" /S /M *.ts /D -%max_age_hours% /C "cmd /c if @isdir==FALSE del @path"
另给出一个小tip:
前端hls.js暂没找到办法播放【复合流】hls,因此需要在摄像头后台将【复合流】视频更换为【视频流】
主码流无法更改视频流设置的,可以试试更改子码流