Nginx + Rtmp 做直播推流

一、安装Nginx

cd usr/local/src 首先跳转目录

wget http://nginx.org/download/nginx-1.17.1.tar.gz

tar -xzvf nginx-1.17.1.tar.gz

cd nginx-1.17.1

嗯,,,貌似少了点什么,,,,额,,对了 如果 nginx做Rtmp推流需要一个扩展 cd.. 退出当前目录 返回到/usr/local/src 下载 git clone https://github.com/arut/nginx-rtmp-module.git

安装 Nginx需要 openssl zlib pcre 下载安装就行了 openssl可以直接通过 https://www.openssl.org/source/ 自己需要的版本直接解压即可

zlib: http://www.zlib.net/

pcre: http://www.pcre.org/

好了那就来安装Nginx 在 /usr/local 下面 建立 nginx 文件夹就行了

在 /usr/local/src/ nginx-1.17.1 (这是nginx的解压目录哦) 执行以下命令:

./configure --prefix=/usr/local/nginx  --with-openssl=/usr/local/src/openssl-1.0.2 --with-pcre=/usr/local/src/pcre-8.37  --with-zlib=/usr/local/src/zlib-1.2.8 --add-module=/usr/local/src/nginx-rtmp-module --with-http_ssl_module<br> make<br> make install

安装完成之后 打开nginx的配置文件 vi /usr/local/nginx/conf/nginx.conf

rtmp{
     server{
         listen 1935;
         chunk_size 4000;
         application live{             #推流请求地址 rtmp://server_name/live/test
             live on;
             on_publish http://server_name/live/hash_key.php; #如果需要验证直播码在这里填入验证文件
             hls  on;
             hls_path /usr/local/webserver/nginx/html/live/hls; #hls流保存地址
             hls_fragment 5s;
         }
     }
 }
location ^~ /play { 
        # rtmp://server_name/live/test
        # 直播播放地址 http://server_name/play/test

        add_header Access-Control-Allow-Origin *;

        alias  /usr/local/webserver/nginx/html/live/hls;
}

location /hls {

        types{
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
        }

        root /usr/local/webserver/nginx/html/live/hls;
        add_header  Cache-Control on-cache;
        add_header Access-Control-Allow-Origin *;

}

然后再http模块下添加

二、前端通过 Videojs播放

<script src = "https://unpkg.com/video.js@7.3.0/dist/video.js"></script>
<script src="https://unpkg.com/flv.js/dist/flv.min.js"></script>
<script src="https://unpkg.com/videojs-flvjs/dist/videojs-flvjs.min.js"></script>
<script src = "https://cdn.bootcss.com/videojs-contrib-hls/5.14.1/videojs-contrib-hls.js"></script> 

 <video id = "live-video" style="width: 100%;height: 520px;" class = "video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" data-setup='{"muted":false}'>
<source src="http://server_name/play/test" type="application/x-mpegURL"
</video> 

演示地址:http://o4d.cn/live/1.html

点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注