免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4211 | 回复: 0
打印 上一主题 下一主题

ffmpeg编译及使用 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-11-14 23:38 |只看该作者 |倒序浏览

ffmpeg编译及使用
1 ffmpeg介绍
ffmpeg是音视频的分离,转换,编码解码及流媒体的完全解决方案,其中最重要的就是libavcodec库。它被mplayer或者xine使用作为解码器。还有,国内比较流行的播放器影音风暴或MyMPC的后端ffdshow也是使用ffmpeg的解码库的。
ffmpeg软件包经编译过后将生成三个可执行文件,ffmpeg,ffserver,ffplay。其中ffmpeg用于对媒体文件进行处理,ffserver是一个http的流媒体服务器,ffplay是一个基于SDL的简单播放器。
ffmpeg中有五个库文件,libavcodec,libavformat,libavutil,libswscale,libpostproc,其中库libavcodec,libavformat用于对媒体文件进行处理,如格式的转换;libavutil是一个通用的小型函数库,该库中实现了CRC校验码的产生,128位整数数学,最大公约数,整数开方,整数取对数,内存分配,大端小端格式的转换等功能;libswscale,libpostproc暂时不知道何用。
2 ffmpeg下载
最新的ffmpeg可以通过svn下载,SVN辅助的软件有:
SubVersion,从 http://subversion.tigris.org/ 下载,支持linux。
TortoiseSVN,从 http://tortoisesvn.tigris.org/ 下载,是很不错的SVN客户端程序,为windows外壳程序集成到windows资源管理器和文件管理系统的Subversion客户端,用起来很方便。
subversion安装,记住最好之前装过apr和apr-util,在apache.org网站能下到
wget http://subversion.tigris.org/downloads/subversion-1.3.2.tar.gz
tar zvxf subversion-1.3.2.tar.gz
cd subversion-1.3.2
./configure --with-apr=/usr/local/apr-httpd --with-apr-util=/usr/local/apr-util-httpd/
make
make install
如果安装了FC6,它已经带了svn,不用装了。
ffmpeg的下载:我们就可以通过svn命令获取最新的ffmpeg,命令如下:
svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

3 ffmpeg支持库的安装
* xvid
xvid的获取地址如下:
http://www.xvid.org/
wget http://downloads.xvid.org/downloads/xvidcore-1.1.3.tar.gz
配置编译
   for x86
#./configure --prefix=/usr/local
#make
#make install
   for arm
#CC=arm-linux-gcc ./configure --prefix=/usr/local/arm/arm-linux --build=i686-pc-linux --host=arm-linux --target=arm-linux
#make
#make install
* x264
x264的获取地址如下:
svn co svn://svn.videolan.org/x264/trunk x264
配置编译
   for x86
#./configure --enable-shared --prefix=/usr/local
#make
#make install
   for arm
#CC=arm-linux-gcc ./configure --enable-pthread --enable-shared --host=arm-linux
--prefix=/usr/local/arm/arm-linux
#make
#make install
* 支持mp3
lame的获取地址如下: http://lame.sourceforge.net/index.php
配置编译
   for x86
./configure --enable-shared --prefix=/usr/local
* 支持Ogg Vorbis:
* AC3和dts编码的支持
libdts编译参数
./configure --prefix=/usr
make
make install
* mpg4 aac格式支持,如果ffserver服务器还针对手机用户服务,所以,类似aac,mpg4铃声格式的支持,我们也得做。这里我们安装faad2和faac就行,下载请到http://www.audiocoding.com/modules/mydownloads/,http://prdownloads.sourceforge.net/faac
   FAAD2的编译
cd faad2
autoreconf -vif
./configure --prefix=/usr --with-mp4v2 --enable-shared
make
make install
   faac的编译
