HLS协议及java切片相关
http://blog.csdn.net/cjsafty/article/details/7922849
簡介:HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基于HTTP的流媒體 網(wǎng)絡(luò)傳輸協(xié)議。
是蘋果公司QuickTime X和iPhone軟件系統(tǒng)的一部分。它的工作原理是把整個流分成一個個小的基于HTTP
的文件來下載,每次只下載一些。當(dāng)媒體流正在播放時,客戶端可以選擇從許多不同的備用源中以不同的速
率下載同樣的資源,允許流媒體會話適應(yīng)不同的數(shù)據(jù)速率。在開始一個流媒體會話時,客戶端會下載一個包
含元數(shù)據(jù)的extended M3U (m3u8) playlist文件,用于尋找可用的媒體流。
HLS只請求基本的HTTP報文,與實時傳輸協(xié)議(RTP)不同,HLS可以穿過任何允許HTTP數(shù)據(jù)通過的防
火墻或者代理服務(wù)器。它也很容易使用內(nèi)容分發(fā)網(wǎng)絡(luò)來傳輸媒體流。
此協(xié)議詳細內(nèi)容請參考apple官方網(wǎng)站:https://developer.apple.com/resources/http-streaming/
有兩種方式搭建HLSserver,
一種是利用apple SDK,
一種是利用adobe 的fms,4.5版本支持hls,參考,
http://www.adobe.com/products/flash-media-streaming/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_new.html
adobe的fms現(xiàn)在很強大,但是商用需要licence。有興趣的可以研究下。
一種是利用opensouce.我比較喜歡這一種。
?
方法:
opensource的方法主要是使用m3u8-segmenter+ffmpeg對ts文件進行分片。
因此思路就是:
1,用編譯好的ffmpeg制作所需要的ts文件,
2,安裝libavformat-dev版本,
3,編譯m3u8-segmenter,
4,部署到nginx
5,高級功能,流切換
6,頁面
過程
1,本來想下載ffmpeg源碼編譯,但是因為要涉及到faac,x264,lame庫。有時候ffmpeg版本對這些庫的版本又有最低版本要求,在編譯
faac時候遇到以下問題
[plain] view plaincopy
最后一個問題無法解決,好像是Linux(ubuntu)下同一個目錄下,如果已經(jīng)有一個文件了,則不能創(chuàng)建同名文件夾,遂放棄編譯,
直接從ffmpeg網(wǎng)站:http://ffmpeg.org/download.html ,的linux下載頁面下載編譯好ffmpeg靜態(tài)文件。這個靜態(tài)文件的主要目的是
為了把各種文件轉(zhuǎn)換成apple所規(guī)定的文件。所以需要AAC,mp3,x264庫支持。
或者干脆按照2的方法。apt-get install ffmpeg.這樣會得到ffmpeg可行性文件。
2,安裝ffmpeg支持庫,主要用于編譯m3u8-segmenter,這里的ffmpeg支持庫,其目的是給segmenter提供libavformat支持。不涉及編解碼。
apt-get install libavformat-dev.
[plain] view plaincopy這樣會自動安裝ffmepg幾個相關(guān)庫。
3,從https://github.com/johnf/m3u8-segmenter?下載m3u8-segmenter
下載后不要用它的反復(fù)編譯,直接取m3u8-segmenter.c文件,
[plain] view plaincopy從源碼來看,因為只用到了avformat庫,所以只鏈接這一個即可。生成segmenter文件,用help命令,可以看到已經(jīng)成功。
[plain] view plaincopy從上圖來看,語法很簡單,這里貼一個我用的。 [plain] view plaincopy
i表示輸入文件,n表示切割30個,p表示切割文件的前綴。m表示生成的m3u8文件名,u表示這些切割后的文件處于web server的哪個目錄下,這個一定要和web目錄匹配
4,部署到nginx。
nginx的相關(guān)部署我在前兩個博客中已經(jīng)詳細說明,這里在jwplayer博客的基礎(chǔ)上部署hls。
1)目錄問題:
在html/jwplayer目錄下,建立hls文件夾,將m3u8文件和切割后的全部ts文件拷貝到此目錄下,
在VLC PLAYER或者ipad safie瀏覽器或者在ffplayer(我用的是0.11版本的windows編譯版本)
上的訪問路徑應(yīng)該是http://192.168.1.10:8080/hls/stream-test.m3u8
2)文件類型問題:編輯?/usr/local/nginx/conf/mime.types 文件,添加如下類型
[plain] view plaincopy
輸入上述路徑,你應(yīng)該就看到視頻了。
5,高級功能,流切換
上述m3u8文件,只有一個流,不具備流切換功能。在優(yōu)酷上,如果是ipad客戶端,可以看到有標(biāo)清,高清,超清的按鈕,其實那個是對應(yīng)著不同標(biāo)準(zhǔn)
的(單個)m3u8文件,來實現(xiàn)流切換的,不知道apple是不是這樣做的,apple好像是要求“智能”流切換。即不要求用戶去選擇,而是根據(jù)網(wǎng)絡(luò)狀況自適應(yīng)的。
apple給的sample的流切換是把各個流的m3u8寫在一個m3u8文件里實現(xiàn)的。
類似于這樣,其實原理是一樣的。
[plain] view plaincopy
6,頁面,
如果再繼續(xù)搭建一個頁面,把上述地址嵌在頁面里面,這樣配合CSS就比較美觀了。頁面可以在這個基礎(chǔ)上,用webpy去做。
頁面參考:
蘋果開發(fā)網(wǎng):https://developer.apple.com/resources/http-streaming/
博客:http://www.nginxs.com/linux/459.html
ffmpeg開發(fā)網(wǎng):http://ffmpeg.org/download.html
segmenter源碼:https://github.com/johnf/m3u8-segmenter
adboe fms介紹:http://www.adobe.com/products/flash-media-streaming/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_new.html
?
2014.02備注:
文中所描述的切片軟件可能有問題,可選擇這個
https://code.google.com/p/httpsegmenter/downloads/list
說明:在寫這篇文章時不知道查了多少資料不是資料不完整就是根本不能安裝,網(wǎng)上許多資料都是轉(zhuǎn)載。寫這篇文章時我親自做了測試,安裝完全通過,如果有什么不對的地方歡迎拍磚?
一、安裝ffmpeg?
操作系統(tǒng):centos 5.6?
(一)安裝編譯環(huán)境?
#yum install -y automake autoconf libtool gcc gcc-c++??
(二)安裝所需程序庫的RPM包到 centos(因為centos自帶的庫中沒有ffmpeg包,這里相當(dāng)于是擴展)?
view plaincopy to clipboardprint??
#rpm -Uhv http://apt.sw.be/redhat/el5/en/i386/rpmforge/RPMS/rpmforge-release-0.3.6-1.el5.rf.i386.rpm??
如果上面的代碼執(zhí)行失敗,你可以在本站下載,然后安裝這個rpm包。?
(三)安裝 Install ffmpeg 等模塊?
yum -y install ffmpeg ffmpeg-devel
CentOS yum方式安裝ffmpeg
?天使羊波波閃耀光芒原文??http://www.live-in.org/archives/1860.html系統(tǒng)為CentOS 6.4,自己編譯ffmpeg和眾多解碼器太痛苦了,yum方式安裝ffmpeg的解碼器很全,先用用看。
1、安裝所需軟件?
yum install -y automake autoconf libtool gcc gcc-c++
2、安裝第三方更新源?
rpm -ivh http://apt.sw.be/redhat/el6/en/i386/rpmforge/RPMS/rpmforge-release-0.5.3-1.el6.rf.i686.rpm
3、安裝ffmpeg?
yum install ffmpeg ffmpeg-devel
會自動安裝眾多解碼器,ffmpeg版本為0.6.5-1.el6.rf。看了下yum安裝的解碼器版本還不是很舊,但是ffmpeg版本較老。
4、測試一下?
將一個.avi視屏轉(zhuǎn)為.flv格式?
ffmpeg -i Destination.avi -y -ab 56 -ar 22050 -r 15 -sameq test2.flv
轉(zhuǎn)換前后兩個文件的格式比較:
Destination.avi
?? General
????? Complete name : E:\PV\Destination.avi
????? Format : AVI
????? Format/Info : Audio Video Interleave
????? Format/Family : RIFF
????? File size : 79.5 MiB
????? PlayTime : 4mn 1s
????? Bit rate : 2754 Kbps
????? StreamSize : 469 KiB
????? Subject : doobie (2007)
????? Writing application : VirtualDubMod 1.5.10.2 (build 2540/release)
????? Writing library : VirtualDubMod build 2540/release
?? Video #0
????? Codec : XviD
????? Codec/Family : MPEG-4
????? Codec/Info : XviD project
????? Codec profile : Streaming Video Profile/Level 1
????? Codec settings/PacketBitStream : Yes
????? Codec settings/BVOP : Yes
????? Codec settings/QPel : No
????? Codec settings/GMC : 0
????? Codec settings/Matrix : Default
????? PlayTime : 4mn 1s
????? Bit rate : 2556 Kbps
????? Width : 640 pixels
????? Height : 480 pixels
????? Display Aspect ratio : 4/3
????? Frame rate : 29.976 fps
????? Resolution : 8 bits
????? Chroma : 4:2:0
????? Interlacement : Progressive
????? Bits/(Pixel*Frame) : 0.277
????? StreamSize : 73.5 MiB
?? Audio #1
????? Codec : MPEG-1 Audio layer 3
????? Codec profile : Joint stereo
????? PlayTime : 4mn 1s
????? Bit rate : 190 Kbps
????? Bit rate mode : CBR
????? Channel(s) : 2 channels
????? Sampling rate : 48 KHz
????? Resolution : 16 bits
????? StreamSize : 5.47 MiB
????? Writing library : Gogo (after 3.0)
轉(zhuǎn)換后
test2.flv
?? General
????? Complete name : E:\PV\test2.flv
????? Format : Flash Video
????? File size : 45.0 MiB
????? PlayTime : 4mn 1s
????? Bit rate : 1564 Kbps
?? Video
????? Codec : Sorenson H263
????? PlayTime : 4mn 1s
????? Bit rate : 195 Kbps
????? Width : 640 pixels
????? Height : 480 pixels
????? Display Aspect ratio : 4/3
????? Frame rate : 15.000 fps
????? Bits/(Pixel*Frame) : 0.042
?? Audio
????? Codec : MPEG-1 Audio Layer 3
????? Bit rate : 55 bps
????? Channel(s) : 2 channels
????? Sampling rate : 22 KHz
????? Resolution : 16 bits
參考資料:
http://www.rosoo.net/a/201205/16001.html
分類:?LINUX?發(fā)表評論
-
CentOS編譯安裝ffmpeg以及相關(guān)編碼解碼器
CentOS編譯安裝ffmpeg是一件很痛苦的過程,因為各種編碼器、解碼器什么的很繁雜,而且相互依賴關(guān)系更復(fù)雜。
編譯了一天終于把它搞定并基本測試無問題,以下是步驟。
安裝yasm
wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
tar zxf yasm-1.2.0.tar.gz
cd yasm-1.2.0
./configure
make
make install安裝frei0r(先安裝新版autoconf,這會覆蓋掉系統(tǒng)中的舊版本哦)
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar zxf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure –prefix=/usr
make
make installwget http://files.dyne.org/frei0r/releases/frei0r-plugins-1.4.tar.gz
tar zxf frei0r-plugins-1.4.tar.gz
cd frei0r-plugins-1.4
./configure
make
make install安裝gnutls
參考:http://blog.creke.net/800.html
安裝mp3lame
wget http://iweb.dl.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
tar zxf lame-3.99.5.tar.gz
cd lame-3.99.5
make
make install安裝opencore-amr
wget http://iweb.dl.sourceforge.net/project/opencore-amr/opencore-amr/opencore-amr-0.1.3.tar.gz
tar zxf opencore-amr-0.1.3.tar.gz
./configure –enable-shared
make
make install安裝opus
wget http://downloads.xiph.org/releases/opus/opus-1.0.2.tar.gz
tar zxf opus-1.0.2.tar.gz
cd opus-1.0.2
make
make install安裝rtmpdump
wget http://rtmpdump.mplayerhq.hu/download/rtmpdump-2.3.tgz
tar zxf rtmpdump-2.3.tgz
cd rtmpdump-2.3
make
make install安裝schroedinger(先安裝好orc庫)
wget http://code.entropywave.com/download/orc/orc-0.4.17.tar.gz
tar zxf orc-0.4.17.tar.gz
cd orc-0.4.17
./configure
make
make install
/sbin/ldconfigwget http://diracvideo.org/download/schroedinger/schroedinger-1.0.11.tar.gz
tar zxf schroedinger-1.0.11.tar.gz
cd schroedinger-1.0.11
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig ./configure
make
make install安裝soxr(注:新版libsoxr貌似與當(dāng)前ffmpeg不兼容,用舊版)
yum install -y xz
wget http://hivelocity.dl.sourceforge.net/project/soxr/soxr-0.1.0-Source.tar.xz
xz -d soxr-0.1.0-Source.tar.xz
tar xf soxr-0.1.0-Source.tar
cd soxr-0.1.0-Source
./go
cd Release/
make
make install
/sbin/ldconfig安裝speex(先安裝libogg庫)
wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
tar zxf libogg-1.3.0.tar.gz
./configure
make
make install
/sbin/ldconfigwget http://downloads.xiph.org/releases/speex/speex-1.2rc1.tar.gz
tar zxf speex-1.2rc1.tar.gz
cd speex-1.2rc1
./configure
make
make install安裝theora(先安裝好libvorbis庫和libsdl庫)
wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
tar zxf libvorbis-1.3.3.tar.gz
./configure
make
make installwget http://www.libsdl.org/release/SDL-1.2.15.tar.gz
tar zxf SDL-1.2.15.tar.gz
cd SDL-1.2.15
./configure
make
make install
/sbin/ldconfigwget http://downloads.xiph.org/releases/theora/libtheora-1.1.1.tar.bz2
tar zjf libtheora-1.1.1.tar.bz2
./configure
make
make install安裝twolame
wget http://downloads.sourceforge.net/twolame/twolame-0.3.13.tar.gz
tar zxf twolame-0.3.13
.tar.gz
cd twolame-0.3.13
./configure
make
make install
/sbin/ldconfig安裝VisualOn AAC(注:github的tag和master版均無法autoconf,用zeranoe版代替)
wget wget http://ffmpeg.zeranoe.com/builds/source/external_libraries/vo-aacenc-0.1.2.tar.xz
xz -d vo-aacenc-0.1.2.tar.xz
tar xf vo-aacenc-0.1.2.tar
cd vo-aacenc-0.1.2
./configure
make
make install安裝VisualOn AMR-WB(注:github的tag和master版均無法autoconf,用zeranoe版代替)
wget http://ffmpeg.zeranoe.com/builds/source/external_libraries/vo-amrwbenc-0.1.2.tar.xz
xz -d vo-amrwbenc-0.1.2.tar.xz
tar xf vo-amrwbenc-0.1.2.tar
cd vo-amrwbenc-0.1.2
./configure安裝libvpx(注:新版libvpx貌似與當(dāng)前ffmpeg不兼容,用舊版)
wget http://webm.googlecode.com/files/libvpx-v1.1.0.tar.bz2
tar jxf libvpx-v1.1.0.tar.bz2
cd libvpx-v1.1.0
./configure
make
make install安裝x264
wget ftp://ftp.videolan.org/pub/x264/snapshots/last_x264.tar.bz2
tar jxf last_x264.tar.bz2
cd x264-snapshot-20130311-2245/
./configure –enable-shared
make
make install安裝xvid
wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
tar zxf xvidcore-1.3.2.tar.gz
cd xvidcore
cd build/generic
./configure
make
make install最重要的一步——編譯安裝ffmpeg!
wget http://ffmpeg.org/releases/ffmpeg-1.1.3.tar.gz
tar zxf ffmpeg-1.1.3.tar.gz
cd ffmpeg-1.1.3
PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/local/lib/pkgconfig:/usr/lib/pkgconfig ./configure –disable-static –enable-shared –enable-gpl –enable-version3 –enable-bzlib –enable-fontconfig –enable-frei0r –enable-gnutls –enable-libfreetype –enable-libmp3lame –enable-libopencore-amrnb –enable-libopencore-amrwb –enable-libopus –enable-librtmp –enable-libschroedinger –enable-libsoxr –enable-libspeex –enable-libtheora –enable-libtwolame –enable-libvo-aacenc –enable-libvo-amrwbenc –enable-libvorbis –enable-libvpx –enable-libx264 –enable-libxvid –enable-zlib
make
make install注1:
我在編譯安裝時參考Windows Build:http://ffmpeg.zeranoe.com/builds/。
注2:
與Windows Build相比,以下沒有選項開啟:
–enable-libgsm –enable-libilbc -enable-libass –enable-libbluray –enable-libcaca –enable-libopenjpeg –disable-w32threads –enable-avisynth –enable-libxavs
分類:?所謂技術(shù)?| 標(biāo)簽:?CentOS,ffmpeg,安裝,編碼器,編譯,解碼器
http://www.cnblogs.com/mystory/archive/2013/04/07/3006200.html
linux下搭建生成HLS所需的.ts和.m3u8文件
要想利用HLS來實現(xiàn)視頻的在線播放,就得需要將一個完整的視頻文件切割成多個ts視頻流,然后利用m3u8的索引文件來播放。
在Mac下,蘋果提供了streamingTools的工具,里面有mediafilesegmenter和mediastreamsegmenter來分別實現(xiàn)文件和直播流的切割,一行命令直接就可以將輸入的原始視頻文件導(dǎo)出成幾個ts和索引文件,直接就可以用了。但是一般服務(wù)器都是基于linux的,要想在linux下實現(xiàn)同樣的切割,著實費了一番功夫。
網(wǎng)上也找了好多相關(guān)的資料,基本是利用開源的ffmpeg和segmenter工具來實現(xiàn),但是這搭建這個環(huán)境的過程是曲折的,編譯這些工具的時候會出現(xiàn)好多錯誤,難以解決,好多文章的步驟介紹的也很復(fù)雜,又要安裝各種包,又要修改源代碼,經(jīng)過幾天的各種反復(fù)嘗試,才知道在不是直接將源文件切割,而是先要轉(zhuǎn)換一下,再切割,最終終于痛苦的摸索到了一個成功的路子,特此記錄下來以防忘記。
1、首先獲得ffmpeg
apt-get install ffmpeg
2、安裝ffmpeg支持庫,為m3u8-segmenter準(zhǔn)備
apt-get install libavformat-dev
3、獲得m3u8-segmenter
https://github.com/johnf/m3u8-segmenter?
4、編譯并安裝m3u8-segmenter
aclocal
automake -ac
./configure
make
sudo make install
4、編譯其中的文件m3u8-segmenter
gcc -Wall -g ?m3u8-segmenter.c -o segmenter -lavformat?
5、利用ffmpeg將已有的視頻文件轉(zhuǎn)換成ts文件
ffmpeg -y -i <in file> -vcodec copy -acodec copy -vbsf h264_mp4toannexb <output file>
其中in file為待轉(zhuǎn)換的視頻文件,比如input.mov,output file為轉(zhuǎn)換后的文件,要命名為output.ts
6、利用segmenter將轉(zhuǎn)換好的ts文件切割成多個ts片,并生成.m3u8的索引文件
./segmenter -i out.ts -n 10 -p segmenter_test -m test.m3u8 -u?
i表示輸入文件,n表示切割10秒,p表示切割文件的前綴。m表示生成的m3u8文件名,u表示這些切割后的文件處于web server的哪個目錄下
PS:實踐過程中發(fā)現(xiàn)一個問題,就是切片之后,最后一段ts不管是不是10s,同樣會按10s去切,導(dǎo)致切好后的視頻最后一段會加上去幾秒的黑段來補足10s,原因是segmenter.c中的程序?qū)懙倪€有缺陷,修正方法如下:
https://github.com/johnf/m3u8-segmenter/pull/10/files
參考:
http://blog.chinaunix.net/uid-25530360-id-3483535.html
http://shappy1978.iteye.com/blog/1071815
http://www.ioncannon.net/programming/452/iphone-http-streaming-with-ffmpeg-and-an-open-source-segmenter/
http://hi.baidu.com/lphack/item/83865611c5f82c8988a956df
http://blog.cnrainbird.com/index.php/2012/03/25/ping_guo_http_shi_pin_dian_bo_ji_shu/
如何編譯m3u8_segmenter
2014-03-20 19:30?535人閱讀?評論(0)?收藏?舉報最近研究hls,發(fā)現(xiàn)需要使用m3u8_segmenter這個程序,搞了半天才編譯通過,特此記錄一下。
使用正常的./configure等編譯老是出錯誤
gcc?-Wall?-g?m3u8-segmenter.c?-o?segmenter?-lavformat?-lavcodec?-lavutilm3u8-segmenter.c:?In?function?‘add_output_stream’:
m3u8-segmenter.c:58:?warning:?implicit?declaration?of?function?‘avformat_new_stream’
m3u8-segmenter.c:58:?warning:?assignment?makes?pointer?from?integer?without?a?cast
m3u8-segmenter.c:?In?function?‘main’:
m3u8-segmenter.c:343:?warning:?implicit?declaration?of?function?‘avformat_find_stream_info’
m3u8-segmenter.c:380:?error:?‘AVFMT_FLAG_IGNDTS’?undeclared?(first?use?in?this?function)
m3u8-segmenter.c:380:?error:?(Each?undeclared?identifier?is?reported?only?once
m3u8-segmenter.c:380:?error:?for?each?function?it?appears?in.)
m3u8-segmenter.c:390:?warning:?implicit?declaration?of?function?‘avcodec_open2’
m3u8-segmenter.c:397:?error:?‘AVIO_FLAG_WRITE’?undeclared?(first?use?in?this?function)
?
在網(wǎng)上查資料,特別是參考xjd_1985的文章,m3u8-segmenter工程編譯和使用
?
./configure?FFMPEG_CFLAGS=-I../ffmpeg-2.1.1??FFMPEG_LIBS='-L?../ffmpeg-2.1.1/libavformat?-L../ffmpeg-2.1.1/libavcodec?-L../ffmpeg-2.1.1/libavutil?-pthread?-lavformat?-lavcodec?-lavutil?-lm?-lz'
?
?
ffmpeg-2.1.1是我使用ffmpeg的版本和目錄
然后再make就通過了。
總結(jié)
以上是生活随笔為你收集整理的HLS协议及java切片相关的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Makefile 中:= ?= += =
- 下一篇: centos ffmpeg m3u8切片