Nginx rtmp 直播
首页 > >    作者:lininn   2020年2月27日 17:17 星期四   热度:1843°   百度已收录  
时间:2020-2-27 17:17   热度:1843° 

Nginx rtmp

第一步需要安装nginx
具体过程参考Centos 安装 Nginx 详细过程。
已安装 的 nginx -V

wget -c http://nginx.org/download/nginx-1.8.1.tar.gz

下载指定版本,编译原有参数加--add-module=/home/nginx_rtmp/nginx-rtmp-module && make

之后objs下nginx替换原有nginx即可


第二步安装编译rtmp模块
从官方地址拉下源码:https://github.com/arut/nginx-rtmp-module
保存在一个临时目录下:/home/nginx_rtmp
在nginx 源代码目录下运行:

./configure --add-module=/home/nginx_rtmp/nginx-rtmp-module && make && make install
1
注意:是在nginx的源代码路径下运行不是在安装后的目录

安装已经结束,下面来看配置。
在nginx安装目录下先复制一份默认的配置文件出来:

cd /usr/local/nginx/conf
cp nginx.conf live_rtmp.conf
编辑live_rtmp.conf如下:
#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;
}

rtmp {

    server {

        listen 1935;

        chunk_size 4000;

        # TV mode: one publisher, many subscribers
        application mylive {

            # enable live streaming
            live on;

            # record first 200M of stream
            record all;
            record_path /home/live_record;
            record_max_size 200M;

            hls on;
            hls_path /home/hls;
            hls_fragment 1s;
            hls_playlist_length 5;

            allow play all;

            #on_publish 'http://when start publish live call this url';
            #on_done 'http://when live stop call this url';
        }
    }
}

http {
    server {
        listen       8080;

        # This URL provides RTMP statistics in XML
        location /stat {
            rtmp_stat all;

            # Use this stylesheet to view XML as web page
            # in browser
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl {
            # XML stylesheet to view RTMP stats.
            # Copy stat.xsl wherever you want
            # and put the full directory path here
            root /usr/local/nginx/html/;
        }

        location /hls {
            # Serve HLS fragments
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /home;
            add_header Cache-Control no-cache;
        }
   }

}


验证配置文件正确性:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/live_rtmp.conf 
1
配置文件说明:
1、hls开启后产生的m3u8文件会存在hls_path下,播放端调用404的时候先去看看m3u8有没有生成。
2、stat.xsl要从nginx-rtmp的源代码根目录中考出来,考到配置的那个文件夹:

cp /home/nginx_rtmp/stat.xsl /usr/local/nginx/html/

3、验证的时候遇到个报错:

nginx: [emerg] unknown directive “rtmp” in xxxxx

这问题由两个方面造成,先确保安装rtmp模块的时候没有报错,其次是配置文件的编码最好是ASCII text,可以使用file nginx.conf指令查看一下。我的问题是第一个,安装的时候有报错:

报了大量的   $'\r': command not found

回忆了一下犯了个错误,我是在windows下git拉下来考到服务器上的,中间文件换编码了。。。。切记切记。。。。。要直接从linux git clone下来再安装。。。。
重新安装rtmp之后解决了这个问题。

4、./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using –with-openssl= option.

解决方法:
执行命令yum -y install openssl openssl-devel 成功之后再在nginx 源代码目录下运行
./configure

启动nginx之后就可以开始推流了:

/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/live_rtmp.conf 

推流地址: rtmp://服务器ip/mylive/xxxxx
播放地址: rtmp://服务器ip/mylive/xxxxx
hls地址: http://服务器ip:8080/hls/xxxxx.m3u8
状态查看地址:http://服务器ip:8080/stat

https://blog.csdn.net/O0mm0O/article/details/73896384


二维码加载中...
本文作者:lininn      文章标题: Nginx rtmp 直播
本文地址:?post=435
版权声明:若无注明,本文皆为“覆手为雨”原创,转载请保留文章出处。
分享本文至:

返回顶部    首页    手机版本    后花园   会员注册   
版权所有:覆手为雨    站长: lininn