cd faac
chmod +x bootstrap
./bootstrap
./configure --prefix=/usr --with-mp4v2 --enable-shared
make
make install
在编译ffmpeg,在configure时加上--enable-amr_nb --enable-faad --enable-faac参数。
* 支持3gp格式,这也是现在好多手机支持的格式,所以也得支持编译
编译的时候加上--enable-amr_nb --enable-amr_wb参数就行,根据编译系统的提示,所以我们得下载一些编译3gp所需得文件。
源码网址:http://www.3gpp.org/ftp/Specs
wget http://www.3gpp.org/ftp/Specs/archive/26_series/26.204/26204-510.zip
解压以后把里面的文件都拷贝到libavcodec/amrwb_float
wget http://www.3gpp.org/ftp/Specs/archive/26_series/26.104/26104-510.zip
解压以后把里面的文件都拷贝到libavcodec/amr_float
* ffmpeg支持VC1格式
    微软ASF格式的三个版本,WMV1,WMV2,WMV3分别对应MediaPlayer的版本7,8和9,所以很多时候会称VC1为WMV3或 WMV9,都是它了,有时候在代码里,也能看到称呼它为VC9的。因为微软还没有正式公开这种格式,所以当前对VC1的支持还很不完善。本文基本是根据Multimedia Mike的一篇博客翻译和完善而来。
    (1) 首先要下载 SMPTE VC-1 reference decoder,这个组织是要收费的,可以从这里下载免费的。
    (2) 在ffmpeg目录下的libavcodec目录下面,建立目录libvc1。
    (3) 将VC1_reference_decoder_release6/decoder/目录中的*.c和*.h文件全部copy到libvc1目录下。
    (4) 将VC1_reference_decoder_release6/shared/目录中的*.c和*.h文件全部copy到libvc1目录下。
    (5) 将 libvc1-makefile.txt放到libvc1下的Makefile文件。
    (6) 将smpte-vc1.c文件放到libavcodec目录下。
    (7) 修改libavcodec目录下的vc9.c,将文件最后的wmv3_decoder这个AVCodec的structure,用#if 0和#endif包含起来,也就是使它失效了。
    (8) 修改libavcodec目录下的allcodecs.c,将register_avcodec(&wmv3_decoder)上下的注释去掉,使它发挥作用。
    (9) 修改libavcodec目录下的Makefile,把OBJS的列表中加入smpte-vc1.o。
    (10)修改ffmpeg主目录下的Makefile文件,把-L./libavcodec/libvc1 -lvc1$(BUILDSUF)加入到FFLIBS后面。
    (11) 进入ffmpeg/libavcodec/libav1,执行make
    (12) 到ffmpeg主目录下,执行config;make;make install。config时根据实际情况带参数。

* 采用ffmpeg转码制作FLV文件的方法
    采用ffmpeg转码制作FLV文件,和转码成其它媒体类型的重要差别是一定要有lame库支持,因为FLV的声音编码采用mp3格式,非lame这个东东不行。编译ffmpeg中加入lame库真是一场灾难,特别在windows下,很多参数都不能发挥作用,最后直接手工copy和改一些文件,记录如下:
    (1) 如果在Windows下编译,第一步当然是下载MinGW和MSYS来装上了。到http://mingw.sourceforge.net/去下载最新版的MinGW-5.0.2.exe和MSYS-1.0.11-2004.04.30-1.exe。
    (2) 先安装MinGW,直接运行MinGW-5.0.2.exe安装,选择目录,譬如选择D:\MinGW为安装目录。安装时需要选择gcc和make模块,安装文件本身很小,会从网上下载模块来安装。
    (3) 然后安装MSYS,也是直接运行MSYS-1.0.11-2004.04.30-1.exe安装。安装目录一般选择D:\MinGW\bin \1.0。,安装过程会询问刚才安装MinGW的目录,输入D:\MinGW,其它都回答'Y'就搞定了。如果不清楚,可以看这个图片效果。
    (4) 运行MSYS,桌面上有个图标,双击就运行了,运行结果是一个模拟unix的命令窗口,后面的编译都在这种状态下进行。前面4步在linux不需要。
    (5) 到http://lame.sourceforge.net/去下载最新版的lame-3.97b2.tar.gz,copy到你认为合适的地方,解压后进入lame解压出来的目录中。执行
        ./configure --prefix=PREFIX
        make
        make install
    (6) 就把编译出来的include下的lame目录copy到/usr/include目录下,把lib下的几个库文件都copy到/usr/lib目录 下。这里注意有个变化,如果只copy lib目录下的静态库到/usr/lib下,就是只copy libmp3lame.a文件,编译出来的ffmpeg最终就不会对libmp3lame的动态库有依赖关系,这是因为编译首先找动态库,动态库没有才找 静态库。如果不做这个copy,后面编译ffmpeg时无论如何指定参数,都会报错LAME not found,不知道是哪里的bug。
    (7) 从http://ffmpeg.mplayerhq.hu/取得最新的ffmpeg,现在自由软件都大量采用SVN了,要先装一个SVN,可以去http://tortoisesvn.tigris.org/下载windows版的SVN,去http://subversion.tigris.org/下载linux版的SVN。SVN如何编译安装这里就省略了。
    (8) 如果在windows下,打开解压后的ffmpeg目录下的Makefile文件,在FFLIBS的那一行后面加上-lmp3lame$(BUILDSUF)。这个也不知道是哪个bug引起的,搞了好长时间才搞出来,郁闷。Linux下不用这样。
