【FFmpeg】设置H264参数

发布时间:2025-12-10 11:26:28 浏览次数:8

一、设置x264参数的接口

// 获取编码器AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264);// 创建编码器上下文AVCodecContext *codecContext = avcodec_alloc_context3(codec);// 准备编码器参数AVDictionary *codecOptions = nullptr;av_dict_set(&codecOptions, "profile", "main", 0);// 设置编码器参数avcodec_open2(codecContext, codec, &codecOptions);

二、x264参数详解

1、preset

指定编码速度,速度越慢,画质越好,cpu占用越高,可取值:

ultrafast,superfast,veryfast,faster,fast,medium,slow,slower,veryslow,placebo
2、tune

融合视觉优化,可取值:

film:电影、真人类型animation:动画grain:需要保留大量的grainstillimage:静态图像编码时使用psnr:为提高psnr做了参数上的优化ssim:为提高ssim做了优化的参数fastecode:可以快速解码的参数zerolatency:0延迟,用在需要非常低的延迟的情况下,比如电视电话会议的编码。

ffmpeg 调用如下方式来设置preset 和 tune

x264_param_default_preset(&x4->params, x4->preset, x4->tune)
3、prfile

压缩等级,依次为基本画质、主流画质、高画质

baseline、main、high、high10、high422、high444

Baseline profile多应用于实时通信领域;
Main profile多应用于流媒体领域;
High profile则多应用于广电和存储领域。
ffmpeg 调用如下方式来来设置 prfile

x264_param_apply_profile(&x4->params, x4->profile)
4、fastfirstpass

当编码时,第一遍扫描时,使用fast设置,x264接口:

x264_param_apply_fastfirstpass(&x4->params);
5、level

用来约束 分辨率、帧率 和 码率,720P一般为3.1、1080P一般为4。
ffmpeg中获取对应level的参考帧的最大数目

x4->params.i_frame_reference = av_clip(x264_levels[i].dpb / mbn / scale, 1, x4->params.i_frame_reference);
6、passlogfile、stats

Filename for 2 pass stats,用于2pass模式,保存编码的统计数据,在x264中默认为"x264_2pass.log",
ffmpeg中直接将它传递给x264:x264_param_parse

7、wpredp、weightp

p帧的加权预测,来改善P帧的压缩。亦改善淡入/淡出的品质。模式越高越慢。对应x264的 weightp ,取值

- 0: Disabled- 1: Weighted refs- 2: Weighted refs + Duplicates

ffmpeg中直接将它传递给x264:x264_param_parse

8、a53cc:

使用A53隐藏字幕(如果有的话),(这里涉及到SEI补充增强信息,提供了向视频码流中加入信息的办法,是H.264/H.265 视频压缩标准的特性之一)

9、x264opts

向x264传递参数,ffmpeg循环调用 x264_param_parse 来设置。

10、crf

Constant Rate Factor 恒定码流控制,取值范围为0-51,其中0为无损模式,数值越大,画质越差,生成的文件却越小。从主观上讲,18~28是一个合理的范围。
码流控制方式:CQP(恒定质量),CRF(恒定码率),ABR(平均码率),默认是CRF

11、crf_max

在CRF模式,防止VBV降低质量超过这一点

12、qp

恒定质量控制(恒定QP)

13、aq-mode

自适应量化模式(Adaptive Quantization Mode),自适应量化就是根据宏块的复杂度来调整每个宏块量化时的量化参数。

- 0: Disabled 禁止AQ- 1: Variance AQ (complexity mask),默认值- 2: Auto-variance AQ- 3: Auto-variance AQ with bias to dark scenes
14、aq-strength

AQ的强度。减少块和模糊在平坦和纹理区域。默认: 1.0,设置AQ偏向于低细节度(“平滑”)宏块的强度。不允许为负值。建议选值不超过0.0~2.0范围。

15、psy

使用心理视觉优化。

