簡介和安裝
Nginx 本身是一個非常出色的HTTP服務器,OBS (Open Broadcaster Software) 是一個免費且開源的,全平台支持的視頻錄制和直播軟件。這兩個東西通過一個nginx的模塊nginx-rtmp-module組合在一起,即可以搭建一個功能流媒體服務器。這個流媒體服務器可以支持RTMP和HLS(Live Http Stream)。
FFmpeg也是一個非常優秀的音視頻解決軟件。但OBS的界面更加友好,且支持屏幕捕獲,所以我選擇OBS。其實是因為FFmpeg沒有GUI界面,所以我懶得再去研究它。
關於Nginx的安裝,可以參考這里。在Linux端的Nginx與RTMP模塊的安裝,可以參考這里。RTMP模塊的Github地址在這里,你可以在這里看到更為詳盡的使用說明。
作者在Windows環境下搭建服務器,並且僅僅以最簡單的方式實現作者需要的功能。
配置
假定你已經安裝好了Nginx和RTMP插件,現在是使用它的時候了。
首先,定義一個Nginx的配置文件nginx-win-rmtp.conf
。
展開查看范例
#user nobody;
# multiple workers works !
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 8192;
# max value 32768, nginx recycling connections+registry optimization =
# this.value * 20 = max concurrent connections currently tested with one worker
# C1000K should be possible depending there is enough ram/cpu power
# multi_accept on;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
}
}
}
http {
#include /nginx/conf/naxsi_core.rules;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr:$remote_port - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
# # loadbalancing PHP
# upstream myLoadBalancer {
# server 127.0.0.1:9001 weight=1 fail_timeout=5;
# server 127.0.0.1:9002 weight=1 fail_timeout=5;
# server 127.0.0.1:9003 weight=1 fail_timeout=5;
# server 127.0.0.1:9004 weight=1 fail_timeout=5;
# server 127.0.0.1:9005 weight=1 fail_timeout=5;
# server 127.0.0.1:9006 weight=1 fail_timeout=5;
# server 127.0.0.1:9007 weight=1 fail_timeout=5;
# server 127.0.0.1:9008 weight=1 fail_timeout=5;
# server 127.0.0.1:9009 weight=1 fail_timeout=5;
# server 127.0.0.1:9010 weight=1 fail_timeout=5;
# least_conn;
# }
sendfile off;
#tcp_nopush on;
server_names_hash_bucket_size 128;
## Start: Timeouts ##
client_body_timeout 10;
client_header_timeout 10;
keepalive_timeout 30;
send_timeout 10;
keepalive_requests 10;
## End: Timeouts ##
#gzip on;
server {
listen 80;
server_name localhost;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root nginx-rtmp-module/;
}
location /control {
rtmp_control all;
}
#charset koi8-r;
#access_log logs/host.access.log main;
## Caching Static Files, put before first location
#location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
# expires 14d;
# add_header Vary Accept-Encoding;
#}
# For Naxsi remove the single # line for learn mode, or the ## lines for full WAF mode
location / {
#include /nginx/conf/mysite.rules; # see also http block naxsi include line
##SecRulesEnabled;
##DeniedUrl "/RequestDenied";
##CheckRule "$SQL >= 8" BLOCK;
##CheckRule "$RFI >= 8" BLOCK;
##CheckRule "$TRAVERSAL >= 4" BLOCK;
##CheckRule "$XSS >= 8" BLOCK;
root html;
index index.html index.htm;
}
# For Naxsi remove the ## lines for full WAF mode, redirect location block used by naxsi
##location /RequestDenied {
## return 412;
##}
## Lua examples !
# location /robots.txt {
# rewrite_by_lua '
# if ngx.var.http_host ~= "localhost" then
# return ngx.exec("/robots_disallow.txt");
# end
# ';
# }
#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; # single backend process
# fastcgi_pass myLoadBalancer; # or multiple, see example above
# 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 spdy;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_prefer_server_ciphers On;
# ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:ECDH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!eNULL:!MD5:!DSS:!EXP:!ADH:!LOW:!MEDIUM;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
挑重點的說,我們需要注意的是
rtmp {
server {
listen 1935;
chunk_size 4000;
application live {
live on;
}
}
}
此處的listen指的是你的rtmp開在了本地的哪個端口,當你需要鏈接服務器的時候,別忘了把端口號加上,如果設備處於內網,那么記得打開端口映射,防火牆選擇放行。因為我們關注的只有rtmp服務器,所以無需關注其他端口。
OBS推流
打開OBS。
1.進入推流設置,將服務設置為“自定義”。
2.服務器設置為rtmp://IP:Port/APP
。
3.串流秘鑰不填寫,作者不會使用權限控制的功能。想要進行權限設置,可以參考這里。
因為在配置文件中我們創建的
app
名字就叫live
,所以在這里APP填寫為live。如rtmp://192.168.1.110:1935/live
。
運行
我寫了一個Powershell腳本來運行nginx。可以在一個PS窗口中非常方便的運行和結束nginx。
該腳本默認nginx-win-rtmp.conf
配置文件在nginx.exe目錄下的\conf
目錄中。
而nginx.exe
的路徑在D:\Hider\Tools\FFmpeg+nginx+LIVE\nginx-1.7.11.3-Gryphon
中,請根據實際情況自行修改。
cd "D:\Hider\Tools\FFmpeg+nginx+LIVE\nginx-1.7.11.3-Gryphon"
# 若nginx已經運行,則停止
Get-Process -Name "nginx" | Stop-Process
$job = Start-Job -ScriptBlock {
cd "D:\Hider\Tools\FFmpeg+nginx+LIVE\nginx-1.7.11.3-Gryphon";
.\nginx.exe -c conf\nginx-win-rtmp.conf;
}
Write-Host "Starting...`n"
sleep 2
Get-Job
Write-Host "`n"
Get-Process -Name "nginx" | Format-Table
do {
Write-Host "`nNginx is running, finish it?`n";
$exit = Read-Host "Type Y to finish";
Write-Host "`n"
} while ($exit -ne "Y")
Write-Host "Before:"
Get-Process -Name "nginx" | Format-Table
.\nginx.exe -s quit
# 先平穩關閉,若失敗則強制結束進程
Get-Process -Name "nginx" | Stop-Process
sleep 1
Write-Host "`nAfter:"
Get-Process -Name "ngin*" | Format-Table
Write-Host "(It has been finished if there were no output.)"
Write-Host "`nNginx has been finished.`n"
pause
拉流
讓OBS開始推流,如果一切都沒有問題,此時應該是可以正常運行的。接下來,我們要讓客戶端拉取直播。
作者使用Potplayer,各個(靠譜的)播放器播放流的方法都大同小異:
1.菜單 - 打開 - 打開流(或 打開鏈接 等)
2.輸入剛剛你推流的rtmp://網址
,和OBS推流地址相同。
完成
到此為止,你應該已經成功的在本地搭建了一個簡單的nginx直播服務器,並且能夠正常觀看。
十分感謝您的閱讀,這是我第一次寫博客,希望能夠得到更多的指教,包括技術方面和寫作方面的。