Nginx搭建RTMP推拉流服务器

推流效果
拉流效果

如题,今天就来实现一个推拉流服务器,模拟下推流后被客户端拉流看到效果。

详细步骤如下

  • 安装Nginx
  • 安装FFmpeg
  • 安装VLC客户端

安装Nginx

在Mac上有一个很好用的包管理插件,名为homebrew。 具体的安装可以自行去搜索下。下面就借助Homebrew来安装Nginx。

首先是拉取Nginx

$ brew tap home/nginx

执行安装

$ brew install nginx-full --with-rtmp-module

这里需要注意的就是后面的--with-rtmp-module参数,其意思就是带上rtmp的模块,这样我们才能借助Nginx实现一个rtmp的推拉流服务器。

安装过程中,homebrew或帮我们自动的安装如pcre,openssl等模块。因此相对于其他平台的安装方式或者源码安装方式,homebrew贼省心。
经过稍长的等待时间,带有rtmp模块的Nginx就安装好了。查看安装详情的命令为:

brew info nginx-full

就可以看到具体的安装信息了,比如配置文件在哪里,可执行文件又在哪里。
我这里有如下路径:

  • 配置文件路径 /usr/local/etc/nginx/
  • web容器路径 /usr/local/var/www
  • 可执行文件路径/usr/local/Ceallar/nginx/

配置rtmp

在nginx.conf的HTTP节点后面添加一个同级别的rtmp接单。具体内容如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

error_log   /usr/local/var/logs/nginx/error.log debug;
pid         /usr/local/var/run/nginx.pid;

#pid        logs/nginx.pid;


events {
    worker_connections  256;
}


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   /usr/local/var/logs/access.log  main;
    #access_log  logs/access.log  main;

    sendfile        on;
    port_in_redirect off;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            root    /usr/local/var/www;
            index  index.html index.htm index.php;
        }

        #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   /usr/local/var/www;
        }

        # 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$ {
            fastcgi_intercept_errors on;
            #root           html;
            root            /usr/local/var/www;
            fastcgi_pass   127.0.0.1:9000;
            #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$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;
    #    }
    #}
    include servers/*;
}

rtmp {
server {
  listen 1935;
  chunk_size 4000;
  application rtmplive {
    live on;
    max_connections 1024;
  }

  application hls {
    live on;
    hls on;
    hls_path /usr/local/var/www/hls;
    hls_fragment 1s;
  }
}
}

最后面hls_path就是待会要用到的推流文件目录了。一般来说不必创建,如果出现文件夹权限问题的话,手动添加下可读可写权限就可以了。

安装ffmpeg

安装它在其他的平台上可能会超级费劲,但是在Mac上,有了homebrew,那就真的不是事了。

➜  $/opt nginx brew install ffmpeg
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (caskroom/cask).

==> Installing dependencies for ffmpeg: lame, x264, xvid
==> Installing ffmpeg dependency: lame
==> Downloading https://homebrew.bintray.com/bottles/lame-3.99.5.high_sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring lame-3.99.5.high_sierra.bottle.1.tar.gz
🍺  /usr/local/Cellar/lame/3.99.5: 27 files, 2MB
==> Installing ffmpeg dependency: x264
==> Downloading https://homebrew.bintray.com/bottles/x264-r2795.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring x264-r2795.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/x264/r2795: 11 files, 3.2MB
==> Installing ffmpeg dependency: xvid
==> Downloading https://homebrew.bintray.com/bottles/xvid-1.3.4.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring xvid-1.3.4.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/xvid/1.3.4: 10 files, 1.2MB
==> Installing ffmpeg
==> Downloading https://homebrew.bintray.com/bottles/ffmpeg-3.4.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ffmpeg-3.4.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/ffmpeg/3.4: 248 files, 50.9MB

安装VLC客户端

VLC客户端是一个很好用的可以拉流并进行读取的软件,Mac上挺好用的。


VLC打开后大致如下

开始推流,拉流

推流

推流之前,先准备一个视频软件。我就直接用QQ的录屏来录制了一个视频,放在桌面上,名为demo.mp4

然后在命令行里面输入:

ffmpeg -re -i 本地视频路径如(如/Users/changba/Desktop/Player/demo.mp4)  -vcodec copy -f flv rtmp://localhost:1935/rtmplive/home

这里rtmplive是上面的配置文件中,配置的应用的路径名称;后面的room可以随便写,待会使用拉流软件的时候把地址对应上就可以了。

rtmp的配置

输入完之后,就可以打开VLC客户端了。


推流效果

拉流

具体操作为:file-->>Open Network
然后在弹出的URL框中输入如下链接。

rtmp://localhost:1935/rtmplive/home

记得对应上名字就可以了,大致的拉流效果如下:

拉流效果

总结

至此,基于rtmp的推拉流的Nginx服务器就算是完成了。不妨来尝试一下,其实还是挺有意思的。

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

推荐阅读更多精彩内容