In building windows, arranged nginx streaming media server, and push rtmp stream flow, the flow pull test

    Before blogger Bowen " the Ubuntu installation, compiled, and run nginx-RTMP-Module1 nginx " describes a method in building nginx streaming media server at linux (Ubuntu). The following describes a method nginx streaming media server set up under the windows, it supports live rtmp protocol and configuration support through the live status monitoring.

 

A, nginx installation and configuration

    First we download nginx. In the official nginx nginx is downloaded from the Internet without rtmp module, so we http://nginx-win.ecsds.eu/download/ download nginx 1.7.11.3 Gryphon.zip, as shown in the following figure. This version of nginx contains rtmp components, through rtmp components, in order to provide streaming media services to enable nginx become rtmp streaming media server.

 

After downloading we unpacked nginx 1.7.11.3 Gryphon.zip, get nginx 1.7.11.3 Gryphon folder. Use the command prompt to enter the path after decompression, enter the command

nginx.exe -v

Action of the command is to look nginx version, after the input to the command information shown in FIG showing successful installation nginx

 

We create three directories under the directory nginx 1.7.11.3 Gryphon: m3u8File, rec, vod, as shown below:

 

Then we enter the conf directory nginx 1.7.11.3 Gryphon folder, create a new file "nginx.conf", as shown below:

 

Nginx.conf open, enter the following information is stored.


worker_processes  1;   #Nginx进程数,建议设置为等于CPU总核数

events {
    worker_connections  1024;  #工作模式与连接数上限
}

rtmp_auto_push on;


#RTMP服务
rtmp{
    server{
	    listen 1935;        #服务端口
		chunk_size 4096;    #数据传输块的大小
		
		application vod{
		    play ./vod;   #视频文件存放位置
		}
		application live{
		    live on;                     #开启直播
			hls on;                      #开启hls直播。这个参数把直播服务器改造成实时回放服务器
			#wait_key on;                #对视频切片进行保护,这样就不会产生马赛克了
			hls_path ./m3u8File;         #切片视频文件存放位置(HLS,m3u8文件存放位置)
			hls_fragment 2s;             #每个视频切片的时长
			hls_playlist_length 16s;
			recorder myRecord{
			    record all manual;
				record_suffix _.flv;
				record_path ./rec;
			}
			#hls_continuous on;          #连续模式
			#hls_cleanup on;             #对多余的切片进行删除
			#hls_nested on;              #嵌套模式
		}
	}
}


#HTTP服务
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

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

        location /live_hls{
		    types{
			    #m3u8 type设置
				application/vnd.apple.mpegurl m3u8;
				#ts分片文件设置
				video/mp2t ts;
			}
			#指向访问m3u8文件目录
			alias ./m3u8File;
			    add_header Cache-Control no-cache; #禁止缓存
		}

        location /control{
		    rtmp_control all;
		}
		
		location /stat{
		    rtmp_stat all;
			rtmp_stat_stylesheet stat.xsl;
		}
		location /stat.xsl{
		    root ./nginx-rtmp-module-master;
		}

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

 

Then we enter the command in the directory where nginx.exe command prompt:

nginx.exe -t

The role of the command is to check whether the correct nginx configuration file, when the output of the following information represents the profile nginx.conf correctly.

 

当通过上面的步骤验证了nginx配置文件的正确性后,我们就可以启动nginx了。通过命令提示符在nginx.exe所在的目录下输入命令:

start nginx

上述命令的作用是载入缺省./conf/nginx.conf配置文件,启动nginx。输入完成后,在浏览器中输入nginx所在电脑的ip(该ip地址可以通过ipconfig命令查看,或者直接输入127.0.0.1localhost,也是可以的),得到下图所示页面,则表示nginx启动成功了。然后用户可以通过命令nginx.exe -s stop或者nginx.exe -s quit停止nginx。

 

启动nginx成功的另一个标志是在nginx 1.7.11.3 Gryphon文件夹的logs目录下生成nginx.pid文件,如下图所示:

 

