搭建nginx流媒體服務器(支持HLS)


環境搭建

(一)下載源代碼

nginx,地址:http://nginx.org/可以選擇需要的版本下載

nginx_mod_h264_streaming-2.2.7.tar.gz ,支持MP4流,具體的說明在下面的這個網頁

http://h264.code-shop.com/trac/wiki/Mod-H264-Streaming-Nginx-Version2

m3u8-segmenter: HLS分片工具 ,下載地址,https://github.com/johnf/m3u8-segmenter

ffmpeg:媒體編解碼工具,這里做為HLS 直播流的發布工具

(二)安裝nginx

tar -zxvf nginx_mod_h264_streaming-2.2.7.tar.gz

tar -zxvf nginx-1.4.4.tar.gz

cd nginx-1.4.4

./configure  --prefix=/usr/local/nginx-stream --with-debug  --with-http_dav_module  --with-http_gzip_static_module  --with-http_ssl_module   --with-ipv6 --with-sha1=/usr/include/openssl  --with-md5=/usr/include/openssl  --add-module=../nginx_mod_h264_streaming-2.2.7  --with-http_flv_module  --with-http_mp4_module

如果沒有出現錯誤

make

如果出現錯誤類似:‘ngx_http_request_t’ 沒有名為 ‘zero_in_uri’ 的成員,則進入 nginx_mod_h264_streaming-2.2.7目錄,進入src,修改 ngx_http_streaming_module.c,注釋掉 TODO window32 模塊下的:

if (r->zero_in_uri) {

return NGX_DECLINED;

}

然后make clean之后重新configure和make

如果出現錯誤類似:[objs/addon/src/mp4_reader.o]..進入nginx源碼中的obis目錄,修改Makefile,刪除 --wrror

然后重新編譯make

編譯通過后

sudo make install

(三)安裝 m3u8-segmenter,這個在下載地址中有安裝步驟。

(四)安裝ffmpeg,在本博客其它日志中有安裝方式。

 

配置

 在server模塊下加入以下內容:

 location /hls {

alias /usr/local/media/hls;

types {

application/vnd.apple.mpegurl m3u8;

video/mp2t ts;

}

add_header Cache-Control no-cache;

expires -1;

}

location ~* \.flv$ {

flv;

root /usr/local/media/flv;

}

location ~* \.mp4$ {

mp4;

root /usr/local/media/mp4;

}

然后保存退出,啟動nginx服務器

 

點播flv,mp4視頻

在FLV和MP4的根目錄(usr/local/media/flv,/usr/local/media/mp4)分別放入測試視頻test.flv和test.mp4

使用ffmpeg中的播放器ffplay測試,

ffplay http://ip:port/test.flv

ffplay http://ip:port/test.mp4

 

HLS 點播 

使用m3u8-segmenter把視頻切成一系列TS文件同時生成后綴為m3u8的播放列表,視頻編碼需為H264/AAC 或者H264/MP3。

進入  /usr/local/media/hls,放入測試文件test.ts,然后使用以下命令分割,

m3u8-segmenter -i testvod.ts -d 10 -p test -m testvod.m3u8 -u http://ip:port/hls/

 -i ,輸入文件

-d ,每個分片的時長

-p ,每個分片的名稱的前綴

-m ,播放列表名稱

-u ,播放列表中url前綴

使用ffplay測試:

ffplay http://ip:port/hls/test.m3u8

 

HLS直播

使用ffmpeg發布直播流,這里沒有用設備抓取視頻,使用ffmpe 的-re選項來模擬直播流,re表示依照輸入視頻的幀率

ffmpeg -re -i test.ts -codec copy -hls_time 10 testlive.m3u8

使用ffplay測試

ffplay http://ip:port/hls/testlive.m3u8

 

注:mp4轉ts ,ffmpeg -i test.mp4 -codec copy -vbsf h264_mp4toannexb test.ts

        hls協議支持自適應碼率,可以使用播放列表的嵌套,nginx-rtmp-module對hls有類似的一些支持


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM