使用nginx与nginx-rtmp-module搭建流媒体服务器

现在,一起学习一下如何自己搭建一个流媒体服务器吧!

本次搭建流媒体使用的环境是centos 7.0+nginx;(如果对于防火墙关闭的问题请观看我的Lamp环境搭建里头有)


让我们一起开始奇妙的流媒体之旅吧!


1、下载nginx-rtmp-module:

nginx-rtmp-module的官方github地址:https://github.com/arut/nginx-rtmp-module

使用命令:

[php]  view plain  copy
  1. git clone https://github.com/arut/nginx-rtmp-module.git  

将nginx-rtmp-module下载到linux中。

如果在linux中,软件下载失败,直接在windows下载完毕后,上传到linux中效果是一样的,解压后命名为nginx-rtmp-module


2、安装nginx:

nginx的官方网站为:http://nginx.org/en/download.html

安装时候可能会报错没有安装openssl,需要执行命令:

[php]  view plain  copy
  1. yum -y install openssl openssl-devel
[php]  view plain  copy
  1. wget http://nginx.org/download/nginx-x.x.x.tar.gz  
  2. tar -zxvf nginx-x.x.x.tar.gz  
  3. cd nginx-x.x.x  
  4. ./configure --prefix=/usr/local/nginx  --add-module=../nginx-rtmp-module  --with-http_ssl_module    
  5. make && make install  



3、修改nginx配置文件:

hls_path需要可读可写的权限,因为/usr/share/nginx/html/hls在目录中还没有,所以使用以下命令创建目录

mkdir -p /usr/share/nginx/html/hls
[php]  view plain  copy
  1. vi /usr/local/nginx/conf/nginx.conf  

加入以下内容rtmp模块:( rtmp{}的内容和http{}为同级,位置不要放错 )

[php]  view plain  copy
  1. rtmp {    
  2.     
  3.     server {    
  4.     
  5.         listen 1935;  #监听的端口  
  6.     
  7.         chunk_size 4000;    
  8.           
  9.            
  10.         application hls {  #rtmp推流请求路径  
  11.             live on;    
  12.             hls on;    
  13.             hls_path /usr/share/nginx/html/hls;    
  14.             hls_fragment 5s;    
  15.         }    
  16.     }    
  17. }  

修改http中的server模块( 重点修改红色字体 ):

[php]  view plain  copy
  1. server {  
  2.     listen       81;  
  3.     server_name  localhost;  
  4.   
  5.     #charset koi8-r;  
  6.   
  7.     #access_log  logs/host.access.log  main;  
  8.   
  9.     location / {  
  10.         add_header Cache-Control no-cache;
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
            add_header 'Access-Control-Allow-Headers' 'Range';
  11.         root   /usr/share/nginx/html;  
  12.         index  index.html index.htm;  
  13.     }  
  14.   
  15.     #error_page  404              /404.html;  
  16.   
  17.     # redirect server error pages to the static page /50x.html  
  18.     #  
  19.     error_page   500 502 503 504  /50x.html;  
  20.     location = /50x.html {  
  21.         root   html;  
  22.     }  

然后启动nginx:

[php]  view plain  copy
  1. /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf    

访问http://x.x.x.x:81。出现403,说明安装成功。

重启nginx的命令为:

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -s reload


关于更多rtmp的参数可以参考:https://github.com/arut/nginx-rtmp-module/wiki


4、开始推流

做好以上的配置后,就可以开始推流了,我们可以使用obs来推流。

在设置->串流 中填写信息:URL为 rtmp://xxx:1935/hls,xxx为你的服务器的IP地址,hls是用来存放流媒体的。

秘钥可以随便填写一个,用来播放的时候识别播放哪个流媒体的,例如填写mystream。

填写完毕后,点击开始串流,就说明我们的流媒体服务器搭建成功了。

打开/usr/share/nginx/html/hls,里面出现mystream.m3u8说明推流正常了


5、观看直播(拉流)

观看直播就比较简单了,可以简单的使用h5的vedio标签就可以观看了。

可以访问http://xxx:81/hls/mystream.m3u8来观看直播,其中xxx为你的服务器IP地址,

因为pc不支持m3u8,所以使用video.js   地址 https://github.com/videojs/video.js

新建一个test.html文件,代码如下:

代码如下:

  1. <link href="//vjs.zencdn.net/7.0/video-js.min.css" rel="stylesheet">
  2. <script src="//vjs.zencdn.net/7.0/video.min.js"> </script>
  3. <script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"> </script>
  4. <video
  5. id= "my-player"
  6. class= "video-js"
  7. controls
  8. preload= "auto"
  9. poster= "//vjs.zencdn.net/v/oceans.png"
  10. data-setup= '{}'>
  11. <source
  12. src= "http://x.x.x.x:81/hls/123.m3u8"
  13. type= "application/x-mpegURL">
  14. <p class="vjs-no-js">
  15. To view this video please enable JavaScript, and consider upgrading to a
  16. web browser that
  17. <a href="http://videojs.com/html5-video-support/" target="_blank">
  18. supports HTML5 video
  19. </a>
  20. </p>
  21. </video>
  22. <script>
  23. var player = videojs( 'my-player');
  24. var options = {};
  25. var player = videojs( 'my-player', options, function onPlayerReady() {
  26. videojs.log( 'Your player is ready!');
  27. // In this context, `this` is the player that was created by Video.js.
  28. this.play();
  29. // How about an event listener?
  30. this.on( 'ended', function() {
  31. videojs.log( 'Awww...over so soon?!');
  32. });
  33. });
  34. </script>

然后使用手机访问这个网站就能够观看直播了。延迟大概在20S左右。


写在最后

为什么延迟 那么高呢?这是因为服务器将视频流切断成一个个小的以.ts结尾的文件。

(hls文件夹内容。即推流到该文件夹了)

而我们访问的是.m3u8文件,这个文件内容是将一个个ts文件串联起来的,这就达到了一个播放的效果,所以看起来会有很大的延迟。

(m3u8文件内容)

如果降低延迟也不是没有方法,可以设置切片生成的大小以及访问的速度,但是这样大大增加了服务器的压力。

当然,我们也可以用rtmp拉流工具(VLC等)来看该直播,延迟大概在2-5S左右,拉流地址与推流地址一致。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值