使用Nginx实现简单的RTMP推流直播笔记

实现结果

推流的实现:使用OBS Studio进行推流
拉流播放的实现:使用VLC进行拉流地址播放
直播服务器实现:centos系统下的nginx+RTMP模块

实现步骤:

第1步:nginx相关依赖包的安装

nginx相关依赖包的安装:

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++   openssl-devel cmakepcre-develnanowget  gcc gcc-c++ ncurses-devel perl

其他需要用到的依赖包:

 yum -y install git
yum -y install nano
yum -y install wget

第2步:nginx-rtmp-module

nginx-rtmp-module,官方github地址

// 通过git clone 的方式下载到服务器上
[root~]# git clone https://github.com/arut/nginx-rtmp-module.git

第3步:安装Nginx服务器

1.下载Nginx解压,并安装rtmp和openssl的模块
PS:add-module=/data/bak/nginx-rtmp-module 中的/data/bak是自己的下载的相关模块的存放路径

[root~]# wget http://nginx.org/download/nginx-1.10.3.tar.gz 
[root~]# tar -zxvf nginx-1.10.3.tar.gz 
[root~]# cd nginx-1.10.3
//添加rtmp和openssl支持
[root~]# ./configure --add-module=/data/bak/nginx-rtmp-module --with-http_ssl_module
[root~]# make && make install

第4步:查看安装模块信息:

[root@localhost nginx-1.10.3]# ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
[root@localhost nginx-1.10.3]# nginx -V
nginx version: nginx/1.10.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --add-module=/data/bak/nginx-rtmp-module --with-http_ssl_module

第5步:启动nginx,查看启动的情况

[root@localhost nginx-1.10.3]# nginx -s stop
[root@localhost nginx-1.10.3]# nginx
[root@localhost nginx-1.10.3]# curl http://192.168.74.128/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost nginx-1.10.3]# 

第6步:配置相关的模块信息修改nginx.conf

PS:注意是在http{}外进行添加:

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }
        application vod {
        play /opt/video/vod;
        }

    }
}

完成的配置文件如下:

==
#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;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /stat {     #第1处添加的location字段。
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
        }

        location /stat.xsl { #第2处添加的location字段。
           root /usr/local/nginx/nginx-rtmp-module/;
        }

        location / {
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

rtmp {
    server {
        listen 1935;
        application rtmplive {
            live on;
            record off;
        }
        application vod {
        play /opt/video/vod;
        }

    }
}

第7步:关闭防火墙,为方便测试

ps:一般线上环境是不会直接关闭防火墙,而是开放对应的端口。写对应的规则!

systemctl stop firewalld

第8步:重启的启动nginx,并用浏览器打开:http://192.168.74.128/stat

[root@localhost nginx-1.10.3]# nginx -s reload
[root@localhost nginx-1.10.3]# curl http://192.168.74.128/stat
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="stat.xsl" ?>
<rtmp>
<nginx_version>1.10.3</nginx_version>
<nginx_rtmp_version>1.1.4</nginx_rtmp_version>
<compiler>gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) </compiler>
<built>Feb 26 2018 21:48:59</built>
<pid>29574</pid>
<uptime>608</uptime>
<naccepted>0</naccepted>
<bw_in>0</bw_in>
<bytes_in>0</bytes_in>
<bw_out>0</bw_out>
<bytes_out>0</bytes_out>
<server>
<application>
<name>rtmplive</name>
<live>
<nclients>0</nclients>
</live>
</application>
<application>
<name>vod</name>
<play>
<nclients>0</nclients>
</play>
</application>
</server>
</rtmp>

PS:说明启动成功

<name>rtmplive</name>
<live>
<nclients>0</nclients>
</live>
</application>
<application>
<name>vod</name>

第9步:使用OBS Studio进行推流

首先在PC端安装一下OBS Studio :https://obsproject.com/download#mp
启动后的软件示意图:

image.png

点击来源:
直播来源:


image.png
image.png

点击确定:

image.png

点击设置,设置推送地址流服务器 rtmp://192.168.74.128/rtmplive

image.png

点击开始推送:


image.png

说明连接服务器成功:

image.png

如果关闭了nginx,则会提示:

image.png

第10步:使用VLC播放器进行播放拉流地址

image.png

最终结果:


image.png

PS:使用另一种方式使用ffmpeg进行推流的实现,待验证:
地址:http://blog.csdn.net/redstarofsleep/article/details/45092147

ffmpeg -re -i "D:\download\film\aqgy\02.mp4" -vcodec libx264 -vprofile baseline -acodec aac  
 -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -q 10 rtmp://192.168.74.128:1935/  
myapp/test1

参考地址:
http://blog.csdn.net/kingroc/article/details/50839994

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,470评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,393评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,577评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,176评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,189评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,155评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,041评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,903评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,319评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,539评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,703评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,417评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,013评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,664评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,818评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,711评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,601评论 2 353

推荐阅读更多精彩内容