編譯m3u8-segmenter
從https://github.com/johnf/m3u8-segmenter下載下傳m3u8-segmenter,
執行:cd m3u8-segmenter
執行:gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat -lavcodec -lavutil -I/usr/local/ffmpeg/include -L/usr/local/ffmpeg/lib
執行:cp segmenter /usr/bin/
測試:segmenter -i test.ts -n 30 -p sample_test -m stream-test.m3u8 -u ./
i表示輸入檔案,n表示切割30個,p表示切割檔案的字首。m表示生成的m3u8檔案名,u表示這些切割後的檔案處于web server的哪個目錄下。
ffmpeg切片
切本地檔案指令:ffmpeg -i test.ts -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 10 output%03d.ts
切網絡位址指令:ffmpeg -i rtmp://192.168.42.128/myapp/test1 -c copy -map 0 -f segment -segment_list playlist.m3u8 -segment_time 10 output%03d.ts
fedora搭建nginx
安裝PCRE,ZLIB,OPENSSL庫,參見http://blog.csdn.net/evsqiezi/article/details/40146777。
nginx-rtmp-module
nginx-rtmp-module一款NGINX的流媒體插件,除了直播釋出音視訊流之外具備流媒體伺服器的常見功能
•比如推拉流媒體資源
•基于HTTP的FLV/MP4 VOD點播
•HLS (HTTP Live Streaming) M3U8的支援
•基于http的操作(釋出、播放、錄制)
•可以很好的協同現有的流媒體伺服器以及播放器一起工作
•線上調用ffmpeg對流媒體進行轉碼
•H264/AAC音視訊編碼格式的支援
•linux/BSD/MAC系統的支援
從https://github.com/arut/nginx-rtmp-module下載下傳nginx-rtmp-module,并解壓,
編譯nginx如下:
# ./configure --prefix=/usr/local/nginx --with-openssl=/home/zf/thirdpart/openssl-1.0.0t --with-zlib=/home/zhangfan/thirdpart/zlib-1.2.8 --add-module=/home/code/nginx-rtmp-module-master,
# make && make install
打開Nginx的配置檔案nginx.conf進行配置,
首先在裡面加入rtmp的配置
rtmp {
server {
listen 1935;
application myapp {
live on;
}
application hls {
live on;
hls on;
hls_path /tmp/hls; //此為linux下的絕對路徑。
hls_fragment 2s;
hls_playlist_length 6s;
}
}
}
然後,針對hls,還需要在http裡面增加一個location配置
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
這是一個最簡單,最基礎的配置, rtmp監聽1935端口,如果是hls的話用hls on開啟hls,并且為hls設定一個臨時檔案目錄hls_path /tmp/hls,其它更進階的配置可以參看nginx-rtmp-module的readme。
然後執行如下:
# cd /user/local/nginx/sbin
# ./nginx
到此,nginx已在背景運作,在用戶端打開浏覽器,輸入 http://IP(如 http://172.16.200.60),即可看到nginx的歡迎頁面。通過netstat -ltn指令可以看到增加了一個1935端口的監聽.80是nginx預設的http監聽端口。
測試RTMP點播
用ffmpeg向伺服器推流ffmpeg -re -i source.flv -vcodec copy -acodec copy -f flv -y rtmp://192.168.1.110:1935/myapp/test,
點播位址: rtmp://192.168.1.110:1935/myapp/test。
測試HLS點播
ffmpeg轉碼
ffmpeg -i hk.ts -vcodec h264 -s 640x480 -bf 0 -r 25 -g 25 -acodec aac -ar 44100 hk.mp4
用ffmpeg向伺服器推流
ffmpeg -re -i killer.mp4 -vcodec copy -acodec copy -f flv -y rtmp://192.168.1.110:1935/hls/test
ffmpeg -re -i rtmp://live.hkstv.hk.lxdns.com/live/hks -vcodec copy -acodec copy -f flv -y rtmp://10.80.3.17:1935/hls/test
點播位址: http://192.168.1.110/hls/test.m3u8。
常用指令
重新開機
./nginx -s reload
關閉
查詢nginx主程序号
ps -ef | grep nginx
從容停止 kill -QUIT 主程序号
快速停止 kill -TERM 主程序号
強制停止 kill -9 nginx