NGINX-RTMP实现HLS直播

·
Tech nginx March 15, 2018

HTTP Live Streaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议。是苹果公司QuickTime X和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包含元数据的extended M3U (m3u8)playlist文件,用于寻找可用的媒体流。
HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。

0x00 安装 NGINX 以及 RTMP模块

下载

  • nginx下载地址
    http://nginx.org/download/
  • nginx-rtmp-module 下载地址
    https://github.com/arut/nginx-rtmp-module

    编译

    下载完nginx源码后,解压,进入nginx目录

    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --add-module=../nginx-rtmp-module-master
    make
    make install

    其中--add-module=改为nginx-rtmp-module的目录

0x01 配置nginx

修改nginx配置文件 /usr/local/nginx/conf/nginx.conf

http {
    server {
        listen 80;

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /usr/local/nginx/www;
            add_header Cache-Control no-cache;
        }
    }
}
rtmp {
    server {
            listen 1935;
            application live {
                live on;
            }
            application hls {
                live on;
                hls on;
                hls_path /usr/local/nginx/www/hls;
            }
     }
}

其中/usr/local/nginx/www为ts流存放的目录

0x02 测试效果

  • RTMP流: rtmp://localhost:1935/live
  • HLS流: http://localhost:1935/hls/stream.m3u8

推流工具: OBS

RTMP流

OBS设置:

测试效果:

HLS流

OBS设置:

测试效果:

在/usr/local/nginx/www目录下能看到生产的ts流文件以及m3u8文件,文件名为obs中配置的串码流

root:~/ # ls /usr/local/nginx/www/hls                                                
stream-82.ts  stream-85.ts  stream-88.ts  stream-91.ts  stream-94.ts
stream-83.ts  stream-86.ts  stream-89.ts  stream-92.ts  stream-95.ts
stream-84.ts  stream-87.ts  stream-90.ts  stream-93.ts  stream.m3u8
  • Numpy 笔记
  • LRO导致openstack虚拟机网络问题

已有 5 条评论
  1. Sonic

    HLS流: http://localhost:1935/hls/stream.m3u8 请问这里是正确的吗? 端口不是80?

    Sonic February 2nd, 2020 at 11:10 pm回复
    1. Coo

      分成两部分 推流和流播放

      推流地址用的是 localhost:1935/hls
      端口是在 nginx 配置文件中的 rtmp server listen 配置的
      path(/hls) 是在 nginx 配置文件中 rtmp server application 配置的

      hls 流播放的地址是 http://localhost:80/hls/stream.m3u8
      配置是 nginx 配置文件的 http 部分

      Coo February 3rd, 2020 at 12:07 am回复
      1. Sonic

        多谢回复!踩过N个坑之后,下午终于弄好了。

        Sonic February 3rd, 2020 at 04:14 pm回复
  2. Sonic

    今天折腾了六七个小时,还是没搞定。期待指教~ 多谢

    Sonic February 2nd, 2020 at 11:35 pm回复
  3. 橙子

    谢谢,有用

    橙子 February 17th, 2020 at 12:14 pm回复
取消回复

说点什么?

© 2023 Coo's Note. Using Typecho & Moricolor.