该nginx.pid文件存放的是当前nginx主进程的ID号,打开该文件后可以看到里面存放着“3980”这个数字,这个数字就是本次启动nginx主进程的ID号(每次启动的数字都会不同的)

 

我们可以在任务管理器中查询到该进程,则表示nginx启动成功了。

 

 

二、推流测试:

  启动nginx后,我们就可以进行推流、拉流测试了。推流指的是把采集阶段封包好的内容传输到服务器的过程,主播端把本地采集的语音视频流推送到媒体服务器。就是将现场的视频信号传到网络的过程。下面我们使用ffmpeg将媒体文件video3.mp4推流到nginx中,然后通过vlc播放器播放出来。

在windows的命令提示符中执行命令:

ffmpeg -i video3.mp4 -f flv rtmp://127.0.0.1/live/test1

其中-i 表示输入流。这里的输入流是video3.mp4。-f 表示设定的输出格式。这里因为推流为rtmp流得使用flv格式,所以设成-f flv。因为是本机测试,所以推流的IP地址填127.0.0.1(也可以填用ipconfig命令查看到的nginx所在电脑的ip),推流地址为rtmp://127.0.0.1/live/test1。关于"live"目录,详情可见《 关于使用FFmpeg推流时,live目录的理解》。上述语句的意思是通过ffmpeg将媒体文件video3.mp4转换成rtmp流,推流到本机的nginx服务器中。

执行完上述命令,会出现如下界面,表示推流成功了。

 

 

三、拉流测试

  拉流指的是用户端从服务器拉取语音视频流到客户端播放。下面我们会通过vlc拉流nginx中的语音视频流,然后在vlc播放。执行完上述推流的步骤后,我们在windows中打开vlc,点击“打开网络串流”,如下图所示:

 

输入网络URL,如下图所示(因为是本机测试,所以IP地址填127.0.0.1,也可以填用ipconfig命令查看到的nginx所在电脑的ip))

 

可以看到在vlc中出现视频画面了,表示拉流成功了

 

四、进行直播状态监听

    我们在https://github.com/arut/nginx-rtmp-module/,下载nginx-rtmp-module-master.zip,解压后复制到目录:nginx 1.7.11.3 Gryphon下,如下图所示:

在nginx-rtmp-module-master目录下,我们可以看到有个文件stat.xsl。这正好对应于配置文件nginx.conf中的设置,如下图所示:

 

然后我们在浏览器中输入http://127.0.0.1/stat。如下图所示,在下面的页面中我们就可以进行直播状态监听了。

 

五、遇到的问题和注意事项

1.启动nginx报错,如下图所示。产生该问题的原因为nginx安装路径含有中文,选一个不含中文的路径安装即可解决这个问题。

 

2.使用vlc拉流播放视频会很卡,而且会很模糊。我们改变ffmpeg推流的命令,如下所示,再拉流播放,即可缓解这个问题。

ffmpeg -re -i video3.mp4 -vcodec h264 -acodec copy -f flv rtmp://127.0.0.1/live/test1

 

3.nginx服务器默认占用的端口为1935(rtmp服务端口)和80(http端口)(这两个端口可以从配置文件nginx.conf中更改)。启动nginx前请在命令提示符中用"netstat -ano"命令查看这两个端口是否已经被占用,保证这两个端口没有被其它程序占用

 

4.启动nginx前最好要关闭所在电脑的防火墙

 

 

六、其它相关文章的链接

    1.本文仅仅讲述使nginx支持rtmp协议的直播的方法。要想使其支持hls协议的直播和rtmp点播,请参考:《在windows下配置nginx流媒体服务器,使其支持hls协议的直播和rtmp点播

    2.本文讲述的是在windows下安装,配置nginx的例子。下面的两篇文章讲述的是在linux下安装、配置nginx的方法

Ubuntu下安装、编译、运行nginx和nginx-rtmp-module》,《使用FFmpeg将视频推流到nginx,通过vlc拉流播放(通过命令的方式)

发布了54 篇原创文章 · 获赞 55 · 访问量 12万+

Guess you like

Origin blog.csdn.net/u014552102/article/details/100906058