【史上最全】Nginx+ffmpeg實現流媒體系統

天涯丿過客發表於2017-05-06
#centos6.6安裝搭建nginx+ffmpeg流媒體伺服器
#此係統實現了視訊檔案的直播及快取點播,並支援移動端播放(支援Apple和Android端)
#系統需要自行安裝,流媒體伺服器配置完成之後桌面可能掛掉,以後維護需要進命令列進行操作
#必須按照以下步驟配置流媒體伺服器,每一步必須要完成才可以配置成功
# "#"是文件註釋內容 "--"也是註釋內容
# 1.首先設定伺服器IP地址,並更改為自動連線
# 2.修改系統時間和時區為當前時間
# 3.關閉防火牆
#在控制檯進行如下操作(需要登入root賬號): 安裝所有的依賴包:
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

#在usr/local/目錄下面新建nginx資料夾
#開啟命令視窗
#輸入:
mkdir soft-source
cd soft-source

#安裝git
wget http://codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.xz
xz -d git-latest.tar.xz
tar xf git-latest.tar 
#cd進入的是當前安裝的日期
cd git-2014-12-29
autoconf
 ./configure
make
make install
git --version      --檢視git的版本
cd ..

#安裝zlib
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
cd ..

#安裝pcre
wget http://exim.mirror.fr/pcre/pcre-8.12.tar.gz
tar zxvf pcre-8.12.tar.gz
cd pcre-8.12
./configure
make
#	make的時候如果出現錯誤:
#	libtool: compile: unrecognized option `-DHAVE_CONFIG_H' 
#	libtool: compile: Try `libtool --help' for more information. 
#	make[1]: *** [pcrecpp.lo] Error 1 
#	make[1]: Leaving directory `/home/mfcai/pcre-8.10' 
#	make: *** [all] Error 2 
#	make install
執行:yum install gcc gcc-c++ kernel-devel
然後:./configure
make
make install
cd ..

#安裝yadmi給flv視訊新增關鍵幀
wget http://sourceforge.net/projects/yamdi/files/yamdi/1.4/yamdi-1.4.tar.gz
tar xzvf yamdi-1.4.tar.gz
cd yamdi-1.4
make
make install
cd ..

#安裝OpenSSL
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
cd openssl-1.0.1c
./config
make
make install
cd ..

#安裝ffmpeg及其依賴包:
yum install autoconf automake gcc gcc-c++ git libtool make nasm pkgconfig zlib-devel

安裝Yasm
cd /usr/local/nginx
mkdir ffmpeg_sources
cd ffmpeg_sources
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar xzvf yasm-1.3.0.tar.gz
cd yasm-1.3.0
 ./configure
make
make install
cd ..

#安裝ffmpeg
cd /ffmpeg_sources
git clone --depth 1 git://source.ffmpeg.org/ffmpeg
cd ffmpeg
PKG_CONFIG_PATH="$HOME/ffmpeg_build/lib/pkgconfig"
 ./configure
make install


#ffmpeg正常安裝後執行ffmpeg時出現如下錯誤:

#  ffmpeg: error while loading shared libraries: libavdevice.so.53: cannot open shared object file: No such file or directory

#解決辦法:

vi /etc/ld.so.conf

#加入:
/usr/local/lib

#執行
ldconfig


#安裝nginx模組
cd /usr/local/nginx/soft-source
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
git clone git://github.com/arut/nginx-rtmp-module.git

#nginx安裝
wget http://nginx.org/download/nginx-1.6.0.tar.gz
tar zxvf nginx-1.6.0.tar.gz
cd nginx-1.6.0

#編譯安裝nginx(直接複製nginx_configure.sh至nginx-1.6.0目錄,檔案在附件中) 
cp nginx_configure.sh /usr/local/nginx/soft-source/nginx-1.6.0
chmod +x nginx_configure.sh
./nginx_configure.sh
make 

#在執行make的時候可能出現錯誤
#	/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c: In function ‘ngx_streaming_handler’:
#	/root/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c:158: error: ‘ngx_http_request_t’ has no member named ‘zero_in_uri’
#	make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1
#	make[1]: Leaving directory `/root/nginx-0.8.54'
#	make: *** [build] Error 2

#解決方法:

#將/usr/local/nginx/soft-source/nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c檔案中以下程式碼刪除或者是註釋掉就可以了:大概在157行
#	/* TODO: Win32 */
#	if (r->zero_in_uri)
#	{
#	return NGX_DECLINED;
#	}
#

#在執行make的時候可能出現錯誤
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函式‘esds_read’中:
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:377:16: 錯誤: 變數‘stream_priority’被設定但未被使用 [-Werror=unused-but-set-variable]
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:376:12: 錯誤: 變數‘stream_id’被設定但未被使用 [-Werror=unused-but-set-variable]
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c: 在函式‘stsd_parse_vide’中:
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:529:22: 錯誤: 變數‘level_indication’被設定但未被使用 [-Werror=unused-but-set-variable]
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:528:22: 錯誤: 變數‘profile_compatibility’被設定但未被使用 [-Werror=unused-but-set-variable]
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:527:22: 錯誤: 變數‘profile_indication’被設定但未被使用 [-Werror=unused-but-set-variable]
#	../nginx_mod_h264_streaming-2.2.7/src/mp4_reader.c:526:22: 錯誤: 變數‘configuration_version’被設定但未被使用 [-Werror=unused-but-set-variable]
#	cc1: all warnings being treated as errors
#	make[1]: *** [objs/addon/src/mp4_reader.o] 錯誤 1

#解決方法:
# vi objs/Makefile (修改nginx-1.6.0/objs/Makefile檔案, 去掉其中的"-Werror"), 然後就能夠正常編譯了.
# 然後再make就能過了。
vi objs/Makefile
make install

#開放80和1935埠 關閉防火牆
/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT   寫入修改
/sbin/iptables -I INPUT -p tcp --dport 1935 -j ACCEPT   寫入修改 
/etc/init.d/iptables save   儲存修改
#關閉防火牆
service iptables stop
#檢視埠狀態
 /etc/init.d/iptables status
埠對映
對映流媒體伺服器的 80  1935  22 埠到外網
#配置Nginx相關模組
 進入到nginx資料夾!執行./nginx啟動nginx


然後進行推送到nginx
rtmp://x.x.x.x/hls/ch68h
播放
rtmp://x.x.x.x.:1935/hls/ch68h
http://x.x.x.x/hls/ch68h.m3u8

以上內容為流媒體伺服器的基本配置說明,根據專案不同可配置不同的配置檔案,因Nginx配置檔案為核心檔案,所以暫不公開,如有需要的可與我溝通聯絡,此係統已經完善部署到幾十個專案中,執行狀況良好,同時也有少許bug,並在不斷更新中.


相關文章