這里實(shí)現(xiàn)了簡(jiǎn)單Nginx+ffmpeg 推本地mp4視頻文件的功能,以后將會(huì)繼續(xù)更新
環(huán)境
系統(tǒng)環(huán)境:centos release 6.7 (Final)
需求
利用nginx和ffmpeg搭建流媒體服務(wù)器
利用nginx和ffmpeg搭建流媒體服務(wù)器(直播流),其他流后續(xù)會(huì)有所更新
關(guān)于用Nginx搭建flv,mp4,hls流媒體服務(wù)器的技術(shù)干貨!
模塊:nginx_mod_h264_streaming(支持h264編碼MP4格式的視頻)
模塊:http_flv_module (支持flv)
模塊:http_mp4_module (支持mp4)
模塊: nginx-rtmp-module (支持rtmp協(xié)議,也支持HLS)
步驟
(1)模塊下載和解壓
wget http://nginx.org/download/nginx-1.6.0.tar.gz
wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
wget http://sourceforge.net/projects/pcre/files/pcre/8.35/pcre-8.35.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget http://www.openssl.org/source/openssl-1.0.1g.tar.gz
wget -O nginx-rtmp-module.ziphttps://github.com/arut/nginx-rtmp-module/archive/master.zip
將所有模塊統(tǒng)一放置在/usr/local/nginx_sources目錄下,方便管理
安裝步驟:openssl-->zlib-->pcre
1.openssl
./config --prefix=/usr/local/openssl -->make -->make install
2. zlib
./configure --prefix=/usr/local/zlib-->make -->make install
3.pcre
./configure ==prefix=/usr/local/pcre -->make -->make install
(2)配置命令,會(huì)生成makefile文件
cd /usr/local/nginx-1.6.0
./configure
--prefix=/usr/local/nginx
--add-module=/usr/tmp/nginx_mod_h264_streaming-2.2.7
--add-module=/usr/tmp/nginx-rtmp-module-master
--with-http_flv_module
--with-http_mp4_module
--with-http_stub_status_module
--with-http_ssl_module
--with-pcre=/usr/tmp/pcre-8.35
--with-zlib=/usr/tmp/zlib-1.2.8
--with-debug
--with-openssl=/usr/tmp/openssl-1.0.1g
錯(cuò)誤提示:如果在configure過程中出現(xiàn)以下錯(cuò)誤:
/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
那么將src/ngx_http_streaming_module.c文件中以下代碼刪除或者是注釋掉就可以了:
/* TODO: Win32 */
if (r->zero_in_uri)
{
return NGX_DECLINED;
}
如果你沒有對(duì)這個(gè)文件做個(gè)更改,那么應(yīng)該在源碼的第157-161行。
cd /usr/local/nginx/sbin--> ./nginx ( 啟動(dòng)nginx) -->netstat -nltp(如果80端口啟動(dòng)就說明安裝成功)
-->在瀏覽器輸入http://172.19.197.244:80
(3)開始安裝ffmpeg,參考安裝文檔http://www.voidcn.com/blog/yangguangmeng/article/p-4881242.html(轉(zhuǎn)載)
安裝ffmpeg:(如果系統(tǒng)不支持上網(wǎng)功能,可以自行從網(wǎng)下先下載,再上傳到服務(wù)器)
下載FFmpeg和libx264的包
ffmpeg-3.2.4.tar.bz2 last_x264.tar.bz2
libx264需要yasm,所以先安裝yasm
zypper install yasm
然后安裝libx264
zypper install libx264-dev
解壓縮libx264
tar -xzvf last_x264.tar.bz2
安裝libx264
./configure --enable-shared --enable-pthread --enable-pic
make
make install
然后安裝ffmpeg,ffmpeg有許多依賴包,需要一個(gè)一個(gè)先安裝,可能安裝過程中還需要下載其他依賴包,這個(gè)安裝過程依照提示進(jìn)行下載安裝即可
1. libfaac
zypper install libfaac-dev
2. libmp3lame
zypper install libmp3lame-dev
3. libtheora
zypper install libtheora-dev
4. libvorbis
zypper install libvorbis-dev
5. libxvid
zypper install libxvidcore-dev
6. libxext
zypper install libxext-dev
7. libxfixes
zypper install libxfixes-dev
依賴包安裝完后,安裝ffmpeg
先解壓縮ffmpeg
tar -xzvf ffmpeg-3.2.4.tar.bz2
cd /usr/local/ffmpeg_sources #我把它統(tǒng)一放在/usr/local/ffmpeg_sources目錄
cd ffmpeg ffmpeg-3.2.4
./configure --prefix=/usr/local/ffmpeg --enable-gpl --enable-version3 --enable-nonfree --enable-postproc --enable-pthreads --enable-libmp3lame --enable-libtheora --enable-libx264 --enable-libxvid --enable-x11grab
make && make install
cd /usr/local/nginx/conf -->vi nginx.conf
http {
include mime.types;
default_type Application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /hls {
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /tmp;
add_header Cache-Control no-cache;
}
# proxy the php scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /usr/local/nginx-rtmp-module/; #在nginx-rtmp-module源碼根目錄
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
錯(cuò)誤提示:安裝ffmpeg出現(xiàn)錯(cuò)誤, cannot open shared object file
vi /etc/ld.so.conf
在尾部增加一行:/usr/local/lib
-->ldconfig
溫馨提示:如果編譯過程中出現(xiàn)一些錯(cuò)誤,學(xué)會(huì)看config.log,里面會(huì)有錯(cuò)誤提示及解決方法
5、配置好后判斷配置是否正確
/usr/local/nginx/sbin/nginx -t
-->重啟nginx /usr/local/nginx/sbin/nginx -s reload
-->查看是否已經(jīng)啟動(dòng)流媒體服務(wù)器的端口
netstat -anp | grep 1935
6、開始推流,我推的是直播流
-->cd /usr/local/ffmpeg_sources/ffmpeg-3.2.4
-->./ffmpeg -re -i "/usr/local/WEB.mp4" -vcodec libx264 -vprofile baseline -acodec aac -ar 44100 -strict -2 -ac 1 -f flv -s 1280x720 -pix_fmt yuv420p -q 10 rtmp://172.19.197.244:1935/myapp/test
-->make && make install
7. 搭建客戶端測(cè)試環(huán)境,本文測(cè)試播放器使用的是jwplayer
-->準(zhǔn)備網(wǎng)頁
-->下載jwplayer:http://www.longtailvideo.com/players/jw-flv-player/
-->下載后,解壓到:/usr/local/nginx/html/
-->建立測(cè)試頁面test.html,也放到上面目錄下。
<html>
<head>
<script src="jwplayer/jwplayer.js"></script>
</head>
<body>
<div id='my-video'></div>
<script type='text/JAVAscript'>
jwplayer('my-video').setup({
file:'rtmp://172.19.197.244/myapp/test', //#live是applicatioin,test是直播緩存流文件
width:'100%',
aspectratio:'3:2',
fallback:'false',
primary:'flash'
});
</script>
</body>
</html>
ps:由于腳本默認(rèn)不調(diào)用JavaScript函數(shù),所以我們要去官網(wǎng)下載這個(gè)函數(shù)配置文件,將它放在
/usr/local/nginx/html/jwplayer/jwplayer.js,由于jwplayer.js是沒有的,需要自己創(chuàng)建
-->下載網(wǎng)址:https://github.com/jwplayer/jwdeveloper-demos/blob/master/demos/toolbox/live-streaming/index.html
找到下圖這個(gè)網(wǎng)址,然后打開網(wǎng)址將jwplayer.js的代碼復(fù)制進(jìn)你之前創(chuàng)建的配置文件中
8,使用ffmpeg推流到nginx
推一個(gè)本地的mp4到上面配置的myapp上:
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/myapp/test1"
1
流播放地址為(10.0.0.6是我本地的IP):rtmp://10.0.0.6:1935/myapp/test1
1
推一個(gè)本地的mp4到hls上
ffmpeg -re -i /tmp/ffmpeg_test.mp4 -vcodec copy -acodec copy -f flv "rtmp://127.0.0.1:1935/hls/test2"
1
流播放地址為: http://10.0.0.6/hls/test2.m3u8
1
流媒體播放器地址:http://www.cutv.com/demo/live_test.swf

VLC播放hls流:
