使用Nginx-rtmp-module搭建hls直播
HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基于HTTP的流媒體網絡傳輸協議。是蘋果公司QuickTime X和iPhone軟件系統的一部分。它的工作原理是把整個流分成一個個小的基于HTTP的文件來下載,每次只下載一些。當媒體流正在播放時,客戶端可以選擇從許多不同的備用源中以不同的速率下載同樣的資源,允許流媒體會話適應不同的數據速率。在開始一個流媒體會話時,客戶端會下載一個包含元數據的extended M3U (m3u8) playlist文件,用于尋找可用的媒體流。
HLS只請求基本的HTTP報文,與實時傳輸協議(RTP)不同,HLS可以穿過任何允許HTTP數據通過的防火墻或者代理服務器。它也很容易使用內容分發網絡來傳輸媒體流。
此協議詳細內容請參考apple官方網站:https://developer.apple.com/resources/http-streaming/
hls的技術細節就不說了,這里搭建hls直播的目的就是想研究下Nginx-rtmp-module針對rtmp直播流實時轉換為hls直播流的基本細節。
經過測試,我發現,rtmp直播流會被動態切分為ts片段和一個不斷刷新的u3m8文件,我正需要關注這一點,必要時可能對相關代碼進行調試。這里按順序分幾個部分講述我的搭建步驟:軟件編譯,nginx配置,rtmp源的提供,html代碼修改,客戶端播放
1.軟件編譯
從下面的網址分別下載nginx和nginx-rtmp-module:
http://nginx.org/en/download.html
https://github.com/arut/nginx-rtmp-module
我目前的現狀是已經安裝好了nginx-1.4.4,需要在它的基礎上安裝這個模塊,可以使用下面的方法增加后續的第三方模塊。
進入nginx的源碼目錄下面
./configure --add-module=/path/to/nginx-rtmp-module --with-http_ssl_module --with-debug
來增加這個模塊,然后
make
make install
注意新生成的配置文件不會覆蓋原來的配置文件。參見下面的截圖
可見,在config時已經看到了這個新加入的模塊,下面的截圖說明,現在Nginx只針對新添加的模塊進行編譯
由上圖可見,在make install時,對原來已經存在的nginx.conf,只會進行原封不動的復制。這一點比較人性化,特別是在線上運維上,這樣我們可以任意增加后續模塊,然后基于前一次的nginx.conf進行修改就可以了,超贊。
如果是全新安裝,就更簡單了,這里略去安裝步驟。
下面是我的實戰記錄(注意我原來使用了google perftools模塊,所以以后添加的所有模塊需要都加上這個編譯)
cd /usr/local/src
git clone https://github.com/arut/nginx-rtmp-module.git
cd ../nginx-1.4.4
./configure --prefix=/usr/local/nginx --with-google_perftools_module --add-module=/usr/local/src/nginx-rtmp-module ?--with-http_ssl_module --with-debug
make
make install
2.nginx配置
Nginx可以支持多虛機配置,如果是一個ip或域名多虛機的情況,就是要不同的虛機對應不同的端口服務,而如果是多ip或域名一個虛機的情況,則又不一樣。這里的實際情況就是,80和8080分別對應一個http協議的虛機,1935對應一個rtmp協議的虛機。關于hls具體配置項的解釋參見
https://github.com/arut/nginx-rtmp-module/wiki/Directives
在原有的nginx.conf中加入如下配置
rtmp {server {listen 1935;chunk_size 4000;#HLS# For HLS to work please create a directory in tmpfs (/tmp/app here)# for the fragments. The directory contents is served via HTTP (see# http{} section in config)## Incoming stream must be in H264/AAC. For iPhones use baseline H264# profile (see ffmpeg example).# This example creates RTMP stream from movie ready for HLS:## ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264 # -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 # -f flv rtmp://localhost:1935/hls/movie## If you need to transcode live stream use 'exec' feature.#application hls {live on;hls on;hls_path /usr/local/nginx/html/hls;hls_fragment 5s;}}
}http {server {listen 8080;location /hls {# Serve HLS fragmentstypes {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root html;expires -1;}}
}其中rtmp部分與原有的http部分在同一個級別,但是下面的http部分要放到已有的http部分中,也就是增加一個server部分。
然后運行如下命令檢查nginx.conf是否有語法錯誤
service nginx configtest
重新加載配置文件
service nginx reload
運行下面的命令查看nginx狀態
service nginx status
然后查看端口
netstat -nlp
注意,這里reload并不能開啟這幾個服務,需要使用
service nginx restart
來將rtmp服務和hls服務的端口1935和8080都打開,參看下面的截圖
3.rtmp源提供
我目前采用ffmpeg來將本地文件處理來模擬產生一個rtmp直播流,現在使用下面的命令來產生一個rtmp直播流
ffmpeg -re -i sample.flv -vcodec copy -acodec copy -f flv rtmp://192.168.90.26/hls/mystream
注意,這里提供rtmp源的機器不一定和nginx在同一臺物理主機上,可以是網絡上的另一臺機器,但是要保證它能與nginx所在的主機建立tcp鏈接,
也就是nginx主機需要開啟rtmp服務的監聽端口,這里是1935,當然你也可以修改為其他的端口。
根據nginx.conf中的hls_path配置,這個命令會向192.168.90.26主機的/usr/local/nginx/html/hls下面寫入ts片段和m3u8文件,參見下面的截圖:
4.修改html代碼,保存為playhls.html, 這是一個HTML5+HLS視頻的例子
<!DOCTYPE html>
<html>
<head><meta http-equiv="content-type" content="text/html; charset=utf-8"><title>HLS Player</title>
</head>
<body>
<video poster="poster.png" height="720" width="1280" controls><source src="http://192.168.90.26:8080/hls/mystream.m3u8" type="application/vnd.apple.mpegurl" /><p class="warning">Your browser does not support HTML5 video.</p>
</video>
</body>
</html> 這個html文件存放在和m3u8文件相同的目錄下面:/usr/local/nginx/html/hls
5.客戶端播放
在支持html5的瀏覽器中,輸入如下網址
http://192.168.90.26:8080/hls/playhls.html
但是測試支持html5的瀏覽器比較糟糕,沒有一個可以正常播放的,在android手機上測試百度瀏覽器,畫面出來了,但是播放時出了問題,崩掉了。我使用ipad 4,打開Safari瀏覽器,
就可以正常播放這個直播的視頻了。參見下面的截圖。
在vlc中加入如下地址也可以播放
http://192.168.90.26:8080/hls/mystream.m3u8
參見下面的截圖,注意直播不能拖動!
6,狀態查看
要查看到狀態信息,需要在nginx.conf中加入stat模塊的相關配置信息,也就是加入下面的幾行信息,注意root的值是nginx-rtmp-module所在的目錄
location /stat { ?
rtmp_stat all; ?
rtmp_stat_stylesheet stat.xsl; ?
} ?
location /stat.xsl { ?
root /usr/local/src/nginx-rtmp-module/; ?
}
注意為了得到統計信息,還需要開啟下面的控制模塊
location /control { ?
rtmp_control all; ?
} ?
將它們添加到8080那臺http虛機上的配置如下:
rtmp {server {listen 1935;chunk_size 4000;#HLS# For HLS to work please create a directory in tmpfs (/tmp/app here)# for the fragments. The directory contents is served via HTTP (see# http{} section in config)## Incoming stream must be in H264/AAC. For iPhones use baseline H264# profile (see ffmpeg example).# This example creates RTMP stream from movie ready for HLS:## ffmpeg -loglevel verbose -re -i movie.avi -vcodec libx264 # -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1 # -f flv rtmp://localhost:1935/hls/movie## If you need to transcode live stream use 'exec' feature.#application hls {live on;hls on;hls_path /usr/local/nginx/html/hls;hls_fragment 5s;}}
}http {server {listen 8080;location /stat { rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { root /usr/local/src/nginx-rtmp-module/; } location /control { rtmp_control all; } location /hls {# Serve HLS fragmentstypes {application/vnd.apple.mpegurl m3u8;video/mp2t ts;}root html;expires -1;}}
}
然后,在瀏覽器中打開
http://192.168.90.26:8080/stat
如果一切正常應該能看到相關的統計信息了,參見下面的截圖
如果不添加控制模塊的配置,就不能看到統計數據了,參見截圖
參考文獻
[1].http://blog.csdn.net/kl222/article/details/12968815
[2].http://blog.csdn.net/cjsafty/article/details/9108587
[3].http://blog.csdn.net/cccallen/article/details/8440191
[4].http://www.rosoo.net/a/201111/15273.html
[5].http://blog.raphaelzhang.com/2013/04/video-streaming-and-ffmpeg-transcoding/
總結
以上是生活随笔為你收集整理的使用Nginx-rtmp-module搭建hls直播的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用git更新github上的开源项目
- 下一篇: FFMPEG转码常用命令研究