您的位置:首页 > 其它

ffmpeg+mencoder环境搭建和视频处理总结

2007-08-16 09:56 405 查看
ffmpeg+mencoder几乎可以完成目前基于web的播客平台任何音视频处理的操作.如果还需要添加一些什么的话,那么就是视频在线录制功能了,这个也可以用ffmpeg+fms来完成,因此一般的类似于YouTube的一些可见功能都可以在ffmpeg+mencoder+fms来做后台实现.由于fms没有实践,因此这里不描述.
本文档有三部分:
1)ffmpeg+mencoder环境搭建
2)常见操作说明
3)个人的一些使用心得

1.ffmpeg+mencoder环境搭建

1)概论
音视频界众多的编解码协议和各个公司定义的专用格式导致目前的视频音频文件纷繁复杂,单纯的ffmpeg支持的格式并不完全包括所有种类,至少swf,rmvb(rv4)目前的版本是不支持的.同时wma9似乎可以支持了.但没有测试.同时mencoder能支持rm,rmvb等格式,但是从视频中获取某帧截图的工作只能由ffmpeg完成.因此可以采用ffmpeg+mencoder完成目前所有流行格式的视频压缩转换,设置视频信息,截取视频中的图片等功能了,同时,采用其他的一些开源工具如MediaInfo可以获取视频的元数据信息.

2)ffmpeg篇
首先获取软件包:ffmpeg,lame(支持mp3),ogg vorbis,x264(h264 codec),xvid,3gp,libdts,mpeg4 aac.这些软件包在71.21的/home/zhengyu/tools里面都能找到.如果需要网上下载的话,可以提供下载地址.
ffmpeg官网下载:http://ffmpeg.mplayerhq.hu...
如果官网下载有问题的,xplore也提供了1月30的snapshot:下载ffmpeg.

lame下载:当前版本为3.97,http://sourceforge.net/pro...
或者到xplore下载lame.
ogg vorbis:这个一般的redhat自带,不需要下载.可以去看看/usr/lib/libvorbis.a在不在,如果不在可以yum install或apt-get install.
xvid下载:http://downloads.xvid.org/..., xplore下载xvid.
x264下载:这个可以去ftp://ftp.videolan.org/下寻找最近的snapshot下载,或者svn获取,注意如果ffmpeg是什么时候的,x264的snapshot也应该是什么时候的,不然编译的时候容易报错.ftp://ftp.videolan.org/pub...
xplore下载x264的1月29日的snapshot.
libdts:http://download.chinaunix...., xplore下载libdts:
上面的软件包除了ffmpeg之外,在下载完成后解包,编译的参数都是./configure --prefix=/usr --enable-shared;make;sudo make install

mpeg4 aac/aad2:http://www.audiocoding.com...,http://www.audiocoding.com...
faac和faad2在下载解包之后需要自己automake生成编译文件.其中faac的1.25版本需要将内置的configure.in文件最后的AM_OUTPUT中的几个续行去掉,并取消分行.然后按照bootstrap里面的操作进行,无非是aclocal -I .;autoheader;libtoolize --automake;automake -a --copy;autoconfig(或者前面的由autoreconf -vif替代);./configure --prefix=/usr --enable-shared;make;sudo make install;
faad2的2.5版本需要修改内置的configure.in文件,不然会在没有libbmp时编译会通不过.找到configure.in中下面一段:

引用
if test x$WITHBMP = xyes; then
AC_DEFINE([HAVE_BMP], 1, [User wants beep media player plugin built])
AM_CONDITIONAL([HAVE_XMMS], true)
AM_CONDITIONAL([HAVE_BMP], true)
fi

if test x$WITHDRM = xyes; then
改成
if test x$WITHBMP = xyes; then
AC_DEFINE([HAVE_BMP], 1, [User wants beep media player plugin built])
AM_CONDITIONAL([HAVE_XMMS], true)
AM_CONDITIONAL([HAVE_BMP], true)
else
AC_MSG_NOTICE(no bmp build configured)
AM_CONDITIONAL([HAVE_BMP], false)
fi

if test x$WITHDRM = xyes; then

然后autoreconf -vif;./configure --prefix=/usr --enable-shared;make;sudo make install;
这里提供两个已经修改好的.只需要make clean;make;sudo make install;的tar包.faac1.25下载,faad2.5下载.

3gp支持:在编译ffmpeg加入--enable-amr_nb --enable-amr_wb的时候,会有提示,下载:http://www.3gpp.org/ftp/Sp...,解压的源代码文件以后把里面的文件都拷贝到ffmpeg的源代码目录下libavcodec/amrwb_float;然后下载:http://www.3gpp.org/ftp/Sp...,解压得源代码文件以后把里面的文件都拷贝到ffmpeg解包目录下的libavcodec/amr_float,然后交给ffmpeg编译去做了.
也可以在这里下载这两个包:amrwb_float下载,amr_float下载.

