如何在基于ffmpeg的程序中以编程方式传递VP8编码器选项

How to pass VP8 encoder option programmatically in ffmpeg based program

本文关键字:方式传 编程 VP8 选项 编码器 程序 ffmpeg      更新时间:2023-10-16

我正在基于标准ffmpeg转码器示例构建使用ffmpeg库的程序。我的目标是建立视频转码器,它编码任何合适的视频(即ffmpeg可以读取)到WEBM格式。问题是我如何传递选项到VP8编码器来控制输出视频质量和其他参数?我的意思是通过c++代码传递这些选项

使用以下代码:

AVDictionary *options = NULL;
AVCodec *codec = avcodec_find_encoder(AVCODEC_ID_VP8);
AVCodecContext *ctx = avcodec_alloc_context3(codec);
av_dict_set(&options, "option", "value", 0);
int res = avcodec_open2(ctx, codec, &options);
if (res < 0)
    error();
while (..) {
    res = avcodec_encode_video2(ctx, ..);
    if (res < 0)
        error();
}
avcodec_close(ctx);
avcodec_free_context(ctx);

相关的"选项"/"值"对是您从vp8编码指南中获得的任何内容,例如FFmpeg wiki。例如,要设置比特率为1mbps (wiki中的第一个例子),使用:

av_dict_set_int(&options, "b", 1024 * 1024, 0);

av_dict_set(&options, "b", "1M", 0);

我建议使用VP9而不是VP8,使用VP8你不会得到很好的质量,但这显然是你的选择。