原创

Nginx-rtmp-module搭建直播服务器并实现直播

1.下载nginx rtmp module,然后安装(这里不做详细介绍)

[root@localhost]# wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

2.下载一个同版本的nginx

yum安装的nginx没有configure,也别折腾了最简单的快速就是在下载一个 可以编译扩展的nginx

[root@localhost]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
[root@localhost]# tar -zxvf nginx-1.16.0.tar.gz

然后进入刚下载的nginx源码目录 编译一下子

[root@localhost]# ./configure --prefix=/root/lshTool/nginx-1.16.0/nginx --add-module=/root/lshTool/nginx-rtmp-module-master/  --with-http_stub_status_module --with-http_ssl_module

configuring additional modules
adding module in /root/lshTool/nginx-rtmp-module-master/
 + ngx_rtmp_module was configured
checking for PCRE library ... found
checking for PCRE JIT support ... found
checking for OpenSSL library ... found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/root/lshTool/nginx-1.16.0/nginx"
  nginx binary file: "/root/lshTool/nginx-1.16.0/nginx/sbin/nginx"

出现上面的信息就是编译成功了
然后make 编译之后的nginx二进制文件就在objs目录下,不建议 make install 有可能覆盖你本机之前的nginx配置

[root@localhost]# make
"查看是否成功"
[root@localhost nginx-1.16.0]# ./objs/nginx -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/root/lshTool/nginx-1.16.0/nginx --add-module=/root/lshTool/nginx-rtmp-module-master/ --with-http_stub_status_module --with-http_ssl_module

3.报错解决

Centos7.4执行添加rtmp模块的时候会报 ./configure: error: SSL modules require the OpenSSL library.
执行以下命令然后继续添加rtmp模块即可

[root@localhost]# yum -y install openssl openssl-devel

./configure: error: C compiler cc is not found
执行以下命令然后继续添加rtmp模块即可

[root@localhost]# yum install  gcc
[root@localhost]# gcc -v

4.配置并运行

events {
    worker_connections  1024;
}
rtmp {
    server {
        listen 1935;
        chunk_size 4000;
        application hls {
            live on;
            #record all;
            #record_path /home/live_record;
            #record_max_size 200M;
            hls on;
            hls_path /root/lshTool/nginx-1.16.0/live/hls;
            hls_fragment 2s;
            #publish_notify on; #表示开启鉴权服务
            #on_publish http://127.0.0.1:80/publish; #鉴权地址
        }
    }
}

"运行即可"
[root@localhost nginx-1.16.0]# ./nginxLive -c /root/lshTool/nginx-1.16.0/rtmp.conf
"查看1935是否开启"
[root@localhost nginx-1.16.0]# netstat -nptl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9881/php-fpm: maste
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      5850/redis-server 1
tcp        0      0 0.0.0.0:1935            0.0.0.0:*               LISTEN      27324/nginx: master
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5854/nginx: master
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      5854/nginx: master
tcp        0      0 0.0.0.0:82              0.0.0.0:*               LISTEN      5854/nginx: master
tcp        0      0 0.0.0.0:83              0.0.0.0:*               LISTEN      5854/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      4176/sshd
tcp        0      0 0.0.0.0:8888            0.0.0.0:*               LISTEN      5854/nginx: master
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      4616/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5854/nginx: master
tcp6       0      0 :::3306                 :::*                    LISTEN      4264/mysqld
tcp6       0      0 :::8081                 :::*                    LISTEN      21918/java
tcp6       0      0 :::8082                 :::*                    LISTEN      21772/java
tcp6       0      0 :::22                   :::*                    LISTEN      4176/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      4616/master

5.下载OBS软件

  1. 现在几乎所有的直播平台用的 推流软件都是OBS要么就是基于OBS包装的
  2. 自己下载吧跟下载qq一样简单。。。

1.设置OBS的场景和来源,来源设置捕捉屏幕之类的或者自己选择特定的窗口捕捉

2.在设置里面设置推流地址和房间名

  1. 推流地址 URL如下图填写即可,换成自己的ip,
  2. 房间名随便填写,主要同一个直播服务器开多个直播间
  3. 房间名还可以带上参数用于校验是否是合法直播,校验需要在application里面配置on_publish 值为一个url地址根据返回 http code码200来进行播放
  4. 在推流地址和房间名也是在播放时候的重要参数

6.播放直播

1.找一个可以拉rtmp流或者根据m3u8文件地址播放视频的软件即可
2.浏览器拉rtmp流播放可以用 flowplayer 配置如下

 flowplayer("player", "${config.staticWebSite}/js/live/flowplayer-3.2.18.swf",{
        clip: {
                url: 'rtmp://47.111.174.201:1935/hls/fy',
                provider: 'rtmp',
                live: true,
                autoPlay: false,
                autoBuffering:false
            },
            plugins: {
            rtmp: {
                url: '${config.staticWebSite}/js/live/flowplayer.rtmp-3.2.8.swf',
                netConnectionUrl: 'rtmp://47.111.174.201:1935/hls/fy'
            }
        }
    });

3.还可以用video.js 根据m3u8文件地址播放,核心配置代码如下

"fy.m3u8其中fy就是推流时候配置的房间名,m3u8的地址会在hls_path的配置地址里面,还会生成很多ts文件"
<source src="http://ctodr.com:81/live/hls/fy.m3u8" type="application/x-mpegURL">

4.rtmp的效果比较好,延迟低,我的阿里云服务器是1核1g1M,延迟仅4秒

正文到此结束
本文目录