Loading

使用 Nginx+OBS 搭建rmtp直播服务器并进行直播

简介和安装

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.串流秘钥不填写,作者不会使用权限控制的功能。想要进行权限设置,可以参考这里
OBS Streaming Setting

因为在配置文件中我们创建的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推流地址相同。
Potplayer

完成

到此为止,你应该已经成功的在本地搭建了一个简单的nginx直播服务器,并且能够正常观看。
十分感谢您的阅读,这是我第一次写博客,希望能够得到更多的指教,包括技术方面和写作方面的。

posted @ 2020-05-29 15:04  ZHider  阅读(585)  评论(0编辑  收藏  举报