搭建基于nginx的rtmp流媒体服务器(windows32)

时间:2021-02-07
本文章向大家介绍搭建基于nginx的rtmp流媒体服务器(windows32),主要包括搭建基于nginx的rtmp流媒体服务器(windows32)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

主要包括三部分:推流端,拉流端,服务器端。

一、nginx服务搭建

1.下载nginx开源包和插件

注意里面需要有stat.xsl文件,这个文件是下面这个服务器使用监听情况的主要文件。

  • 还需要下载msvcr100.dll这个文件,因为在运行的时候会出现提示缺少这个文件无法运行的提示。只要下载好这个文件,x64版本的放到C:\WINDOWS\SYSTEM32下,x86版本放到C:\WINDOWS\SYSWOW64下,接着重启系统。

2. nginx配置

  • nginx-1.7.11.3-Gryphon\conf下;
  • 复制nginx-win.conf文件,修改名称为nginx.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;
    #    }
    #}
}

3. nginx启动

3.1 常用nginx的命令

(1)启动

start nginx 或 nginx.exe(使用这个会把cmd给卡死,而没办法输入其他命令)

注:建议使用第一种,第二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作。

(2)停止

nginx.exe -s stop

stop是快速停止nginx,可能并不保存相关信息;

nginx.exe -s quit

quit是完整有序的停止nginx,并保存相关信息。

(3)重新载入Nginx

nginx.exe -s reload

当配置信息修改,需要重新载入这些配置时使用此命令。

(4)重新打开日志文件

nginx.exe -s reopen

(5)查看Nginx版本

nginx -v

(6)验证配置文件是否正确

nginx -t

遇到问题处理

1. nginx端口占用,启动报错:bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a ........)

解决办法1

1、 按键盘win+r 打开运行界面,输入cmd,确定,打开管理员界

2 、输入netstat -aon | findstr :80

查找端口占用情况,找到提示占用的端口号0.0.0.0:80,我的是Apache2.4(之前配置php开发环境时用的)占用了,查看后,pid值为1884

3 、 输入tasklist|findstr "1884"

找到1884端口对应的服务名称,为httpd.exe,这个是apache2.4。如果不知道是哪个文件的,可以百度一下找到对应的服务名称。

4 、 然后再计算机的服务里停止对应的服务,解除占用

5、最后:将服务停止之后Nginx就能正常启动了,如果别的端口占用问题也可用此办法进行解决。简单写写,当做自己的笔记,也希望能帮到别人。

解决办法2

前言

之前在文章windows下nginx的安装、使用及80端口被占用导致nginx不能启动问题的解决中提到nginx默认使用80端口,而Windows下80端口被系统进程占用的解决办法。但有时候不想让nginx使用80端口,如何修改呢?

修改方法

很简单,修改nginx的配置文件,比如版本为nginx-1.11.10的则修改:

nginx-1.11.10\conf\nginx.conf

修改

http -> server -> listen

对应的值,如图:

将其改为别的端口号,就可以了。

2.nginx在windows设置了全局路径后,启动失败的解决方法