4 ffmpeg的编译
配置编译
for x86
#./configure --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
make
make install
补充1:
关于3gp的编译,如果大家要编译--enable-amr_nb-fixed,那就不能跟--enable-amr_nb同时编译,我不大清楚这两者到底有什么区别,似乎
fixed是修正版,管他呢,编译的方法:
wget http://www.3gpp.org/ftp/Specs/archive/26_series/26.073/26073-510.zip
解压以后把里面的文件都拷贝到libavcodec/amr目录下
修改libavcodec/amr/makefile 找到CFLAGS = -Wall -pedantic-errors -I. $(CFLAGS_$(MODE)) -D$(VAD) 换成CFLAGS = -Wall -I.
$(CFLAGS_$(MODE)) -D$(VAD) -DMMS_IO
整体编译参数就是
#./configure --prefix=/usr --enable-gpl --enable-shared --enable-mp3lame --enable-amr_nb-fixed --enable-amr_wb --enable-amr_if2 --enable-libogg --enable-vorbis --enable-xvid --enable-a52 --enable-a52bin --enable-dts --enable-pp --enable-faad --enable-faadbin --enable-faac --enable-x264 --enable-pthreads --disable-ffserver --disable-ffplay
make
make install
for x86的简易配置
#./configure --prefix=./install --disable-shared --enable-pthreads --enable-libx264 --enable-libxvid --arch=i686 --enable-gpl
#make
#make install
for arm
配置编译
#./configure --prefix=/home/zht/redhatzht/sources/image-colletct/ffmpeg/install --enable-static --disable-shared --enable-libx264 --enable-libxvid --cross-compile --cc=arm-linux-gcc --arch=arm --enable-gpl --disable-strip --disable-network --disable-ipv6 --disable-vhook --disable-audio-beos --disable-audio-oss --disable-mpegaudio-hp --enable-pthreads --enable-small --disable-parsers --disable-debug
#make
#make install
注意:
(1)“/home/zht/redhatzht/sources/image-colletct/ffmpeg”为ffmpeg源码所在目录。
(2)“/usr/local/arm”为arm-linux-gcc交叉编译器所在目录。
(3) 如果库文件安装在/usr/local/lib目录中导致配置失败,可以在/etc/ld.so.conf文件中添加/usr/local/lib目录,然后执行#ldconfig。
    x86上的ldconfig不能在arm上运行,arm上的ldconfig工具是在建立交叉编译器时,编译glibc是产生的,可以拷贝到arm-linux中。
(4) 本文大部分内容来自网络,其中xvid,x264的库,我亲手安装过,ffmpeg的配置编译for x86的简易配置,for arm,我亲手配置编译过,并在x86,arm上可用,编译配置都是采用静态库。
5 ffmpeg用法
ffmpeg作为媒体文件处理软件,基本用法如下:
ffmpeg -i INPUTfile [OPTIONS] OUTPUTfile
输入输出文件通常就是待处理的多媒体文件了。可以是纯粹的音频文件,纯粹的视频文件,或者混合的。ffmpeg支持绝大部分的常见音频,视频格式,象常见的各种mpeg,AVI封装的DIVX和Xvid等等,具体的格式支持列表可以使用ffmpeg -formats查看或直接查阅文档。
另外,由于Linux把设备视为文件,因此-i选项后可以跟设备名。比如DV,视频卡,光驱或者其它的各类设备。输出的内容通过
Options调整,其主要的选项如下:
-vcodec 视频流编码方式
-b 视频流码率(默认只有200k,一般都需要手动设置,具体的数值视codec选择而定)
-r 视频流帧数(一般说来PAL制式通常用25,NTSC制式通常用29)
-s 视频解析度(分辨率,也要视codec和你的需要而定。另:具体写法使用“数字x数字”的形式)
-t 处理持续时间。
-acodec 音频流编码方式
-ab 音频流码率(默认是同源文件码率,也需要视codec而定)
-ar 音频流采样率(大多数情况下使用44100和48000,分别对应PAL制式和NTSC制式,根据需要选择)
-vn 屏蔽视频流
-an 屏蔽音频流
-author 设置媒体文件的作者
-title 设置媒体文件的题目
-f 强制使用某种格式
-target type 使用预置的格式转换(可以转成dvd,vcd或svcd)
除此之外还有些更高级的选项,如设定vbr,或设定high quality,或者设定vbr的buff和max/min码率,象一般我们自用的dvd抓轨啦,DV转vcd dvd啦,网上下载的电影转成vcd或dvd都不一定需要用到它们。
常用命令选项说明
-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测试
5 example
(1) ffmpeg的使用
"Video and Audio grabbing"
FFmpeg can use a video4linux compatible video source and any Open Sound System audio source:
        ffmpeg /tmp/out.mpg