这些codec都安装完毕之后便可以编译ffmpeg了,编译参数如下:
./configure --prefix=/usr/local --enable-gpl --enable-shared --enable-mp3lame --enable-amr_nb --enable-amr_wb --enable-amr_if2 --enable-libogg --enable-vorbis --enable-xvid --enable-a52 --enable-a52bin --enable-faadbin --enable-dts --enable-pp --enable-faad --enable-faac --enable-x264 --enable-pthreads --disable-ffserver --disable-ffplay
make
(sudo make install)
然后就可以尝试用ffmpeg做视频转换截图了.
当然,上面那些codec的编译是相当乏味的.你也可以根本不理会这些.因为最新版本的ffmpeg已经比以前支持更多的格式了.因此也可以只安装lame,xvid,x264就可以了.下面是两种配置下支持的格式diff -w的结果:

引用
2c2
< configuration: --prefix=/usr --enable-gpl --enable-shared --enable-mp3lame --enable-amr_nb --enable-amr_wb --enable-amr_if2 --enable-libogg --enable-vorbis --enable-xvid --enable-a52 --enable-a52bin --enable-faadbin --enable-dts --enable-pp --enable-faad --enable-faac --enable-x264 --enable-pthreads --disable-ffserver --disable-ffplay
---
> configuration: --prefix=/usr/local --enable-mp3lame
6c6
< built on Jan 30 2007 17:55:22, gcc: 4.1.1 20061011 (Red Hat 4.1.1-30)
---
> built on Jan 30 2007 17:46:19, gcc: 4.1.1 20061011 (Red Hat 4.1.1-30)
40c40
< DE gxf GXF format
---
> D gxf GXF format
72c72
< DE ogg Ogg Vorbis
---
> D ogg Ogg
111d110
< DEA aac
113c112
< DEA ac3
---
> EA ac3
132,133d130
< DEA amr_nb
< DEA amr_wb
147d143
< D A dts
164c160
< DEV DT h264
---
> D V DT h264
190d185
< D A mpeg4aac
275d269
< EV xvid

3)mencoder篇
首先获取mplayer软件包极其mplayer官网上自带的codecs.如果喜欢mplayer,也可以下载gui和font.关于mplayer-1.0rc1在71.21的/home/zhengyu/tools中能找到.如果需要网上下载,可以去官网:http://www.mplayerhq.hu/de...下载rc1地址如下:http://www1.mplayerhq.hu/M...最新的svn版本:http://www1.mplayerhq.hu/M...官网同时也给出了一些codec,其中就有rm格式的codec:http://www1.mplayerhq.hu/M...
xplore也提供下载,mplayer1.0rc1下载,codec下载.