在 Windows 下载安装了 Nginx,配置了环境变量全局使用 Nginx。但是 Nginx 在使用时 conf-path 是根据相对路径来找的(可以根据 nginx -V 命令看出来)。这样的话,你进入 cmd 后,要想启动 Nginx(或者其他控制 Nginx 的命令选项),就必须切换到 Nginx 所在目录,或者在启动时指定 conf-path 的绝对路径,亦或是重新编译 Nginx 来指定 conf-path(Linux 下挺方便,Win也可以),不然 cmd 会报错而无法启动 Nginx。这样使用起来并不舒服,毕竟你要打一大串路径字符:( 。由于不想重编译,于是想了另一种方式——使用 bat 文件

注意点:
- 你需要先配置环境变量来全局使用 nginx 这个命令
- nginx 的命令选项中,除了需要用到配置文件的 start stop reload 等控制命令在非安装路径下使用时会报错外(前言讲到了),其他都可直接使用。比如 nginx -v 查看版本

解决方案

创建一个 bat 文件(我的是 nginxd.bat),使用 bat 来运行 nginx 命令。创建了之后,就可以使用如下命令:

nginxd [-h,help] [-v,version] [start] [stop] [stop -a] [reload] [reopen] [find]1

具体使用 nginxd -h 查看,当然 nginxd 命令根据 bat 文件名来定的。文件位置随意,但是要能全局使用(即指定环境变量)。代码如下:

@echo off
if "%1"=="help" (goto help) else (if "%1"=="-h" goto help)
if "%1"=="version" (goto version) else (if "%1"=="-v" goto version)
if "%1"=="start" goto start
if "%1"=="stop" goto stop
if "%1"=="reload" goto reloadmd
if "%1"=="reopen" goto reopen
if "%1"=="find" goto find
goto error

:help
nginx -v
echo Usage: nginxd [-h,help] [-v,version] [start] [stop] [stop -a] [reload] [reopen] [find]
echo=
echo Options:
echo   help,-h         : this help
echo   version,-v      : show current nginx version
echo   start           : start nginx master process
echo   stop            : stop the newest nginx master process
echo   stop -a         : stop all nginx master processes
echo   reload          : reload configuration
echo   reopen          : reopen nginx
echo   find            : show the nginx master process list
echo=
exit /B

:version
nginx -v
exit /B

:start
start nginx -p %~dp0
exit /B

:stop
if "%2"=="-a" (taskkill /F /IM nginx.exe) else (if "%2"=="" (nginx -s stop -p %~dp0) else goto error)
exit /B

:reload
nginx -s reload -p %~dp0
exit /B

:find
tasklist /fi "imagename eq nginx.exe"
exit /B

:error
echo nginxd: invalid option: "%1 %2"
echo=   
exit /B
简单解释

这是给小白看的,当然还有更好的实现,我也是刚接触,不足之处还请谅解。另外,以下是我自己的理解,可能有出入,请见谅

  1. @echo off:此命令分 @ 和 echo off 看。@让跟在其后面的命令的执行过程不打印出来,echo off 则让所有命令的执行过程不打印出来(自己测试,看结果就明白)。
  2. goto 与 :: 两个命令配套使用,: 相当于标签,goto 则指定跳到那个标签后面执行。
  3. echo:向命令窗口打印一行指定的字符创,echo= 打印空的字符串,结果相当于换行。
  4. exit: 该命令是退出程序,并且会关闭命令窗口(这不是我想要的)。指定 /B 则退出程序后不会关闭命窗口。/B 是说退回前一个命令所在窗口。
  5. %~dp0 :代表批处理文件所在当前路径。
注意

该批处理文件应放到nginx主路径下。

3.效果不是很好,有点卡,延时比较厉害,改nginx配置试试!

Nginx直播配置:

worker_processes  1;#这个根据CPU核心配
events {
    worker_connections  1024;
}
##################RTMP服务#################
rtmp {
   server { 
     listen 1935;
      chunk_size 4096;
     application video {
           play /usr/local/data/video;
        }
	application live{ #第一处添加的直播字段
	   live on;
	}
    }
}

####启动浏览器查看http://localhost:80/stat
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
    location /stat {     
            #第二处添加的location字段。
            rtmp_stat all;
            rtmp_stat_stylesheet stat.xsl;
    }

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

        location / {
            root   html;
            index  index.html index.htm;
       }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Nginx直播回放配置:

worker_processes  1;
events {
    worker_connections  1024;
}
##################RTMP服务#################
rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application video {
            play /usr/local/data/video;
        }
        application live {
		live on;#直播模式
		hls on; #这个参数把直播服务器改造成实时回放服务器。
		wait_key on; #对视频切片进行保护,这样就不会产生马赛克了。
		hls_path /usr/local/data/hls; #切片视频文件存放位置。
		hls_fragment 10s;     #每个视频切片的时长。
		hls_playlist_length 60s;  #总共可以回看的事件,这里设置的是1分钟。
		hls_continuous on; #连续模式。
		hls_cleanup on;    #对多余的切片进行删除。
		hls_nested on;     #嵌套模式。
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
            listen       80;
            server_name  localhost;
	    location /stat {
		    rtmp_stat all;
		rtmp_stat_stylesheet stat.xsl;
	    }

	    location /stat.xsl {
		root /usr/local/nginx/nginx-rtmp-module/;
	    }
        
	    location /live {  #这里也是需要添加的字段。
		types {  
		    application/vnd.apple.mpegurl m3u8;  
		    video/mp2t ts;  
		}
                        
		alias /opt/video/hls;   
		expires -1;
		add_header Cache-Control no-cache;  
	    }  

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

	   error_page   500 502 503 504  /50x.html;
	   location = /50x.html {
		root   html;
	   }
    }
}

二、推流端

1.使用OBS推流 (下载地址:https://obsproject.com/

2.使用第三方推流SDK,这个是百度云的一个SDK,无需收费,是一个Eclipse项目,不过AS也是支持的,导入便可(https://cloud.baidu.com/doc/Downloadcenter/Push.html#.E7.89.88.E6.9C.AC.E6.9B.B4.E6.96.B0.E8.AF.B4.E6.98.8E

3.使用开源项目推流(https://github.com/begeekmyfriend/yasea)导入后,修改下rtmp链接。

4.利用ffmpeg推流,这个很火对直播而言(https://github.com/WritingMinds/ffmpeg-android-java

以上四种方法,都可以用来进行推流。其中最简单的是第一种。

就拿第一种说事,毕竟简单嘛!

设置步骤:

1、添加场景

2、添加来源:显示器获取 、音频获取等等

3、设置RTMP的服务器路径,并设置流名称

自己测试用的是rtmp://192.168.50.33:1935/live,串流密钥:test

4、开始录制、开始推流

5、验证是否直播成功,打开浏览器http://192.168.50.33:8080/stat,看到如下界面表示直播正常。我的配置中监听的是8080

三、拉流端

VLC下载地址: http://www.videolan.org/vlc/

VLC设置:rtmp://192.168.50.33:1935/live/test

原文地址:https://www.cnblogs.com/mvwork/p/14384538.html