http://www.nginxs.com/linux/459.html

iPhone HTTP Streaming with FFMpeg and an Open Source Segmenter

由于项目需要,需要搭建一个手机流媒体服务器,之前有做过流媒体服务器,不过都是PC客户端的•首选的是 flash media server。但是这次客户端是手机,手机客户端的种类很多,Nokia,iphone, android,他们没有一个支持 rtmp协议的而且支持的格式也很少,后来查询google,得知 android 支持rtsp,又是一路的 google 后来锁定在 live555 和 feng stream server,经过试验•用VLC 播放是可以的,在android上rtsp播放就是个废物不给力,也许是我研究的不够透彻,也在网上看其他网站的手机视频,时光网直接给的是http协议mp4•没有做任何处理•这样有一个弊端就是客户端与服务端建立连接一直下载视频,占用服务器带宽和资源,好处是解决各个手机客户端的不兼容问题。就在准备直接使用http 协议播放mp4的时候,有同事提到视频切片,就在google 上搜索,搜索到 一篇老外的文章,通过ffmpeg把视频转码,转成TS流文件,而后通过开源的 segmenter 把流文件切割成几个TS流文件,生成一个播放列表维护切成的散文件。经过试验,效果不错•该作者的 ffmpeg

1. 我只需要支持 mp4所以我编译ffmpeg 只需要添加几个库•如果你需要支持的视频格式多请参考


http://www.nginxs.com/linux/57.html
wget http://downloads.sourceforge.net/project/lame/lame/3.98.4/lame-3.98.4.tar.gz
wget http://www.tortall.net/projects/yasm/releases/yasm-1.1.0.tar.gz
svn checkout http://svn.assembla.com/svn/legend/segmenter/ segmenter
git clone git://git.videolan.org/x264.git

2,安装 ffmpeg需要的库文件

tar jvf faac-1.28.tar.bz2
cd faac-1.28
./configure
make
make install
tar jxvf faad2-2.7.tar.bz2
cd faad2-2.7
./configure
make
make install
tar zxvf yasm-1.1.0.tar.gz
cd yasm-1.1.0
./configure
make
make install
tar zxvf lame-3.98.4.tar.gz
cd lame-3.98.4
./configure --enable-nasm
make
make install
cd x264
./configure
make
make install

3.安装 ffmpeg 和 segmenter


cd ffmpeg-0.5.4
make clean
./configure --enable-gpl --enable-nonfree --enable-pthreads --disable-ffplay --disable-ffserver  --enable-libfaac --enable-libfaad --enable-libmp3lame --enable-libx264
make
make install


cd segmenter
gcc -Wall -g segmenter.c -o segmenter -lavformat -lavcodec -lavutil -lbz2 -lm -lz -lfaac -lmp3lame -lx264 –lfaad


如果你遇到与我一样的错误:

/usr/local/lib/libavcodec.a(pthread.o): In function `avcodec_thread_free':
/home/finney/ffmpeg/libavcodec/pthread.c:95: undefined reference to `pthread_join'
/usr/local/lib/libavcodec.a(pthread.o): In function `avcodec_thread_init':
/home/finney/ffmpeg/libavcodec/pthread.c:159: undefined reference to `pthread_create'
/usr/local/lib/libx264.a(encoder.o): In function `x264_encoder_close':
encoder.c:(.text+0x23ee): undefined reference to `pthread_join'
/usr/local/lib/libx264.a(encoder.o): In function `x264_encoder_frame_end':
encoder.c:(.text+0x34ec): undefined reference to `pthread_join'
/usr/local/lib/libx264.a(encoder.o): In function `x264_encoder_encode':
encoder.c:(.text+0×7053): undefined reference to `pthread_create'
collect2: ld Return 1

请用下面参数编译:


gcc -Wall -g segmenter.c -o segmenter -lavformat -lavcodec -lavutil -lbz2 -lm -lz -lfaac -lmp3lame -lx264 –lfaad -lpthread
mv segmenter /usr/bin/


4.创建转码脚本我就贴老外的参数了,如果大家对参数意思有兴趣我会写下篇我文章解释参数含义:


cat convert.sh
#!/bin/sh
BR=800k

ffmpeg -i $1 -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s 320*240 -vcodec libx264 -b $BR -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 96k -maxrate $BR -bufsize $BR -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 31 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 1 sample_$BR_pre.ts

segmenter sample_$BR_pre.ts 30 sample_$BR stream-$BR.m3u8 http://192.168.1.28/

rm -f sample_$BR_pre.ts


# $1 是input的 要转码的视频 也就是 mp4文件
 # sample_$BR_pre.ts 是生成的 TS 流文件
 # sample_800k-【1…10】.ts 被 segmenter 切片的TS 流文件
 # stream-800k.m3u8 是 segmenter切片的维护列表•我也就就给nginx 或者apache 这个url
 # http://192.168.1.28/ 是 你的 nginx 或者 apache nameserver
 # 最后客户端访问的 url地址是 http://192.168.1.28/stream-800k.m3u8

5. 修改 nginx 或 apache 配置,支持 TS 和 m3u8
Nginx

Vim /usr/local/nginx/conf/mime.types
 
#最后添加
 
application/x-mpegURL                 m3u8;
video/MP2T                                       ts;

apache 修改:
centos :


vim /etc/httpd/conf/httpd.conf
AddType application/x-mpegURL .m3u8
AddType video/MP2T .ts


Ubuntu:


vim /etc/apache2/mods-enabled/mime.conf
AddType application/x-mpegURL .m3u8
AddType video/MP2T .ts


6.转码测试。
在 apache 的根目录 执行脚本

./convert.sh 3333.mp4

# 有的媒体文件可能会提示:


[aac @ 0x8a17960]SBR not implemented. Update your FFmpeg version to the newest one from SVN. If the problem still occurs, it means that your file has a feature which has not been implemented.


他会提示更新,不是致命错误,不会有影响不需要去理会。


ls
3333.mp4  convert.sh  sample_.ts  sample_800k-1.ts  sample_800k-2.ts  sample_800k-3.ts  sample_800k-4.ts  stream-800k.m3u8


7.在iphone 模拟器播放器访问 http://192.168.1.28/stream-96k.m3u8
或者用vlc 播放器

由于截图 截视频会黑屏,所以没办法看到图像,但是我用红字标出了我播放的流是sample_800k-2.ts