ffmpeg转码参数解析

来源:互联网 发布:wlk数据库 编辑:程序博客网 时间:2024/06/03 16:56

qscale的取值可以是0.01-255但实际使用超过50就很糟糕了

ffmpeg的cbr模式可以把码率控制的不错,但是vbr无法限制最高码率(虽然有max的设置,但是程序没有实现)

x264标准的封装是x264+aac inflv或者x264+aac in MP4

接下来说明下ffmpeg命令行的语法规则(本块内容来自2009-03-02官方文档):

语法规则结构:

ffmpeg [[infileoptions][`-i'infile]]... {[outfile options]outfile}...

一个最简单的命令形式:

ffmpeg -i input.avi -b 64koutput.avi

这个命令把视频以64k的码率重编码。

显然,输入文件前面要加一个-i选项下面介绍一些有用的全局参数:

-formats参数。会显示你机器当前支持的封装、编码、解码器的信息 -y参数,会指示ffmpeg覆盖输出文件 -t指定视频流持续的时常,支持以秒为单位的数字或"时:分:秒[.毫秒]" -fs 指定输出文件大小的限制 -ss指定开始的时间,和-t的单位一样 -target直接设定你想要转成的目标格式,所有的相关设置都会采用内设值,当然也你也可以加上自己要修改的参数。可用的选择有:
"vcd", "svcd", "dvd", "dv", "dv50", "pal-vcd", "ntsc-svcd", …
这个例子把视频转换成vcd的格式

ffmpeg -i myfile.avi -target vcd/tmp/vcd.mpg

接下来介绍视频选项:

-b 指定码率注意单位是bit/s,所以我们一般要加k,比如 -b1000k 就是1000kb/s

-g 设置组的大小

-vframes 指定要编码的帧数,比如-vframes 1就是编码1帧,截图的时候就这样写.

-r 指定帧率,默认是25

-s指定图像分辨率,用wxh的格式,比如320×240

-aspect 指定宽高比可以些16:9这种,也可以写小数比如1.3333

-croptop指定顶部裁减多少像素,类似的还有

-cropleft -cropright-cropbuttom

-bt设置比特率容许的误差,默认4000k,在第一阶段时使用这个参数会告诉码率控制器能够偏移平均码率多远,这个选项和最大最小码率无关.设太小了不利于质量

-maxrate 和-minrate指定允许的最大和最小码率,一般如果要用cbr模式编码的话会用这个:
ffmpeg -i myfile.avi -b 4000k -minrate 4000k -maxrate 4000k-bufsize 1835k out.m2v
否则用处不大

-vcodec 强制使用某种编码器

-sameq使用和源文件相同的质量,这个选项是动态码率的

-pass指定编码阶段,这个只有1和2,第一阶段的时候应该不处理音频,并且把输出导向空,比如: ffmpeg -i foo.mov-vcodec libxvid -pass 1 -an -f rawvideo -y NULffmpeg -i foo.mov-vcodec libxvid -pass 1 -an -f rawvideo -y /dev/null

-qscale使用固定量化因子来量化视频这个是在vbr模式的,前面有提到,越小质量越好,不要超过50,相关的参数还有

-qmin–qmax用来设定最大最小可使用的量化值

-qdiff 指定固定量化器因子允许的最大偏差

-qblur指定量化器模糊系数,可用范围是0.0-1.0越大使得码率在时间上分配的越平均

-qcomp指定视频量化器压缩系数,默认0.5

-me_method 指定运动估计方法(motionestimation method),可用的选择从垃圾到好排列如下:
zero (0向量)
phods
log
x1
hex
umh
epzs (默认)
full (完全搜索,很慢又好不到哪里去)

-mbd 设定宏模块决策,可用的值:
0 使用mb_cmp,不知道是什么东西,所以这些参数我参考一下mencoder里面的
1 选用需要比特最少的宏块模式
2 选用码率失真最优的宏块模式

-4mv 使用宏块的4个运动向量,只支持mpeg4 -part使用数据划分,只支持mpeg4

-ilme强制允许交错的编码,只支持mpeg2和mpeg4,你可以选择用-deinterlace来去交错

 

音频部分:

-ar 设置采样频率,默认44100hz

-ab 设置比特率,默认64k

-an 禁用音频录制

-acodec 指定音频编码器

 

下面举几个x264编码的例子:

我使用mencoder调用x264编码一个psp用的视频:

x264+aac in mp4(我修改过的,原作者的不能使用)
mencoder test_video.vob -oac lavc -lavcoptsacodec=libfaac:abitrate=94 -ovc x264 -x264encopts\
cabac=1:ref=1:deblock=1,0,0:analyse=0x1,0x111:me=hex:subme=6:psy_rd=1.0,0.0:mixed_refs=0:me_range=32:chroma_me=1:trellis=1:8x\
8dct=0:no-chroma-me=0:chroma_qp_offset=-\
2:nr=0:dct_decimate=1:bframes=3:b_pyramid=0:b_adapt=1:b_bias=0:direct=3:keyint=250:keyint_min=25:scenecut=40:qp=18:bitrate=80\
0 -lavdopts er=2 -of lavf -lavfopts format=mp4 -vf scale=720:480 -omen.mp4

对应的ffmpeg编码参数是:

ffmpeg
ffmpeg -i inputfile.avi -f psp -acodec libfaac -ab 94k -vcodeclibx264 -cqp 28 -coder 1 -refs 3 -deblockalpha 1 -deblockbeta -1-me_method umh -subq 9 -me_range 32 -trellis 2 -chromaoffset -2 -nr0 -bf 2 -b_strategy 1 -bframebias 0 -directpred 3 -g 250 -i_qfactor1.3 -b_qfactor 1.4 -flags2 +bpyramid+wpred+mixed_refs+8x8dct -er 2-s 480x320

需要注意的是,flags2里面那块,似乎要按照一定顺序才能正常工作,其他地方都差不多,详细情况可以从下面两篇文章得出:

第一篇是这个在网上被传了很多,但有些问题的对照表格,我修改了有问题的部分:

-g –keyint-b –bitrate-bufsize–vbv-bufsize-maxrate –vbv-maxrate-pass <1,2,3>–pass-crf–crf-cqp –qp-bf –bframes-coder <0,1>–no-cabac-bframebias–b-bias-keyint_min –min-keyint-sc_threshold –scenecut-deblockalpha-deblockbeta –deblock-qmin –qpmin-qmax –qpmax-qdiff –qpstep-qcomp–qcomp-qblur –qblur-complexityblur –cplxblur-refs –ref-directpred–direct-me_method –me-me_range –merange-subq –subme-bidir_refine<0,1>–bime-trellis <0,1,2>–trellis-nr –nr-level–level-bt –ratetol = -bt / -b-rc_init_occupancy –vbv-init =-rc_init_occupancy / -bufsize-i_qfactor –ipratio = 1 /-i_qfactor-b_qfactor –pbratio-chromaoffset –chroma-qp-offset-rc_eq–rc_eq-threads –threads-cmp<-chroma/+chroma>–no-chroma-me-partitions–partitions+parti8×8i8×8+parti4×4i4×4+partp8×8p8×8+partp4×4p4×4+partb8×8b8×8-flags-loop/+loop–no-deblock/–deblock-psnr/+psnr–no-psnr/nothing-flags2+bpyramid–b-pyramid+wpred–weightb+brdo–b-rdo我这里的ffmpeg已经不能用这个了+mixed_refs–mixed-refs+dct8×8–8×8dct-fastpskip/+fastpskip–no-fast-pskip+aud–aud

 

一下是这篇文章在2008年11月19日更新的版本:

Frame-type options:–keyint(x264)
-g (FFmpeg)
Keyframe interval, also known as GOP length. This determines themaximum distance between I-frames. Very high GOP lengths willresult in slightly more efficient compression, but will makeseeking in the video somewhat more difficult. Recommended default:250

–min-keyint (x264)
-keyint_min (FFmpeg)
Minimum GOP length, the minimum distance between I-frames.Recommended default: 25

–scenecut (x264)
-sc_threshold (FFmpeg)
Adjusts the sensitivity of x264′s scenecut detection. Rarely needsto be adjusted. Recommended default: 40

–pre-scenecut (x264)
UNKNOWN (FFmpeg)
Slightly faster (but less precise) scenecut detection. Normalscenecut detection decides whether a frame is a scenecut after theframe is encoded, and if so then re-encodes the frame as anI-frame. This is not compatible with threading, however, and so–pre-scenecut is automatically activated when multiple encodingthreads are used.

–bframes (x264)
-bf (FFmpeg)
B-frames are a core element of H.264 and are more efficient inH.264 than any previous standard. Some specific targets, such asHD-DVD and Blu-Ray, have limitations on the number of consecutiveB-frames. Most, however, do not; as a result, there is rarely anynegative effect to setting this to the maximum (16) since x264will, if B-adapt is used, automatically choose the best number ofB-frames anyways. This parameter simply serves to limit the maxnumber of B-frames. Note that Baseline Profile, such as that usedby iPods, does not support B-frames. Recommended default:16

–b-adapt (x264)
-b_strategy (FFmpeg)
x264, by default, adaptively decides through a low-resolutionlookahead the best number of B-frames to use. It is possible todisable this adaptivity; this is not recommended. Recommendeddefault: 1

0: Very fast, but not recommended.Does not work with pre-scenecut (scenecut must be off to force offb-adapt).

1: Fast, default mode in x264. Agood balance between speed and quality.

2: A much slower but more accurateB-frame decision mode that correctly detects fades and generallygives considerably better quality. Its speed gets considerablyslower at high bframes values, so its recommended to keep bframesrelatively low (perhaps around 3) when using this option. It alsomay slow down the first pass of x264 when in threaded mode.
–b-bias 0 (x264)
-bframebias 0 (FFmpeg)
Make x264 more likely to choose higher numbers of B-frames duringthe adaptive lookahead. Not generally recommended. Recommendeddefault: 0

–b-pyramid (x264)
-flags2 +bpyramid (FFmpeg)
Allows B-frames to be kept as references. The name is technicallymisleading, as x264 does not actually use pyramid coding; it simplyadds B-references to the normal reference list. B-references get aquantizer halfway between that of a B-frame and P-frame. Thissetting is generally beneficial, but it increases the DPB (decodingpicture buffer) size required for playback, so when encoding forhardware, disabling it may help compatibility.

–no-cabac (x264)
-coder 0,1 (FFmpeg)
CABAC is the default entropy encoder used by x264. Though somewhatslower on both the decoding and encoding end, it offers 10-15%improved compression on live-action sources and considerably higherimprovements on animated sources, especially at low bitrates. It isalso required for the use of trellis quantization. Disabling CABACmay somewhat improve decoding performance, especially at highbitrates. CABAC is not allowed in Baseline Profile. Recommendeddefault: -coder 1 (CABAC enabled)

–ref (x264)
-refs (FFmpeg)
One of H.264′s most useful features is the abillity to referenceframes other than the one immediately prior to the current frame.This parameter lets one specify how many references can be used,through a maximum of 16. Increasing the number of refs increasesthe DPB (Decoded Picture Buffer) requirement, which means hardwareplayback devices will often have strict limits to the number ofrefs they can handle. In live-action sources, more reference havelimited use beyond 4-8, but in cartoon sources up to the maximumvalue of 16 is often useful. More reference frames require moreprocessing power because every frame is searched by the motionsearch (except when an early skip decision is made). The slowdownis especially apparent with slower motion estimation methods.Recommended default: -refs 6

–no-deblock (x264)
-flags -loop (FFmpeg)
Disable loop filter. Recommended default: -flags +loop(Enabled)

–deblock (x264)
-deblockalpha (FFmpeg)
-deblockbeta (FFmpeg)
One of H.264′s main features is the in-loop deblocker, which avoidsthe problem of blocking artifacts disrupting motion estimation.This requires a small amount of decoding CPU, but considerablyincreases quality in nearly all cases. Its strength may be raisedor lowered in order to avoid more artifacts or keep more detail,respectively. Deblock has two parameters: alpha (strength) and beta(threshold). Recommended defaults:-deblockalpha 0 -deblockbeta 0(Must have ‘-flags +loop’)

–interlaced (x264)
UNKNOWN (FFmpeg)
Enables interlaced encoding. x264′s interlaced encoding is not asefficient as its progressive encoding; consider deinterlacing formaximum effectiveness. Ratecontrol:–qp (x264)
-cqp (FFmpeg)
Constant quantizer mode. Not exactly constant completely–B-framesand I-frames have different quantizers from P-frames. Generallyshould not be used, since CRF gives better quality at the samebitrate.

–bitrate (x264)
-b (FFmpeg)
Enables target bitrate mode. Attempts to reach a specific bitrate.Should be used in 2-pass mode whenever possible; 1-pass bitratemode is generally the worst ratecontrol mode x264 has.

–crf (x264)
-crf (FFmpeg)
Constant quality mode (also known as constant ratefactor). Bitratecorresponds approximately to that of constant quantizer, but givesbetter quality overall at little speed cost. The best one-passoption in x264.

–vbv-maxrate (x264)
-maxrate (FFmpeg)
Specifies the maximum bitrate at any point in the video. Requiresthe VBV buffersize to be set. This option is generally used whenencoding for a piece of hardware with bitratelimitations.

–vbv-bufsize (x264)
-bufsize (FFmpeg)
Depends on the profile level of the video being encoded. Set onlyif you’re encoding for a hardware device.

–vbv-init (x264)
-rc_init_occupancy (FFmpeg)
Initial VBV buffer occupancy. Note: Don’t mess withthis.

–qpmin (x264)
-qmin (FFmpeg)
Minimum quantizer. Doesn’t need to be changed. Recommended default:-qmin 10

–qpmax (x264)
-qmax (FFmpeg)
Maximum quantizer. Doesn’t need to be changed. Recommended default:-qmax 51

–qpstep (x264)
-qdiff (FFmpeg)
Set max QP step. Recommended default: -qdiff 4

–ratetol (x264)
-bt (FFmpeg)
Allowed variance of average bitrate

–ipratio (x264)
-i_qfactor (FFmpeg)
Qscale difference between I-frames and P-frames.

–pbratio (x264)
-b_qfactor (FFmpeg)
Qscale difference between P-frames and B-frames.

–chroma-qp-offset (x264)
-chromaoffset (FFmpeg)
QP difference between chroma and luma.

–aq-strength (x264)
UNKNOWN (FFmpeg)
Adjusts the strength of adaptive quantization. Higher values takemore bits away from complex areas and edges and move them towardssimpler, flatter areas to maintain fine detail. Default:1.0

–pass <1,2,3> (x264)
-pass <1,2,3>(FFmpeg)
Used with –bitrate. Pass 1 writes the stats file, pass 2 reads it,and 3 both reads and writes it. If you want to use three pass, thismeans you will have to use –pass 1 for the first pass, –pass 3 forthe second, and –pass 2 or 3 for the third.

–stats (x264)
UNKNOWN (FFmpeg)
Allows setting a specific filename for the firstpass statsfile.

–rceq (x264)
-rc_eq (FFmpeg)
Ratecontrol equation. Recommended default: -rc_eq‘blurCplx^(1-qComp)’

–qcomp (x264)
-qcomp (FFmpeg)
QP curve compression: 0.0 => CBR, 1.0 => CQP. Recommendeddefault: -qcomp 0.60

–cplxblur (x264)
-complexityblur (FFmpeg)
Reduce fluctuations in QP (before curve compression)[20.0]

–qblur (x264)
-qblur (FFmpeg)
Reduce fluctuations in QP (after curve compression)[0.5]

–zones /(x264)
UNKNOWN (FFmpeg)
Allows setting a specific quantizer for a specific region ofvideo.

–qpfile (x264)
UNKNOWN (FFmpeg)
Allows one to read in a set of frametypes and quantizers from afile. Useful for testing various encoding options while ensuringthe exact same quantizer distribution. Analysis:–partitions(x264)
-partitions (FFmpeg)

p8x8 (x264) /+partp8x8(FFmpeg)

p4x4 (x264) /+partp4x4(FFmpeg)

b8x8 (x264) /+partb8x8(FFmpeg)

i8x8 (x264) /+parti8x8(FFmpeg)

i4x4 (x264) /+parti4x4(FFmpeg)

One of H.264′s most usefulfeatures is the ability to choose among many combinations of interand intra partitions. P-macroblocks can be subdivided into 16×8,8×16, 8×8, 4×8, 8×4, and 4×4 partitions. B-macroblocks can bedivided into 16×8, 8×16, and 8×8 partitions. I-macroblocks can bedivided into 4×4 or 8×8 partitions. Analyzing more partitionoptions improves quality at the cost of speed. The default is toanalyze all partitions except p4x4 (p8x8, i8x8, i4x4, b8x8), sincep4x4 is not particularly useful except at high bitrates and lowerresolutions. Note that i8x8 requires 8x8dct, and is therefore aHigh Profile-only partition. p8x8 is the most costly, speed-wise,of the partitions, but also gives the most benefit. Generally,whenever possible, all partition types except p4x4 should beused.

–direct (x264)
-directpred (FFmpeg)
B-frames in H.264 can choose between spatial and temporalprediction mode. Auto allows x264 to pick the best of these; theheuristic used is whichever mode allows more skip macroblocks. Autoshould generally be used.

–direct-8×8 (x264)
UNKONWN (FFmpeg)
This should be left at the default (-1).

–weightb (x264)
-flags2 +wpred(FFmpeg)
This allows B-frames to use weighted prediction options other thanthe default. There is no real speed cost for this, so it shouldalways be enabled.

–me

–merange (x264)
-me_range (FFmpeg)
MErange controls the max range of the motion search. For HEX andDIA, this is clamped to between 4 and 16, with a default of 16. ForUMH and ESA, it can be increased beyond the default 16 to allow fora wider-range motion search, which is useful on HD footage and forhigh-motion footage. Note that for UMH and ESA, increasing MErangewill significantly slow down encoding.

–mvrange(x264)
UNKNOWN (FFmpeg)
Limits the maximum motion vector range. Since x264 by defaultlimits this to 511.75 for standards compliance, this should not bechanged.

–subme 6(x264)
-subq 6(FFmpeg)

1: Fastest, but extremely lowquality. Should be avoided except on first passencoding.

2-5: Progressively better andslower, 5 serves as a good medium for higher speed encoding.
6-7: 6 is the default. Activates rate-distortion optimization forpartition decision. This can considerably improve efficiency,though it has a notable speed cost. 6 activates it in I/P frames,and subme7 activates it in B frames.

8-9: Activates rate-distortionrefinement, which uses RDO to refine both motion vectors and intraprediction modes. Slower than subme 6, but again, moreefficient.

An extremely important encodingparameter which determines what algorithms are used for bothsubpixel motion searching and partition decision.

–psy-rd : (x264)
UNKNOWN (FFmpeg)
First value represents the amount that x264 biases in favor ofdetail retention instead of max PSNR in mode decision. Requiressubme >= 6. Second value is psy-trellis, an experimentalalgorithm that tries to improve sharpness and detail retention atthe expense of more artifacting. Recommended starting values are0.1-0.2. Requires trellis >= 1. Recommended default:1.0:0.0

–mixed-refs(x264)
-flags2 +mixed_refs(FFmpeg)
H.264 allows p8x8 blocks to select different references for eachp8x8 block. This option allows this analysis to be done, and boostsquality with little speed impact. It should generally be used,though it obviously has no effect with only one referenceframe.

–no-chroma-me(x264)
UNKNOWN (FFmpeg)
Chroma is used in the last steps of the subpixel refinement bydefault. For a slight speed increase, this can be disabled (at thecost of quality).

–8x8dct (x264)
-flags2 +dct8x8(FFmpeg)
Gives a notable quality boost by allowing x264 to choose between8×8 and 4×4 frequency transform size. Required for i8x8 partitions.Speed cost for this option is near-zero both for encoding anddecoding; the only reason to disable it is when one needs supporton a device not compatible with High Profile.

–trellis <0,1,2>(x264)
-trellis <0,1,2>(FFmpeg)

0: disabled

1: enabled only on the finalencode of a MB

2: enabled on all modedecisions

The main decision made inquantization is which coefficients to round up and which to rounddown. Trellis chooses the optimal rounding choices for the maximumrate-distortion score, to maximize PSNR relative to bitrate. Thisgenerally increases quality relative to bitrate by about 5% for asomewhat small speed cost. It should generally be enabled. Notethat trellis requires CABAC.

–no-fast-pskip(x264)
-flags2 -fastpskip(FFmpeg)
By default, x264 will skip macroblocks in P-frames that don’tappear to have changed enough between two frames to justifyencoding the difference. This considerably speeds up encoding.However, for a slight quality boost, P-skip can be disabled. Inthis case, the full analysis will be done on all P-blocks, and theonly skips in the output stream will be the blocks whose motionvectors happen to match that of the skip vector and motion vectorshappen to match that of the skip vector and which have no residual.The speed cost of enabling no-fast-pskip is relatively high,especially with many reference frames. There is a similar B-skipinternal to x264, which is why B-frames generally encode muchfaster than P-frames, but it cannot be disabled on thecommandline.

–no-dct-decimate(x264)
UNKNOWN (FFmpeg)
By default, x264 will decimate (remove all coefficients from)P-blocks that are extremely close to empty of coefficents. This canimprove overall efficiency with little visual cost, but may workagainst an attempt to retain grain or similar. DCT decimationshould be left on unless there’s a good reason to disableit.

–nr(x264)
UNKNOWN (FFmpeg)
a fast, built-in noise reduction routine. Not as effective asexternal filters such as hqdn3d, but faster. Since x264 alreadynaturally reduces noise through its quantization process, thisparameter is not usually necessary.

–deadzone-inter(264)
–deadzone-intra (x264)
UNKNOWN (FFmpeg)
UNKNOWN (FFmpeg)
When trellis isn’t activated, deadzone parameters determine howmany DCT coefficients are rounded up or down. Rounding up resultsin higher quality and more detail retention, but costs more bits–sorounding is a balance between quality and bit cost. Lowering thesesettings will result in more coefficients being rounded up, andraising the settings will result in more coefficients being roundeddown. Recommended: keep them at the defaults.

–cqm (264)
–cqpfile(x264)UNKNOWN (FFmpeg)
UNKNOWN (FFmpeg)
Allows the use of a custom quantization matrix to weightfrequencies differently in the quantization process. The presetsquant matrices are "jvt" and "flat". –cqpfile reads a custom quantmatrices from a JM-compatible file. Recommended only if you knowwhat you’re doing.
原创粉丝点击