Note that you must activate the right video source and channel before launching ffmpeg. You can use any TV viewer such as
xawtv (http://bytesex.org/xawtv/>) by Gerd Knorr which I find very good. You must also set correctly the audio recording
levels with a standard mixer.
"Video and Audio file format conversion"
* You can input from YUV files:
        ffmpeg -i /tmp/test%d.Y /tmp/out.mpg
The Y files use twice the resolution of the U and V files. They are raw files, without header. They can be generated by all decent video decoders. You must specify the size of the image with the -s option if ffmpeg cannot guess it.
* You can input from a RAW YUV420P file:
        ffmpeg -i /tmp/test.yuv /tmp/out.avi
The RAW YUV420P is a file containing RAW YUV planar, for each frame first come the Y plane followed by U and V planes, which are half vertical and horizontal resolution.
* You can output to a RAW YUV420P file:
        ffmpeg -i mydivx.avi -o hugefile.yuv
* You can set several input files and output files:
        ffmpeg -i /tmp/a.wav -s 640x480 -i /tmp/a.yuv /tmp/a.mpg
Convert the audio file a.wav and the raw yuv video file a.yuv to mpeg file a.mpg
* You can also do audio and video conversions at the same time:
        ffmpeg -i /tmp/a.wav -ar 22050 /tmp/a.mp2
Convert the sample rate of a.wav to 22050 Hz and encode it to MPEG audio.
* You can encode to several formats at the same time and define a mapping from input stream to output streams:
        ffmpeg -i /tmp/a.wav -ab 64 /tmp/a.mp2 -ab 128 /tmp/b.mp2 -map 0:0 -map 0:0
Convert a.wav to a.mp2 at 64 kbits and b.mp2 at 128 kbits. "-map file:index" specify which input stream is used for each output stream, in the order of the definition of output streams.
* You can transcode decrypted VOBs
        ffmpeg -i snatch_1.vob -f avi -vcodec mpeg4 -b 800 -g 300 -bf 2 -acodec mp3 -ab 128 snatch.avi
This is a typical DVD ripper example, input from a VOB file, output to an AVI file with MPEG-4 video and MP3 audio, note that in this command we use B frames so the MPEG-4 stream is DivX5 compatible, GOP size is 300 that means an INTRA frame every 10
seconds for 29.97 fps input video. Also the audio stream is MP3 encoded so you need LAME support which is enabled using --enable-mp3lame when configuring. The mapping is particularly useful for DVD transcoding to get the desired audio language.
NOTE: to see the supported input formats, use ffmpeg -formats.
(2) ffplay的使用
ffplay - FFplay media player
SYNOPSIS
ffplay [options] input_file
DESCRIPTION
FFplay is a very simple and portable media player using the FFmpeg libraries and the SDL library. It is mostly used as a test bench for the various APIs of FFmpeg.
OPTIONS
"Main options"
show help force displayed width force displayed height disable audio disable video disable graphical display force format
"Advanced options"
show the stream duration, the codec parameters, the current position in the stream, and the audio/video synchronisation drift. force RTP/TCP protocol usage instead of RTP/UDP. It is only meaningful if you are doing stream with the RTSP protocol.
set the master clock to audio ( type=audio), video ( type=video) or external ( type=ext). Default is audio. The master clock is used to control audio-video synchronization. Most media players use audio as master clock, but in some cases (streaming or high quality broadcast) it is necessary to change that. This option is mainly used for debugging purposes.
(3) ffserver的使用
ffsserver - FFserver video server
SYNOPSIS
ffserver [options]
DESCRIPTION
FFserver is a streaming server for both audio and video. It supports several live feeds, streaming from files and time shifting on live feeds (you can seek to positions in the past on each live feed, provided you specify a big enough feed
storage in ffserver.conf).
This documentation covers only the streaming aspects of ffserver / ffmpeg. All questions about parameters for ffmpeg, codec questions, etc. are not covered here. Read ffmpeg-doc.html for more information.
OPTIONS
print the license print the help use configfile instead of /etc/ffserver.conf
6 其他(参考)
(1)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


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/64106/showart_1415687.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP