1. 程式人生 > >Nginx搭建RTMP推拉流伺服器

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伺服器就算是完成了。不妨來嘗試一下,其實還是挺有意思的。