下载完成之后,将tar vxjf essential-20061022.tar.bz2;sudo mkdir -p /usr/lib/codecs;sudo cp -rf essential-20061022/* /usr/lib/codecs;然后解包mplayer,按如下方式编译:

./configure --prefix=/usr/local --enable-gui --enable-largefiles --enable-gif --enable-png --enable-jpeg --language=zh_CN --with-codecsdir=/usr/lib/codecs/
make
(sudo make install)

然后就可以使用mencoder,当然也有一个没有gui的mplayer可以播放各种视频了.不过我们需要的是mencoder.至此,ffmpeg+mencoder搭建完成.

2.常见操作说明
对于ffmpeg,可以将除swf,rmvb,wmav9以外的视频/音频格式转换成flv/mp3,同时可以截取这些视频文件中的某个时间的该帧图片.这些实际上就是一个视频播客显示的部分.对于mencoder,支持各种常见格式的视频/音频转换成flv/mp3.或者转换成avi.
1)ffmpeg篇:
ffmpeg的命令行参数因为太多,这里不列出,可以用ffpmeg -h可以查看到.列出非高级参数如下:

引用
Main options:
-L show license
-h show help
-version show version
-formats show available formats, codecs, protocols, ...
-f fmt force format
-i filename input file name
-y overwrite output files
-t duration set the recording time
-fs limit_size set the limit file size
-ss time_off set the start time offset
-itsoffset time_off set the input ts offset
-title string set the title
-timestamp time set the timestamp
-author string set the author
-copyright string set the copyright
-comment string set the comment
-album string set the album
-v verbose control amount of logging
-target type specify target file type ("vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", ...)
-dframes number set the number of data frames to record
-scodec codec force subtitle codec ('copy' to copy stream)
-newsubtitle add a new subtitle stream to the current output stream
-slang code set the ISO 639 language code (3 letters) of the current subtitle stream

Video options:
-vframes number set the number of video frames to record
-r rate set frame rate (Hz value, fraction or abbreviation)
-s size set frame size (WxH or abbreviation)
-aspect aspect set aspect ratio (4:3, 16:9 or 1.3333, 1.7777)
-croptop size set top crop band size (in pixels)
-cropbottom size set bottom crop band size (in pixels)
-cropleft size set left crop band size (in pixels)
-cropright size set right crop band size (in pixels)
-padtop size set top pad band size (in pixels)
-padbottom size set bottom pad band size (in pixels)
-padleft size set left pad band size (in pixels)
-padright size set right pad band size (in pixels)
-padcolor color set color of pad bands (Hex 000000 thru FFFFFF)
-vn disable video
-vcodec codec force video codec ('copy' to copy stream)
-sameq use same video quality as source (implies VBR)
-pass n select the pass number (1 or 2)
-passlogfile file select two pass log file name
-newvideo add a new video stream to the current output stream

Subtitle options:
-scodec codec force subtitle codec ('copy' to copy stream)
-newsubtitle add a new subtitle stream to the current output stream
-slang code set the ISO 639 language code (3 letters) of the current subtitle stream

Audio/Video grab options:
-vd device set video grab device
-vc channel set video grab channel (DV1394 only)
-tvstd standard set television standard (NTSC, PAL (SECAM))
-ad device set audio device
-grab format request grabbing using
-gd device set grab device

Advanced options:
-map file:stream[:syncfile:syncstream] set input stream mapping
-map_meta_data outfile:infile set meta data information of outfile from infile
-benchmark add timings for benchmarking
-dump dump each input packet
-hex when dumping packets, also dump the payload
-re read input at native frame rate
-loop_input loop (current only works with images)
-loop_output number of times to loop output in formats that support looping (0 loops forever)
-threads count thread count
-vsync video sync method
-async audio sync method
-vglobal video global header storage type
-copyts copy timestamps
-shortest finish encoding within shortest input
-dts_delta_threshold timestamp discontinuity delta threshold
-ps size set packet size in bits
-muxdelay seconds set the maximum demux-decode delay
-muxpreload seconds set the initial demux-decode delay

如果这些都明白了,并且有编程基础,你就可以去参与ffmpeg开发了.其实这些堆积起来的命令95%一般是用不上的.这里介绍一些简单的常见的命令:
-fromats 显示可用的格式
-f fmt 强迫采用格式fmt
-I filename 输入文件
-y 覆盖输出文件
-t duration 设置纪录时间 hh:mm:ss[.xxx]格式的记录时间也支持(截图需要)
-ss position 搜索到指定的时间 [-]hh:mm:ss[.xxx]的格式也支持
-title string 设置标题
-author string 设置作者
-copyright string 设置版权
-comment string 设置评论
-target type 设置目标文件类型(vcd,svcd,dvd),所有的格式选项(比特率,编解码以及缓冲区大小)自动设置,只需要输入如下的就可以了:ffmpeg -i myfile.avi -target vcd /tmp/vcd.mpg
-hq 激活高质量设置

-b bitrate 设置比特率,缺省200kb/s
-r fps 设置帧频,缺省25
-s size 设置帧大小,格式为WXH,缺省160X128.下面的简写也可以直接使用:Sqcif 128X96 qcif 176X144 cif 252X288 4cif 704X576
-aspect aspect 设置横纵比 4:3 16:9 或 1.3333 1.7777
-croptop/botton/left/right size 设置顶部切除带大小,像素单位
-padtop/botton/left/right size 设置顶部补齐的大小,像素单位
-padcolor color 设置补齐条颜色(hex,6个16进制的数,红:绿:蓝排列,比如 000000代表黑色)
-vn 不做视频记录
-bt tolerance 设置视频码率容忍度kbit/s
-maxrate bitrate设置最大视频码率容忍度
-minrate bitreate 设置最小视频码率容忍度
-bufsize size 设置码率控制缓冲区大小
-vcodec codec 强制使用codec编解码方式. 如果用copy表示原始编解码数据必须被拷贝.(很重要)

-ab bitrate 设置音频码率
-ar freq 设置音频采样率
-ac channels 设置通道,缺省为1
-an 不使能音频纪录
-acodec codec 使用codec编解码

-vd device 设置视频捕获设备,比如/dev/video0
-vc channel 设置视频捕获通道 DV1394专用
-tvstd standard 设置电视标准 NTSC PAL(SECAM)
-dv1394 设置DV1394捕获
-av device 设置音频设备 比如/dev/dsp

-map file:stream 设置输入流映射
-debug 打印特定调试信息
-benchmark 为基准测试加入时间
-hex 倾倒每一个输入包
-bitexact 仅使用位精确算法 用于编解码测试
-ps size 设置包大小,以bits为单位
-re 以本地帧频读数据,主要用于模拟捕获设备
-loop 循环输入流。只工作于图像流,用于ffserver测试

ffmpeg进行操作的常用方法:

1.转换成flv文件:ffmpeg -i infile.* -y (-ss second_offset -ar ar -ab ab -r vr -b vb -s vsize) outfile.flv
其中second_offset是从开始的多好秒钟.可以支持**:**:**格式,至于ar,ab是音频的参数,可以指定ar=22050,24000,44100(PAL制式),48000(NTSC制式),后两种常见,ab=56(视音频协议的codec而定,如果要听高品质,则80以上).vr,vb,vsize是视频参数,可以指定vr=15,25(PAL),29(NTSC),vb=200,500,800,1500(视视频协议的codec而定,可以通过查看专业的codec说明文档获取,如果你手头有一份详细的各种codec的文档,请提供一份给我,不胜感激.)
还有一些参数-acodec ac -vcodec vc(ac指定音频codec,ar和ab可以省去,vc指定视频codec,vr和vb可以省去,自动采用相应的codec参数)
还有很多高级参数,如-qmin,-qcale等,请查看详细文档.
还有-an和-vn参数,分别从多媒体文件中提取出纯粹视频和音频.
另,如果你是用shell批量处理,请使用-y参数覆盖生成flv.

2.截取图片:ffmpeg -i infile.* -y (-ss second_offset) -t 0.001 -s msize (-f image_fmt) outfile.jpg
其中second_offset同上,msize同vsize,图片大小.image_fmt=image2强制使用jpg,image_fmt=gif,强制使用gif格式.
还可以用-vframes fn指定截取某帧图片,fn=1,2,3,...

2)mencoder篇:
mencoder的作用主要在视频转码方面.在安装完mplayer后,mencoder也编译生成了.可以man mencoder获取mencoder的说明文档.
mencoder的参数更加复杂,不过也无非是音频处理视频处理两个方面,可以参看网络例子:http://www.masoncn.com/pos...这里不作详细的列举了.

mencoder进行操作的常用方法: mencoder infile.* -o outfile.* [-ovc 目标视频格式] [-oac 目标音频格式] [-of 目标文件格式]

1.转换成flv文件: mencoder infile.* -o outfile.flv -of lavf -oac mp3lame -lameopts abr:br=56 -ovc lavc -lavcopts vcodec=flv:vbitrate=150:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -srate 22050
mencoder infile.rmvb -o outfile.flv -vf scale=-3:150 -ofps 12 -oac mp3lame -ovc xvid -xvidencopts bitrate=112

2.转换成avi文件: mencoder infile.* -o outfile.avi -of avi -oac mp3lame -lameopts preset=64 -ovc xvid -xvidencopts bitrate=600

3.转换成wmv文件(复杂写法,其中高级参数可以省去): mencoder infile.* -o outfile.wmv -of lavf -ofps 25 -oac mp3lame -lameopts cbr:preset=128 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=768:mbd=2:mv0:trell:v4mv:cbp:last_pred=3 -vf scale=320:240 -srate 22050 -sws 9 -subcp cp936 -subpos 0 -subalign 0 -subfont-text-scale 3 -lavfopts i_certify_that_my_video_strea

其中-ovc,-oac和-of是必须的,-ovc是指定视频codec,指定了ovc之后通常带一个该codec的opt参数,-oac是指定音频codec,也会在其后带一个codec的opt参数.可以指定细节以决定视频音频质量和转换速率.具体的细节可以参看专业的技术文档.

3.个人的一些心得
在视频播客网站上,对于音视频本身一般存在如下几个问题:
1)有些格式的音视频文件不支持.不能做到全的问题.
2)上传的同样内容,但不同格式音视频排重的问题.从存储和应用两个方面描述这个问题会有不同层次的解决方案.
3)对于某些格式的音视频文件,既有可能是纯粹音频的,也可以是纯粹视频的.如rm格式.怎样区分这种文件以方便应用的问题.
4)音视频检索,视频描述能不能做到内容方式而不是用户定义关键字的方式.
5)音视频相似度的利用和处理.
6)音视频文件的下载获取.

对于第一个问题采用ffmpeg+mencoder可以获取一个让人可以接受的解决办法.第三个问题可以在上传之后安装一个前端过滤程序,区分音频文件和视频文件,已经有相应的开源工具和代码做这些事情.对于第二个问题,首先是统一格式,然后计算音视频文件的hash,在存储部分采用分布式CAS技术存储,应用方面构架在CAS之上.而第四个问题,第五个问题有待深入研究.第六个问题可以考虑p2p的方式,不过这个不是太重要.
对于采用shell在ffmpeg+mencoder+MediaInfo环境下处理视频队列和截取视频文件,可以参看这篇文章.

参考文章:在此向这些文章主人致敬:
http://bbs.chinavideo.org/...
http://bbs.hljradio.com/ht...
http://blog.sina.com.cn/u/...
http://blog.chinadaily.com...
http://ken.bokele.com/?CH=...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: