1. 程式人生 > >使用nginx與nginx-rtmp-module搭建流媒體伺服器

使用nginx與nginx-rtmp-module搭建流媒體伺服器

現在,一起學習一下如何自己搭建一個流媒體伺服器吧!

本次搭建流媒體使用的環境是centos 7.0+nginx;(如果對於防火牆關閉的問題請觀看我的Lamp環境搭建裡頭有)

讓我們一起開始奇妙的流媒體之旅吧!

1、下載nginx-rtmp-module:

使用命令:

  1. git clone https://github.com/arut/nginx-rtmp-module.git

將nginx-rtmp-module下載到linux中。

如果在linux中,軟體下載失敗,直接在windows下載完畢後,上傳到linux中效果是一樣的,解壓後命名為nginx-rtmp-module

2、安裝nginx:

安裝時候可能會報錯沒有安裝openssl,需要執行命令:

  1. yum -y install openssl openssl-devel
  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
  1. vi /usr/local/nginx/conf/nginx.conf  
加入以下內容rtmp模組:(rtmp{}的內容和http{}為同級,位置不要放錯)
  1. rtmp {    
  2.     server {    
  3.         listen 1935;  #監聽的埠  
  4.         chunk_size 4000;    
  5.         application hls {  #rtmp推流請求路徑  
  6.             live on;    
  7.             hls on;    
  8.             hls_path /usr/share/nginx/html/hls;    
  9.             hls_fragment 5s;    
  10.         }    
  11.     }    
  12. }  

修改http中的server模組(重點修改紅色字型):
  1. server {  
  2.     listen       81;  
  3.     server_name  localhost;  
  4.     #charset koi8-r;  
  5.     #access_log  logs/host.access.log  main;  
  6. location / {  
  7.         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';
  8.         root   /usr/share/nginx/html;  
  9.         index  index.html index.htm;  
  10.     }  
  11.     #error_page  404              /404.html;  
  12.     # redirect server error pages to the static page /50x.html  
  13.     #  
  14.     error_page   500 502 503 504  /50x.html;  
  15.     location = /50x.html {  
  16.         root   html;  
  17.     }  

然後啟動nginx:
  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

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檔案,程式碼如下:

程式碼如下:

<link href="//vjs.zencdn.net/7.0/video-js.min.css" rel="stylesheet">
<script src="//vjs.zencdn.net/7.0/video.min.js"></script>
<script src="https://unpkg.com/videojs-contrib-hls/dist/videojs-contrib-hls.js"></script>
<video
    id="my-player"
    class="video-js"
    controls
    preload="auto"
    poster="//vjs.zencdn.net/v/oceans.png"
    data-setup='{}'>
	 <source
     src="http://x.x.x.x:81/hls/123.m3u8"
     type="application/x-mpegURL">
  <p class="vjs-no-js">
    To view this video please enable JavaScript, and consider upgrading to a
    web browser that
    <a href="http://videojs.com/html5-video-support/" target="_blank">
      supports HTML5 video
    </a>
  </p>
</video>
<script>
var player = videojs('my-player');
var options = {};

var player = videojs('my-player', options, function onPlayerReady() {
  videojs.log('Your player is ready!');

  // In this context, `this` is the player that was created by Video.js.
  this.play();

  // How about an event listener?
  this.on('ended', function() {
    videojs.log('Awww...over so soon?!');
  });
});
</script>

然後使用手機訪問這個網站就能夠觀看直播了。延遲大概在20S左右。

寫在最後

為什麼延遲 那麼高呢?這是因為伺服器將視訊流切斷成一個個小的以.ts結尾的檔案。

(hls資料夾內容。即推流到該檔案夾了)

而我們訪問的是.m3u8檔案,這個檔案內容是將一個個ts檔案串聯起來的,這就達到了一個播放的效果,所以看起來會有很大的延遲。

(m3u8檔案內容)

如果降低延遲也不是沒有方法,可以設定切片生成的大小以及訪問的速度,但是這樣大大增加了伺服器的壓力。

當然,我們也可以用rtmp拉流工具(VLC等)來看該直播,延遲大概在2-5S左右,拉流地址與推流地址一致。