个人开发历程知识库

关注C++/Java/C#技术, 致力于安防监控/移动应用/WEB方面开发
------------------------------------ 业精于勤,荒于嬉;行成于思,毁于随
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[转载]ffmpeg的使用

Posted on 2009-05-15 22:47  peterzb  阅读(10528)  评论(1编辑  收藏  举报

 转载自: 视频技术论坛 中华视频网ffmpeg工程组 http://bbs.chinavideo.org/archiver/?fid-10.html

先从ffmpeg开始。

http://ffmpeg.sourceforge.net/上有说明,音视频的分离,转换,解码的完全解决方案。
其中最重要的就是libavcodec库。它被mplayer或者xine使用作为解码器。还有,国内比较流行的播放器影音风暴或MyMPC的后端ffdshow也是使用ffmpeg的解码库的。

ffmpeg包括一组软件,ffmpeg用于对媒体文件进行处理,ffserver是一个http的流媒体服务器,ffplay是一个基于SDL的简单播放器。两个库文件libavcodec和libavformat。

ffmpeg作为媒体文件处理软件,基本用法如下:
ffmpeg -i INPUTfile [OPTIONS] OUTPUTfile
输入输出文件通常就是待处理的多媒体文件了。可以是纯粹的音频文件,纯粹的视频文件,或者混合的。
大部分常见的格式都能够“通杀”。象常见的各种mpeg,AVI封装的DIVX和Xvid等等
具体的格式支持列表可以使用ffmpeg -formats查看或直接查阅文档。

另:由于Linux把设备视为文件,因此-i选项后可以跟设备名。比如DV,视频卡,光驱或者其它的各类设备。
输出的内容通过Options调整。列出几个主要的选项
-vcodec 视频流编码方式
-b       视频流码率(默认只有200k,一般都需要手动设置,具体的数值视codec选择而定)
-r        视频流帧数(一般说来PAL制式通常用25,NTSC制式通常用29)
-s        视频解析度(分辨率,也要视codec和你的需要而定。通常改变某个视频流的解析度是很耗费CPU的事情。另:具体写法使用“数字x数字”的形式。中间是小写字母“x”,这个用过mplayer的应该都知道)
-t        处理持续时间。
-acodec  音频流编码方式
-ab         音频流码率(默认是同源文件码率,也需要视codec而定)
-ar          音频流采样率(大多数情况下使用44100和48000,分别对应PAL制式和NTSC制式,根据需要选择)

还有些可能需要用到的选项如
-vn和-an分别是屏蔽视频流和屏蔽音频流,分别对源文件处理一次即可得到分离的音频和视频
-author -title分别是设置媒体文件的作者和title
-f选项是强制使用某种格式
-target type是使用预置的格式转换(可以转成dvd,vcd或svcd)

除此之外还有些更高级的选项,如设定vbr,或设定high quality,或者设定vbr的buff和max/min码率,象一般我们自用的dvd抓轨啦,DV转vcd或dvd啦,网上下载的电影转成vcd或dvd都不一定需要用到它们。
具体的使用方法在man里面有介绍。简单明了。

少许使用经验:
1:ffmpeg对于rm的处理能力实在不敢恭维。也许是因为我主要使用二进制包安装的缘故,对于Real媒体格式只能处理老式的RV8编码的格式。而且效果不佳。
2:格式转换是一件很耗费CPU资源的事情。虽说ffmpeg已经比WinAVI啦,TmpgEnc这些win下的非专业级视频处理软件做的好些了。毕竟我们可以把ffmpeg运行的时候放到后台。
3:ffmpeg不是万能的,虽说支持的格式很多,但是如果你不是用的最新CVS出来的版本,可能碰上某些古怪的媒体文件就要郁闷。
4:ffmpeg全部是命令行操作。哪位达人写个GUI前端出来就可以让不少菜鸟脱离苦海了。还有就是不能批量处理,但是这个可以用shell帮忙解决。

NAME

ffmpeg - FFmpeg video converter
SYNOPSIS

ffmpeg [[options][-i input_file]]... {[options] output_file}...
DESCRIPTION

If no input file is given, audio/video grabbing is done.

As a general rule, options are applied to the next specified file. For example, if you give the -b 64 option, it sets the video bitrate of the next file. Format option may be needed for raw input files.

By default, ffmpeg tries to convert as losslessly as possible: it uses the same audio and video parameter for the outputs as the one specified for the inputs.
OPTIONS

"Main options"

show license show help show available formats, codecs, protocols, ... force format input file name overwrite output files set the recording time in seconds. hh:mm:ss[.xxx] syntax is also supported. set the title set the author set the copyright set the comment activate high quality settings
"Video Options"

set the video bitrate in kbit/s (default = 200 kb/s) set frame rate (default = 25) set frame size. The format is WxH (default 160x128).  The following abbreviations are recognized: 128x96 176x144 352x288 704x576 set aspect ratio (4:3, 16:9 or 1.3333, 1.7777) set top crop band size (in pixels) set bottom crop band size (in pixels) set left crop band size (in pixels) set right crop band size (in pixels) disable video recording set video bitrate tolerance (in kbit/s) set max video bitrate tolerance (in kbit/s) set min video bitrate tolerance (in kbit/s) set ratecontrol buffere size (in kbit) force video codec to codec. Use the copy special value to tell that the raw codec data must be copied as is. use same video quality as source (implies VBR) select the pass number (1 or 2). It is useful to do two pass encoding. The statistics of the video are recorded in the first pass and the video at the exact requested bit rate is generated in the second pass. select two pass log file name to file.
"Advanced Video Options"

set the group of picture size use only intra frames use fixed video quantiser scale (VBR) min video quantiser scale (VBR) max video quantiser scale (VBR) max difference between the quantiser scale (VBR) video quantiser scale blur (VBR) video quantiser scale compression (VBR) initial complexity for 1-pass encoding qp factor between p and b frames qp factor between p and i frames qp offset between p and b frames qp offset between p and i frames set rate control equation. Default is tex^qComp. rate control override for specific intervals set motion estimation method to method. Available methods are (from lower to best quality): Try just the (0, 0) vector. (default method) exhaustive search (slow and marginally better than epzs) set dct algorithm to algo. Available values are: FF_DCT_AUTO (default) FF_DCT_FASTINT FF_DCT_INT FF_DCT_MMX FF_DCT_MLIB FF_DCT_ALTIVEC set idct algorithm to algo. Available values are: FF_IDCT_AUTO (default) FF_IDCT_INT           FF_IDCT_SIMPLE        FF_IDCT_SIMPLEMMX     FF_IDCT_LIBMPEG2MMX   FF_IDCT_PS2           FF_IDCT_MLIB          FF_IDCT_ARM           FF_IDCT_ALTIVEC       FF_IDCT_SH4           FF_IDCT_SIMPLEARM     set error resilience to n. FF_ER_CAREFULL (default) FF_ER_COMPLIANT FF_ER_AGGRESSIVE FF_ER_VERY_AGGRESSIVE set error concealment to bit_mask. bit_mask is a bit mask of the following values: FF_EC_GUESS_MVS (default=enabled) FF_EC_DEBLOCK (default=enabled) use "frames" B frames (only MPEG-4) macroblock decision FF_MB_DECISION_SIMPLE: use mb_cmp (cannot change it yet in ffmpeg) FF_MB_DECISION_BITS: chooses the one which needs the fewest bits FF_MB_DECISION_RD: rate distoration use four motion vector by macroblock (only MPEG-4) use data partitioning (only MPEG-4) workaround not auto detected encoder bugs how strictly to follow the standarts enable Advanced intra coding (h263+) enable Unlimited Motion Vector (h263+) deinterlace pictures calculate PSNR of compressed frames dump video coding statistics to vstats_HHMMSS.log. insert video processing module. module contains the module name and its parameters separated by spaces.
"Audio Options"

set audio bitrate (in kbit/s) set the audio sampling freq (default = 44100 Hz) set the audio bitrate in kbit/s (default = 64) set the number of audio channels (default = 1) disable audio recording force audio codec to codec. Use the copy special value to tell that the raw codec data must be copied as is.
"Audio/Video grab options"

set video grab device (e.g. /dev/video0) set video grab channel (DV1394 only) set television standard (NTSC, PAL (SECAM)) set DV1394 grab set audio device (e.g. /dev/dsp)
"Advanced options"

set input stream mapping print specific debug info add timings for benchmarking dump each input packet only use bit exact algorithms (for codec testing) set packet size in bits read input at native frame rate. Mainly used to simulate a grab device. loop over the input stream. Currently it works only for image streams. This option is used for ffserver automatic testing.
"FFmpeg formula evaluator"

When evaluating a rate control string, FFmpeg uses an internal formula evaluator.

The following binary operators are available: +, -, *, /, ^.

The following unary operators are available: +, -, (...).

The following functions are available:

The following constants are available:
EXAMPLES

"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"

* ffmpeg can use any supported file format and protocol as input:

Examples:

* You can input from YUV files:


        
        ffmpeg -i /tmp/test%d.Y /tmp/out.mpg

It will use the files: /tmp/test0.Y, /tmp/test0.U, /tmp/test0.V, /tmp/test1.Y, /tmp/test1.U, /tmp/test1.V, etc...

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.

NAME

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. 

NAME

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 

使用ffmpeg转换AVI到3GP命令行参数

ffmpeg -i test.avi -y -b 20 -s sqcif -r 10 -ab 23.85 -ac 1 -ar 8000 test.3gp
(以上参数由ffmpeg工程组 阳光白云 在QQ群中发布,特此感谢)