hls转流服务的nginx配置和一些脚本

前言

        当今,流媒体服务已成为互联网中不可或缺的重要组成部分。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 allow
                      allow publish all; # control access privilege
                      allow 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 4
    goto 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 off

set "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,因此需要在摄像头后台将【复合流】视频更换为【视频流】

        主码流无法更改视频流设置的,可以试试更改子码流

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx配置HLS的步骤如下: 1. 首先,需要安装并编译支持HLSnginxffmpeg。编译nginx时需要添加rtmp模块和HLS模块的支持。具体的编译选项可以参考提供的成功编译的nginx配置选项。这一步不是必须的,你也可以通过apt-get或yum安装nginx。 2. 在nginx配置文件(一般是nginx.conf)中添加以下配置: ``` rtmp { server { listen 1935; # RTMP服务监听的端口号 application live { live on; hls on; hls_path /path/to/hls/files; # HLS切片保存的路径 hls_fragment 10s; # 切片时长(以秒为单位) hls_playlist_length 60s; # 播放列表长度(以秒为单位) } } } ``` 这个配置将启用RTMP服务,并且在live应用程序中启用HLS。你需要根据实际情况修改`hls_path`为HLS切片的存储路径。 3. 重新加载nginx配置或重启nginx服务: ``` sudo nginx -s reload ``` 4. 使用支持HLS的浏览器打开`http://server-ip-address:8080`可以观看直播。如果无法观看,可以尝试访问`http://server-ip-address:8080/hls/livehls.m3u8`进行测试。 请注意,以上步骤仅仅是配置nginx以支持HLS,还需要使用ffmpeg将实时视频流推送到nginx。推送的流仍然是使用RTMP协议。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [基于NGINXHLS直播服务搭建](https://blog.csdn.net/jonta/article/details/62229159)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [nginx上搭建HLS流媒体服务器](https://blog.csdn.net/cjsafty/article/details/7922849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值