使用Nginx+FFMPEG搭建HLS直播转码服务器
FROM:http://blog.csdn.net/wutong_login/article/details/42292787
目的:使Nginx支持Rtmp協議推流,并支持hls分發功能及FFMPEG轉碼多碼率功能。
一、準備工作
模塊:nginx-rtmp-module-master(支持rtmp協議)
下載地址:
http://nginx.org
https://github.com/arut/nginx-rtmp-module
1、安裝依賴包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64 gcc-c++
2、安裝git工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz?
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git --version
git version 1.8.1.GIT
#cd ..
【錯誤處理】
如果 git-latest.tar.gz大小為0,請下載git-latest-tar.xz
然后xz -d git-latest.tar.xz解壓為.tar
再tar xvf git-latest.tar
3、安裝ffmpeg及其依賴包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure --enable-shared?
#make
#make install
#cd ..
++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure --enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
#git clone http://git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure? --enable-shared
#make
#make install
#cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..
【錯誤處理】
編譯FAAC-1.28時遇到錯誤:
mpeg4ip.h:126: error: new declaration ‘char* strcasestr(const char*, const char*)’
解決方法:
從123行開始修改此文件mpeg4ip.h,到129行結束。
修改前:
#ifdef __cplusplus
extern "C" {
#endif
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif
修改后:
#ifdef __cplusplus
extern "C++" {
#endif
const char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif
++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ../../../
++++++++ffmpeg+++++++++++
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure? --prefix=/opt/ffmpeg/ --enable-version3? --enable-libvpx --enable-libfaac --enable-libmp3lame? --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree? --enable-avfilter --enable-pthreads
#make && make install
#cd ..
【錯誤處理】
如果提示libvpx decoder version must be >=0.91,請從Baidu搜索一下libvpx-v1.1.0.tar.bz下載。
bzip2 -d? libvpx-v1.1.0.tar.bz2
tar xvf? libvpx-v1.1.0.tar.bz2
cd libvpx-v1.1.0
./configure --enable-shared --enable-vp8
make
make install
修改/etc/ld.so.conf如下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
【說明】
動態裝入器找到共享庫要依靠兩個文件 — /etc/ld.so.conf 和 /etc/ld.so.cache。
安裝完成后,ffmpeg位于/opt/ffmpeg/bin目錄下。
二、安裝Nginx相關模塊
1.環境準備
yum install –y pcre pcre-devel
yum install –y zlib zlib-devel
2.下載nginx及rtmp模塊
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar xzvf nginx_1.6.2.tar.gz
git clone git://github.com/arut/nginx-rtmp-module.git
3.編譯nginx-rtmp
./configure --prefix=/usr/local/nginx --add-module=../nginx-rtmp-module --with-http_stub_status_module
make
make install
安裝完成后,nginx位于/usr/local/nginx/sbin目錄下,配置文件nginx.conf在/usr/local/nginx/conf目錄下
++++++++測試nginx是否安裝正確+++++++++++
#cd /usr/local/nginx
#./sbin/nginx -c ./conf/nginx.conf
打開網頁http://localhost,如果顯示Welcome表示安裝下正確,如果沒有顯示,請查看一下nginx的日志。
++++++++測試RTMP+++++++++++
修改/usr/local/nginx/conf/nginx.conf的內容如下:
#debug
daemon off;
master_process off;
error_log ./error.log debug;
events{
??? worker_connections 1024;
}
rtmp{
??? server {
??????? listen 1935;
??????? chunk_size 4000;
??????? #live
??????? application myapp {
??????????? live on;
????? }
}
從網上下載一款RTMP推流工具,我使用的OBS(Open Broadcaster Software),開始推流rtmp://your_ip/myapp/test,使用播放器(http://www.cutv.com/demo/live_test.swf)查看是否正常。
++++++++測試HLS切片功能+++++++++++
修改/usr/local/nginx/conf/nginx.conf的內容如下:
#debug
daemon off;
master_process off;
error_log ./error.log debug;
events{
??? worker_connections 1024;
}
rtmp{
??? server {
??????? listen 1935;
??????? chunk_size 4000;
??????? #live
??????? application myapp {
??????????? live on;
?
????? ????? hls on;
??????????? hls_path /tmp/hls;
??????????? hls_fragment 2s;
??????????? hls_playlist_length 6s;
?????? }
??? }
}
#HTTP
http{
??? server {
??????? listen 80;
??????? #welcome
??????? location / {
??????????? root?? html;
??????????? index? index.html index.htm;
??????? }
??????? #hls
??????? location /hls {
??????????? types {
??????????????? application/vnd.apple.mpegusr m3u8;
??????????????? video/mp2t ts;
??????????? }
??????????? root /tmp;
??????????? add_header Cache-Control no-cache;
??????? }??
??? }
}
使用VLC或iPAD上的播放器進行查看 http://yourip/hls/test.m3u8。
++++++++測試FFMPEG轉碼功能+++++++++++
修改/usr/local/nginx/conf/nginx.conf的內容如下:
#debug
daemon off;
master_process off;
error_log ./error.log debug;
events{
??? worker_connections 1024;
}
rtmp{
??? server {
??????? listen 1935;
??????? chunk_size 4000;
??????? #live
??????? application myapp {
??????????? live on;
?
?????????? exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
???????????? -c:a copy? -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;
??????? }
??????
??????? application hls {
??????????? live on;
??????????? hls on;
??????????? hls_path /tmp/hls;
??????????? hls_nested on;
??????????? hls_fragment 2s;
??????????? hls_playlist_length 6s;
?????????? hls_variant _hi? BANDWIDTH=640000;
??????? }
??? }
}
#HTTP
http{
??? server {
??????? listen 80;
??????? #welcome
??????? location / {
??????????? root?? html;
??????????? index? index.html index.htm;
??????? }
??????? #hls
??????? location /hls {
??????????? types {
??????????????? application/vnd.apple.mpegusr m3u8;
??????????????? video/mp2t ts;
??????????? }
??????????? root /tmp;
??????????? add_header Cache-Control no-cache;
??????? }??
??? }
}
使用ffmpeg轉碼時,
exec /opt/ffmpeg/bin/ffmpeg -i rtmp://localhost/myapp/$name
???????????? -c:a copy? -c:v libx264 -b:v 512K -g 30 -f flv rtmp://localhost/hls/$name_low;
僅對視頻進行轉碼,音頻不做處理,同時向流從myapp轉推到hls, hls_variant會生成一個多碼率的m3u8文件,同時把切片文件存放到test_low目錄下,訪問多碼率時,直接訪問http://yourip/hls/test.m3u8,根據這個m3u8中的實現的內容訪問相應的碼流,在本例中,實際碼流URL為http://yourip/hls/test_low/index.m3u8
總結
以上是生活随笔為你收集整理的使用Nginx+FFMPEG搭建HLS直播转码服务器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Rtsp]RTSP对实时摄像头视频流进
- 下一篇: 让FFMPEG支持实时流“伴随”转码