下列說明完全摘自網(wǎng)絡(luò)剧包,后期會(huì)整理后修改调塌。
RTMP是Real Time Messaging Protocol(實(shí)時(shí)消息傳輸協(xié)議)的首字母縮寫。該協(xié)議基于TCP嵌削,是一個(gè)協(xié)議族,包括RTMP基本協(xié)議及RTMPT/RTMPS/RTMPE等多種變種望艺。RTMP是一種設(shè)計(jì)用來進(jìn)行實(shí)時(shí)數(shù)據(jù)通信的網(wǎng)絡(luò)協(xié)議苛秕,主要用來在Flash/AIR平臺(tái)和支持RTMP協(xié)議的流媒體/交互服務(wù)器之間進(jìn)行音視頻和數(shù)據(jù)通信。支持該協(xié)議的軟件包括Adobe Media Server/Ultrant Media Server/red5等找默。
RTMP又是Routing Table Maintenance Protocol(路由選擇表維護(hù)協(xié)議)的縮寫艇劫。 在 AppleTalk 協(xié)議組中,路由選擇表維護(hù)協(xié)議(RTMP惩激,Routing Table Protocol)是一種傳輸層協(xié)議店煞,它在 AppleTalk?路由器中建立并維護(hù)路由選擇表蟹演。RTMP 基于路由選擇信息協(xié)議(RIP)。正如 RIP 一樣顷蟀,RTMP 使用跳數(shù)作為路由計(jì)量標(biāo)準(zhǔn)酒请。一個(gè)數(shù)據(jù)包從源 網(wǎng)絡(luò)發(fā)送到目標(biāo)網(wǎng)絡(luò),必須通過的路由器或其它中間介質(zhì)節(jié)點(diǎn)數(shù)目的計(jì)算結(jié)果即為跳數(shù)鸣个。
CentOS6下基于Nginx搭建mp4/flv流媒體服務(wù)器(可隨意拖動(dòng))并支持RTMP/HLS協(xié)議(含轉(zhuǎn)碼工具)
1.先添加幾個(gè)RPM下載源
1.1)安裝RPMforge的CentOS6源
[root@linuxidc ~]# wget -c http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.3-1.el6.rf.x86_64.rpm
[root@linuxidc ~]# rpm –import http://apt.sw.be/RPM-GPG-KEY.dag.txt
[root@linuxidc ~]# rpm -i rpmforge-release-0.5.3-1.el6.rf.*.rpm
1.2)安裝epel源
[root@linuxidc ~]# wget -c http://download.Fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
[root@linuxidc ~]# rpm -Uvf epel-release-6-8.noarch.rpm
安裝完成羞反,查看一下源列表,看到rpmforge和epel字樣即可
[root@linuxidc ~]# ls -lh /etc/yum.repos.d
2.安裝轉(zhuǎn)碼工具M(jìn)encoder及ffmpeg(約定:每個(gè)小點(diǎn)操作之前先回到用戶主目錄囤萤,即cd ~)
2.1)安裝一些基礎(chǔ)包昼窗,不用擔(dān)心重復(fù),已經(jīng)存在的會(huì)自動(dòng)忽略或升級(jí)
[root@linuxidc ~]# yum install gcc make automake bzip2 unzip patch subversion libjpeg-devel
[root@linuxidc ~]# wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
[root@linuxidc ~]# tar zxvf yasm-1.2.0.tar.gz
[root@linuxidc ~]# cd yasm-1.2.0
[root@linuxidc yasm-1.2.0]# ./configure
[root@linuxidc yasm-1.2.0]# make && make install
[root@linuxidc yasm-1.2.0]# cd ~
2.2)卸載系統(tǒng)原有的ffmgeg和x.264涛舍,沒有則跳過
[root@linuxidc ~]# yum remove ffmpeg x264
2.3)先安裝一些Mplayer編碼庫(kù)(僅限于64位系統(tǒng))
[root@linuxidc ~]# wget -c http://www2.mplayerhq.hu/MPlayer/releases/codecs/essential-amd64-20071007.tar.bz2
上面這一步可能在部分VPS上無法下載膏秫,比如我的阿里云VPS就提示連接失敗,但是可以用本機(jī)瀏覽器下載后上傳到VPS的/root根目錄去做盅,然后繼續(xù)下面的操作
[root@linuxidc ~]# tar xvjf essential-amd64-20071007.tar.bz2
[root@linuxidc ~]# mkdir /usr/local/lib/codecs
[root@linuxidc ~]# cp -Rvp essential-amd64-20071007/* /usr/local/lib/codecs/
編輯下面文件
[root@linuxidc ~]# vim /etc/ld.so.conf
添加以下兩行到上面的文件里
/usr/lib
/usr/local/lib
2.4)再安裝一些格式轉(zhuǎn)換常用的編碼庫(kù)
[root@linuxidc ~]# yum install faac-devel? lame-devel amrnb-devel opencore-amr-devel amrwb-devel? libvorbis-devel libtheora-devel xvidcore-devel
2.5)安裝x.264
[root@linuxidc ~]# wget ftp://ftp.videolan.org/pub/videolan/x264/snapshots/last_stable_x264.tar.bz2
[root@linuxidc ~]# tar xvjf last_stable_x264.tar.bz2
[root@linuxidc ~]# cd x264-snapshot-20140219-2245-stable/? ? (這可能日期有區(qū)別缤削,自己ls一下)
[root@linuxidc x264-snapshot-20140219-2245-stable]# ./configure –enable-shared –enable-pic
[root@linuxidc x264-snapshot-20140219-2245-stable]# make && make install
[root@linuxidc x264-snapshot-20140219-2245-stable]#? cd ~
2.6)安裝libvpx
[root@linuxidc ~]# wget http://webm.googlecode.com/files/libvpx-v1.2.0.tar.bz2
[root@linuxidc ~]# tar xvjf libvpx-v1.2.0.tar.bz2
[root@linuxidc ~]# cd libvpx-v1.2.0
[root@linuxidc libvpx-v1.2.0]# ./configure –enable-shared –enable-pic
[root@linuxidc libvpx-v1.2.0]# make && make install
[root@linuxidc libvpx-v1.2.0]# cd ~
2.7)安裝FFmpeg
[root@linuxidc ~]# wget http://ffmpeg.org/releases/ffmpeg-2.0.1.tar.bz2
[root@linuxidc ~]# tar xvjf ffmpeg-2.0.1.tar.bz2
[root@linuxidc ~]# cd ffmpeg-2.0.1
[root@linuxidc ffmpeg-2.0.1]# ./configure –enable-gpl –enable-version3 –enable-shared –enable-nonfree –enable-postproc –enable-libfaac –enable-libmp3lame –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libtheora –enable-libvorbis –enable-libvpx –enable-libx264 –enable-libxvid
[root@linuxidc ffmpeg-2.0.1]# make && make install
[root@linuxidc ffmpeg-2.0.1]# cd ~
讓動(dòng)態(tài)鏈接庫(kù)被系統(tǒng)共享
[root@linuxidc ~]# ldconfig
2.8)最后安裝mencoder
[root@linuxidc ~]# yum install mplayer mencoder flvtool2
查看一下已經(jīng)安裝好的音頻和視頻編碼器
查看所有所支持的音頻編碼
[root@linuxidc ~]# mencoder -oac help
查看所有所支持的視頻編碼
[root@linuxidc ~]# mencoder -ovc help
具體結(jié)果可參考http://www.mplayerhq.hu/DOCS/HTML/zh_CN/menc-feat-selecting-codec.html
3.安裝配置Nginx
3.1)先安裝各種依賴(nginx需要pcre支持,yamdi用來為flv創(chuàng)建關(guān)鍵幀才能隨意拖動(dòng))
[root@linuxidc ffmpeg-2.0.1]# yum install gcc gcc-c++ openssl-devel zlib-devel pcre pcre-devel yamdi
3.2)下載所需的nginx模塊
第一個(gè)是nginx_mod_h264_streaming,讓nginx支持flv/mp4流播放
[root@linuxidc ~]# wget http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
[root@linuxidc ~]# tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
注意:先要修改一下這家伙的源碼吹榴,注釋掉nginx_mod_h264_streaming-2.2.7/src/ngx_http_streaming_module.c的158到161行
/* TODO: Win32 */
//if (r->zero_in_uri)
// {
//? return NGX_DECLINED;
// }
第二個(gè)是nginx-rtmp-module亭敢,讓nginx支持rtmp/hls協(xié)議
[root@linuxidc ~]# wget -O nginx-rtmp-module.zip? https://github.com/arut/nginx-rtmp-module/archive/master.zip
[root@linuxidc ~]# unzip nginx-rtmp-module.zip
下載清緩存的模塊
[root@linuxidc ~]# wget -O ngx_cache_purge.zip https://github.com/FRiCKLE/ngx_cache_purge/archive/master.zip
[root@linuxidc ~]# unzip ngx_cache_purge.zip
3.3)下載安裝nginx
[root@linuxidc ~]# wget http://nginx.org/download/nginx-1.2.9.tar.gz
[root@linuxidc ~]# tar zxvf nginx-1.2.9.tar.gz
[root@linuxidc ~]# cd nginx-1.2.9
[root@linuxidc nginx-1.2.9]# ./configure –user=daemon –group=daemon –prefix=/usr/local/nginx/ –add-module=../nginx-rtmp-module-master –add-module=../ngx_cache_purge-master –add-module=../nginx_mod_h264_streaming-2.2.7 –with-http_stub_status_module –with-http_ssl_module –with-http_sub_module –with-http_gzip_static_module –with-http_flv_module
[root@linuxidc nginx-1.2.9]# make && make install
PS:若提示make[1]: *** [objs/addon/src/ngx_http_h264_streaming_module.o] Error 1,請(qǐng)參考3.3.2小節(jié)的注意點(diǎn)图筹,然后重新運(yùn)行./configure這一段
/**———以下是安裝了Tengine后重新編譯的代碼帅刀,模塊的路徑自己對(duì)一下———**//
./configure –user=www –group=www –prefix=/usr/local/tengine/ –add-module=../../../nginx-rtmp-module-master –add-module=../../../ngx_cache_purge-master –add-module=../../../nginx_mod_h264_streaming-2.2.7 –with-http_stub_status_module –with-http_ssl_module –with-http_sub_module –with-http_gzip_static_module –with-http_flv_module –with-http_concat_module=shared –with-http_sysguard_module=shared –with-google_perftools_module –with-http_image_filter_module
運(yùn)行一下nginx
[root@linuxidc ~]# /usr/local/nginx/sbin/nginx
然后用瀏覽器你的服務(wù)器IP,看到welcome就對(duì)了
或者到nginx的sbin目錄下远剩,運(yùn)行一下nginx -V扣溺,看看列表出來的modules對(duì)不對(duì)
3.4)各種配置nginx
編輯/usr/local/nginx/conf/nginx.conf文件,最好用sftp軟件(如windows下的flashfxp/Mac下的tramnsmit)下載過來本地編輯瓜晤。
貼一下我的配置文件:(目錄需要自己改動(dòng)锥余,我用的是阿里云的數(shù)據(jù)盤,所以到/mnt/里面去了)
———————————————————————nginx配置文件—————————————————
#filename:nginx.conf
#user? nobody;
worker_processes? 1;
error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
pid? ? ? ? logs/nginx.pid;
events {
use epoll;
worker_connections? 1024;
}
rtmp {
server {
listen 1935;
chunk_size 4000;
# video on demand
application vod {
play /mnt/media/vod;
}
# HLS
# HLS requires libavformat & should be configured as a separate
# NGINX module in addition to nginx-rtmp-module:
# ./configure … –add-module=/path/to/nginx-rtmp-module/hls …
# For HLS to work please create a directory in tmpfs (/tmp/app here)
# for the fragments. The directory contents is served via HTTP (see
# http{} section in config)
#
# Incoming stream must be in H264/AAC/MP3. For iPhones use baseline H264
# profile (see ffmpeg example).
# This example creates RTMP stream from movie ready for HLS:
#
# ffmpeg -loglevel verbose -re -i movie.avi? -vcodec libx264
#? ? -vprofile baseline -acodec libmp3lame -ar 44100 -ac 1
#? ? -f flv rtmp://localhost:1935/hls/movie
#
# If you need to transcode live stream use ‘exec’ feature.
#
application hls {
hls on;
hls_path /mnt/media/app;
hls_fragment 10s;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
#log format
log_format? access? ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” $http_x_forwarded_for’;
#定義一個(gè)名為addr的limit_zone,大小10M內(nèi)存來存儲(chǔ)session
limit_conn_zone $binary_remote_addr zone=addr:10m;
server {
listen 8080;
server_name localhost;
# HTTP can be used for accessing RTMP stats
# This URL provides RTMP statistics in XML
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /mnt/soft/nginx-rtmp-module-master;
}
location /control {
rtmp_control all;
}
location / {
root /mnt/soft/nginx-rtmp-module-master/test/rtmp-publisher;
}
}
server {
listen 80;
server_name localhost;
location / {
root /mnt/wwwroot;
index index.html;
}
location ~ \.flv$ {
root /mnt/media/vod;
flv;
limit_conn addr 20;
limit_rate 200k;
}
location ~ \.mp4$ {
root /mnt/media/vod;
mp4;
limit_conn addr 20;
limit_rate 200k;
}
location /hls {
# Serve HLS fragments
alias /mnt/media/app;
}
access_log? logs/nginxflv_access.log access;
}
}
——————————————————nginx配置文件——————————————
4.把自己的電影轉(zhuǎn)換成mp4和flv格式來測(cè)試nginx搭的環(huán)境
4.1)準(zhǔn)備兩部電影痢掠,硬盤上隨便找驱犹,我找了“諜影重重A.mp4”和“鹿鼎記033.rmvb”,盡量找小一點(diǎn)十來分鐘的足画,等下我們還要看完測(cè)試一下轉(zhuǎn)換的結(jié)果有沒有音影不同步的情況雄驹。
我把兩部電影重命名為 movie1.mp4和 movie2.rmvb,并上傳到服務(wù)器/mnt/media/video下面淹辞,這里目錄用來存放我們的原始視頻医舆。還有一個(gè)目錄是/mnt/media/vod 用來存放轉(zhuǎn)換后的視頻。
我這里的具體目錄結(jié)構(gòu)為:
/mnt/media/video -> 存放原始視頻
/mnt/media/app? -> 存放轉(zhuǎn)成m3u8的視頻,供http訪問(HLS)
/mnt/media/vod? -> 存放轉(zhuǎn)換后的flv和mp4視頻,供http或rtmp訪問
4.2)用ffmpeg轉(zhuǎn)換mp4文件(ffmpeg不支持rmvb)
[root@linuxidc ~]# cd /mnt/media/video/
[root@linuxidc video]# ffmpeg -i movie1.mp4 -y -vcodec libx264 -vf scale=”640:-1″ -r 15 -acodec libfaac ../vod/movie1.flv
-y:文件覆蓋,-vf scale=”640:-1”:尺寸調(diào)整為寬度640高度自動(dòng)蔬将,-r 15:幀數(shù)15fps爷速,這里用libfaac音頻編碼防止轉(zhuǎn)成ts格式時(shí)iPhone沒有聲音
添加關(guān)鍵幀用來支持拖動(dòng)播放
[root@linuxidc video]# cd ../vod
[root@linuxidc vod]# mv movie1.flv movie1-src.flv
[root@linuxidc vod]# yamdi -i movie1-src.flv -o movie1.flv
[root@linuxidc vod]# rm -rf movie1-src.flv
接下來測(cè)試的話,下載VLC Player到本地測(cè)試(VLC有時(shí)候會(huì)花屏娃胆,用JWPlayer就好了)遍希,或者不行麻煩的話裝一個(gè)apache服務(wù)器并下載JWPlayer來測(cè)試。
測(cè)試播放地址為(地址已失效里烦,請(qǐng)勿訪問):
HTTP形式訪問:? ? ? http://www.linuxidc.com/movie1.flv
RTMP形式訪問:? ? ? rtmp://www.linuxidc.com/vod/movie1.flv
4.3)用mencoder轉(zhuǎn)換rmvb文件
4.4)把flv轉(zhuǎn)換成hls的m3u8
下載安裝segmenter
[root@linuxidc ~]# yum install -y curl curl-devel zlib-devel openssl-devel perl perl-devel cpio expat-devel gettext-devel git
[root@linuxidc ~]# git clone https://github.com/johnf/m3u8-segmenter.git
[root@linuxidc ~]# cd m3u8-segmenter/
[root@linuxidc m3u8-segmenter]# gcc -Wall -g m3u8-segmenter.c -o segmenter -lavformat
[root@linuxidc m3u8-segmenter]# cp segmenter /usr/bin/
轉(zhuǎn)換成ts凿蒜,片源大小及清晰度等取決于flv文件,所以轉(zhuǎn)成flv的時(shí)候一定要統(tǒng)一起來
[root@linuxidc vod]# cd /mnt/media/vod
[root@linuxidc vod]# mkdir /mnt/media/app/movie1/
[root@linuxidc vod]# ffmpeg -y -i movie1.flv -f mpegts -c:v copy -c:a copy -vbsf h264_mp4toannexb /mnt/media/app/movie1/main.ts
切片
[root@linuxidc movie1]# cd /mnt/media/app/movie1/
[root@linuxidc movie1]# segmenter -i main.ts -d 10 -p movie1 -m movie1.m3u8 -u http://www.linuxidc.com/hls/movie1/
-d 10:每個(gè)切片為10秒胁黑,-p movie1:切片的名字的前綴
-u URL地址:m3u8中播放列表的地址前綴废封,自己cat一下生成的movie1.m3u8就知道什么用 了
測(cè)試一下,用iPhone的Safari訪問一下http://www.linuxidc.com/hls/movie1/movie1.m3u8
注:安裝日志記錄于半年前的evernote丧蘸,現(xiàn)在才貼出來的漂洋,所以文中的視頻訪問地址都已經(jīng)失效了。
實(shí)測(cè)環(huán)境
IP : 192.168.0.193
/www/hls/
Nginx配置:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
application myapp {
live on;
#record keyframes;
#record_path /tmp;
#record_max_size 128K;
#record_interval 30s;
#record_suffix .this.is.flv;
#on_publish http://localhost:8080/publish;
#on_play http://localhost:8080/play;
#on_record_done http://localhost:8080/record_done;
}
application ios{
live on;
hls on;
hls_path /www/hls;
hls_playlist_length 10s;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
#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;
location / {
root /www/wwwroot;
index index.html index.htm;
}
#error_page 404 /404.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;
}
alias /www/hls;
}
}
}
講RTMP流轉(zhuǎn)換為HLS流力喷,兼容IOS蘋果設(shè)備
ffmpeg -i rtmp://192.168.0.193/myapp/ ?-c:a copy ?-c:v libx264 -b:v 512K -g 30 -f flv rtmp://192.168.0.193/ios/show
蘋果設(shè)備測(cè)試地址為http://192.168.0.193/hls/show.m3u8