Nginx搭建rtmp直播服务器实现代码

 更新时间:2020年11月11日 10:59  点击:1649

1.到nginx源码目录新建个rtmp目录 ,进入 git clone https://github.com/arut/nginx-rtmp-module.git

2.重编译nginx 代码如下

./configure --prefix=/usr/local/nginx-1.2.9/ --add-module=./rtmp/nginx-rtmp-module --with-http_ssl_module --with-pcre=/lamp_source/pcre-8.38;
make;make install;
#重新安装nginx

3.完成安装后。

打开nginx.conf 在http{server...} 之后加上以下代码

rtmp{
    server{
        listen 1935;
        chunk_size 4000;
        application hls {
            live on;
            hls on;
            hls_path /你的服务器路径这个用于存放缓存文件的。必须可写;
            hls_fragment 5s;
        }
    }
}

然后新建个主机配置

server {
 
    listen 8851;
    location /index.html {
    root /网站存放目录;
}
    location / {# return 403;
      # Serve HLS fragments
      types {
        application/vnd.apple.mpegurl m3u8;
        video/mp2t ts;
      }
      root /同上,缓存用;
      expires -1;
    }
  }

完成以上配置后。 下载一个软件叫做 open broadcaster software 的软件推流即可(注意,也可以使用ffmpeg 进行推流)。 软件使用如截图

然后点开始串流就可以了。

下载打开vlc播放器(拉流)。输入 rtmp://你的网址:1935/hls/上图中的密码 然后就ok了。 成功在线直播

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章