16、psy-rd

心理视觉优化的强度(不适用于动画片)

17、rc-lookahead

预先查看帧类型和速率控制的帧数

18、weightb

b帧的加权预测

19、ssim

计算和打印SSIM统计数据。(Structural Similarity结构相似性)

20、intra-refresh

使用周期性的内部刷新代替IDR帧,参见:H264编码和解码的问题——b intra refresh

21、bluray-compat

兼容蓝光。

22、direct-pred:

直接MV预测方式(不懂什么意思?),取值:

#define X264_DIRECT_PRED_NONE 0//none#define X264_DIRECT_PRED_SPATIAL 1//spatial#define X264_DIRECT_PRED_TEMPORAL 2//temporal#define X264_DIRECT_PRED_AUTO 3//auto
23、avcintra-class

AVC-Intra压缩等级,AVC-Intra是H264的一种压缩编码方式,

24、me_method、motion-est

设置运动估计方法,取值:

#define X264_ME_DIA 0//dia#define X264_ME_HEX 1//hex#define X264_ME_UMH 2//umh#define X264_ME_ESA 3//esa#define X264_ME_TESA 4//tesa
25、forced-idr

如果强制关键帧,强迫他们作为IDR帧。
ffmpeg源码:

switch (frame->pict_type) {case AV_PICTURE_TYPE_I:x4->pic.i_type = x4->forced_idr > 0 ? X264_TYPE_IDR : X264_TYPE_KEYFRAME;
26、coder

熵编码类型,取值:

defaultcavlccabacvlc ac
27、b_strategy

I/P/ b帧之间的选择策略, 取值:

#define X264_B_ADAPT_NONE 0#define X264_B_ADAPT_FAST 1#define X264_B_ADAPT_TRELLIS 2
28、chromaoffset

色度和luma之间的QP差异

29、sc_threshold

场景变化阈值,判读是否插入额外的I帧

30、noise_reduction

使用降噪技术,当内容噪音大时使用。

31、x264-params

使用键值对key=value直接配置x264

32、以下选项参见x264命令详解:x264 --fullhelp

b-bias
b-pyramid
mixed-refs
8x8dct
fast-pskip
aud
mbtree
deblock
cplxblur
partitions:以逗号分隔的要考虑的分区列表,取值: p8x8, p4x4, b8x8, i8x8, i4x4, none, all
slice-max-size
nal-hrd

33、ffmpeg 和 x264的参数对照

三、ffmpeg列出的lib264参数和默认值

static const AVOption options[] = {{ "preset","Set the encoding preset (cf. x264 --fullhelp)", OFFSET(preset), AV_OPT_TYPE_STRING, { .str = "medium" }, 0, 0, VE},{ "tune","Tune the encoding params (cf. x264 --fullhelp)", OFFSET(tune), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},{ "profile","Set profile restrictions (cf. x264 --fullhelp) ", OFFSET(profile), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},{ "fastfirstpass","Use fast settings when encoding first pass", OFFSET(fastfirstpass), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE},{ "level","Specify level (as defined by Annex A)", OFFSET(level), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},{ "passlogfile","Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},{ "wpredp", "Weighted prediction for P-frames", OFFSET(wpredp), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},{ "a53cc", "Use A53 Closed Captions (if available)", OFFSET(a53_cc), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, VE},{ "x264opts", "x264 options", OFFSET(x264opts), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, VE},{ "crf", "Select the quality for constant quality mode", OFFSET(crf), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },{ "crf_max", "In CRF mode, prevents VBV from lowering quality beyond this point.",OFFSET(crf_max), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE },{ "qp", "Constant quantization parameter rate control method",OFFSET(cqp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },{ "aq-mode", "AQ method", OFFSET(aq_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "aq_mode"},{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_NONE}, INT_MIN, INT_MAX, VE, "aq_mode" },{ "variance", "Variance AQ (complexity mask)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_VARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },{ "autovariance", "Auto-variance AQ", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE}, INT_MIN, INT_MAX, VE, "aq_mode" },#if X264_BUILD >= 144{ "autovariance-biased", "Auto-variance AQ with bias to dark scenes", 0, AV_OPT_TYPE_CONST, {.i64 = X264_AQ_AUTOVARIANCE_BIASED}, INT_MIN, INT_MAX, VE, "aq_mode" },#endif{ "aq-strength", "AQ strength. Reduces blocking and blurring in flat and textured areas.", OFFSET(aq_strength), AV_OPT_TYPE_FLOAT, {.dbl = -1}, -1, FLT_MAX, VE},{ "psy", "Use psychovisual optimizations.", OFFSET(psy), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },{ "psy-rd", "Strength of psychovisual optimization, in <psy-rd>:<psy-trellis> format.", OFFSET(psy_rd), AV_OPT_TYPE_STRING, {0 }, 0, 0, VE},{ "rc-lookahead", "Number of frames to look ahead for frametype and ratecontrol", OFFSET(rc_lookahead), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },{ "weightb", "Weighted prediction for B-frames.", OFFSET(weightb), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },{ "weightp", "Weighted prediction analysis method.", OFFSET(weightp), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "weightp" },{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_NONE}, INT_MIN, INT_MAX, VE, "weightp" },{ "simple", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SIMPLE}, INT_MIN, INT_MAX, VE, "weightp" },{ "smart", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_WEIGHTP_SMART}, INT_MIN, INT_MAX, VE, "weightp" },{ "ssim", "Calculate and print SSIM stats.", OFFSET(ssim), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },{ "intra-refresh", "Use Periodic Intra Refresh instead of IDR frames.",OFFSET(intra_refresh),AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },{ "bluray-compat", "Bluray compatibility workarounds.", OFFSET(bluray_compat) ,AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE },{ "b-bias", "Influences how often B-frames are used", OFFSET(b_bias), AV_OPT_TYPE_INT, { .i64 = INT_MIN}, INT_MIN, INT_MAX, VE },{ "b-pyramid", "Keep some B-frames as references.", OFFSET(b_pyramid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "b_pyramid" },{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NONE}, INT_MIN, INT_MAX, VE, "b_pyramid" },{ "strict", "Strictly hierarchical pyramid", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_STRICT}, INT_MIN, INT_MAX, VE, "b_pyramid" },{ "normal", "Non-strict (not Blu-ray compatible)", 0, AV_OPT_TYPE_CONST, {.i64 = X264_B_PYRAMID_NORMAL}, INT_MIN, INT_MAX, VE, "b_pyramid" },{ "mixed-refs", "One reference per partition, as opposed to one reference per macroblock", OFFSET(mixed_refs), AV_OPT_TYPE_BOOL, { .i64 = -1}, -1, 1, VE },{ "8x8dct", "High profile 8x8 transform.", OFFSET(dct8x8), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},{ "fast-pskip", NULL, OFFSET(fast_pskip), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},{ "aud", "Use access unit delimiters.", OFFSET(aud), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},{ "mbtree", "Use macroblock tree ratecontrol.", OFFSET(mbtree), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, VE},{ "deblock", "Loop filter parameters, in <alpha:beta> form.", OFFSET(deblock), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},{ "cplxblur", "Reduce fluctuations in QP (before curve compression)", OFFSET(cplxblur), AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX, VE},{ "partitions", "A comma-separated list of partitions to consider. ""Possible values: p8x8, p4x4, b8x8, i8x8, i4x4, none, all", OFFSET(partitions), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE},{ "direct-pred", "Direct MV prediction mode", OFFSET(direct_pred), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "direct-pred" },{ "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_NONE }, 0, 0, VE, "direct-pred" },{ "spatial", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_SPATIAL }, 0, 0, VE, "direct-pred" },{ "temporal", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_TEMPORAL }, 0, 0, VE, "direct-pred" },{ "auto", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_DIRECT_PRED_AUTO }, 0, 0, VE, "direct-pred" },{ "slice-max-size","Limit the size of each slice in bytes", OFFSET(slice_max_size),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE },{ "stats", "Filename for 2 pass stats", OFFSET(stats), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },{ "nal-hrd", "Signal HRD information (requires vbv-bufsize; ""cbr not allowed in .mp4)", OFFSET(nal_hrd), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, VE, "nal-hrd" },{ "none", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_NONE}, INT_MIN, INT_MAX, VE, "nal-hrd" },{ "vbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_VBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },{ "cbr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = X264_NAL_HRD_CBR}, INT_MIN, INT_MAX, VE, "nal-hrd" },{ "avcintra-class","AVC-Intra class 50/100/200", OFFSET(avcintra_class),AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 200 , VE},{ "me_method", "Set motion estimation method", OFFSET(motion_est), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, X264_ME_TESA, VE, "motion-est"},{ "motion-est", "Set motion estimation method", OFFSET(motion_est), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, X264_ME_TESA, VE, "motion-est"},{ "dia", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_DIA }, INT_MIN, INT_MAX, VE, "motion-est" },{ "hex", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_HEX }, INT_MIN, INT_MAX, VE, "motion-est" },{ "umh", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_UMH }, INT_MIN, INT_MAX, VE, "motion-est" },{ "esa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_ESA }, INT_MIN, INT_MAX, VE, "motion-est" },{ "tesa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = X264_ME_TESA }, INT_MIN, INT_MAX, VE, "motion-est" },{ "forced-idr", "If forcing keyframes, force them as IDR frames.", OFFSET(forced_idr), AV_OPT_TYPE_BOOL, { .i64 = 0 }, -1, 1, VE },{ "coder", "Coder type", OFFSET(coder), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1, VE, "coder" },{ "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = -1 }, INT_MIN, INT_MAX, VE, "coder" },{ "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },{ "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },{ "vlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },{ "ac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },{ "b_strategy", "Strategy to choose between I/P/B-frames", OFFSET(b_frame_strategy), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 2, VE },{ "chromaoffset", "QP difference between chroma and luma", OFFSET(chroma_offset), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },{ "sc_threshold", "Scene change threshold", OFFSET(scenechange_threshold), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },{ "noise_reduction", "Noise reduction", OFFSET(noise_reduction), AV_OPT_TYPE_INT, { .i64 = -1 }, INT_MIN, INT_MAX, VE },{ "x264-params", "Override the x264 configuration using a :-separated list of key=value parameters", OFFSET(x264_params), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },{ NULL },};static const AVCodecDefault x264_defaults[] = {{ "b", "0" },{ "bf", "-1" },{ "flags2", "0" },{ "g", "-1" },{ "i_qfactor", "-1" },{ "b_qfactor", "-1" },{ "qmin", "-1" },{ "qmax", "-1" },{ "qdiff", "-1" },{ "qblur", "-1" },{ "qcomp", "-1" },// { "rc_lookahead", "-1" },{ "refs", "-1" },#if FF_API_PRIVATE_OPT{ "sc_threshold", "-1" },#endif{ "trellis", "-1" },#if FF_API_PRIVATE_OPT{ "nr", "-1" },#endif{ "me_range", "-1" },{ "subq", "-1" },#if FF_API_PRIVATE_OPT{ "b_strategy", "-1" },#endif{ "keyint_min", "-1" },#if FF_API_CODER_TYPE{ "coder", "-1" },#endif{ "cmp", "-1" },{ "threads", AV_STRINGIFY(X264_THREADS_AUTO) },{ "thread_type", "0" },{ "flags", "+cgop" },{ "rc_init_occupancy","-1" },{ NULL },};
需要做网站?需要网络推广?欢迎咨询客户经理 13272073477