windows下使用Nginx搭建Rtmp流媒体服务器,实现视频直播功能

987 阅读3分钟

我报名参加金石计划1期挑战——瓜分10万奖池,这是我的第4篇文章,点击查看活动详情

一、环境介绍

操作系统:win10 64位系统

二、流媒体服务器介绍

流媒体服务器的主要功能是以流式协议(RTP/RTSP、MMS、RTMP等)将视频文件传输到客户端,供用户在线观看;也可从视频采集、压缩软件接收实时视频流,再以流式协议直播给客户端。

nginx服务器是开源的高性能的http服务器,反向代理服务器,在国内许多公司应用比较多。

rtmp是实时消息传输协议(real time messaging protocol),主要应用在流媒体传输,实时音视频通信的协议。

三、搭建流媒体服务器

3.1 下载Nginx

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。

Nginx 下载地址:nginx-win.ecsds.eu/download/

img

我这里在电脑D盘建立了一个Nginx文件夹,然后将下载的nginx 1.7.11.3 Gryphon.zip文件解压在Nginx文件夹下(我这里解压,选择的是解压到当前目录)。

img

3.2 配置nginx.conf文件

接下来在D:\Nginx\conf 目录下新建一个nginx.conf文件。

img

写入以下代码:

  worker_processes  1;   #Nginx进程数,建议设置为等于CPU总核数
  
 events {
     worker_connections  1024;  #工作模式与连接数上限
 }
  
 rtmp_auto_push on;
 ​
 #RTMP服务
 rtmp {  
     server {  
         listen 8888;   
         application live {  
             live on;  
             }
          }  
 }
 ​
 #HTTP服务
 http {
     include       mime.types;
     default_type  application/octet-stream;
     sendfile        on;
     keepalive_timeout  65;
  
     server {
         listen       8099;
         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;
         }
     }
 }

需要注意的地方:上面配置文件里,有两个地方设置了端口号,一个是HTTP(8099),一个是RTMP(8888),这两个端口号必须是系统当前没有使用的,如果被占用,下面3.4的步骤就会报错。

3.3 建立工作目录

在D:\Nginx 目录下新建三个目录:m3u8File、rec、vod 。

img

3.4 检查配置是否正确

打开windows命令行终端,进入到D:\Nginx 目录下,运行nginx.exe -t 。 出现以下结果,就表示成功。

 Microsoft Windows [版本 10.0.18363.778]
 (c) 2019 Microsoft Corporation。保留所有权利。
 ​
 D:\Nginx>nginx.exe -t
 nginx: the configuration file D:\Nginx/conf/nginx.conf syntax is ok
 nginx: configuration file D:\Nginx/conf/nginx.conf test is successful
 ​
 D:\Nginx>

img

3.5 启动Nginx服务器

在命令行,继续运行启动命令。

 start nginx

img

启动之后,用户可以通过命令nginx.exe -s stop或者nginx.exe -s quit停止nginx。

接下来,打开浏览器,输入地址:http://127.0.0.1:8099/ ,查看nginx启动状态,看到下面页面就表示启动成功了,。

img

四、推流、拉流测试

在Nginx的配置文件里,设置RTMP服务器的端口号是8888,下面使用编写的推流软件进行测试。

因为是在本地测试,我这里就使用本地IP地址:127.0.0.1

根据上面的配置说明,那么本机Nginx服务器的推流和拉流的地址就是:rtmp://127.0.0.1:8888/live/video01

上面地址里“live是在配置文件里设置的,“video0”是随意写,是服务器创建的临时目录。

img

接下来打开Vlc软件,测试下拉流效果。 拉流地址和推流地址是一样的:rtmp://127.0.0.1:8888/live/video01

img

img

至此:推流服务器搭建完毕。