繁体   English   中英

C++:用FFmpeg编码H264,无法设置预设

[英]C++: Encoding H264 with FFmpeg, unable to set preset

我正在尝试使用 FFmpeg/libAV 对 H.264 电影进行编码,当我尝试设置编解码器预设时,返回代码指示错误:

...

mContext.codec = avcodec_find_encoder(AV_CODEC_ID_H264);

mContext.stream = avformat_new_stream(mContext.format_context, nullptr);
mContext.stream->id = (int)(mContext.format_context->nb_streams - 1);

mContext.codec_context = avcodec_alloc_context3(mContext.codec);

int ret;
ret = av_opt_set(mContext.codec_context->priv_data, "preset", "medium", 0);

// returns -1414549496

...

为了简洁起见,我在示例中省略了错误检查。

我尝试将preset为不同的值(“中”、“慢”、“非常慢”等)

ret = av_opt_set(mContext.codec_context->priv_data, "preset", "medium", 0); // returns -1414549496

我不认为你被允许触摸priv_data 为什么不直接在codec_context上调用av_opt_set() 参见例如这里

该问题不可重现。

在以下代码示例中av_opt_set返回0

#include <stdio.h>

extern "C"
{
#include "libavutil/opt.h"
#include "libavformat/avformat.h"
}

int main()
{
    AVOutputFormat* ofmt = av_guess_format("h264", nullptr, nullptr); //H.264

    if (ofmt == nullptr)
    {
        return -1;
    }

    AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);

    if (codec == nullptr)
    {
        return -1;
    }

    AVFormatContext* format_context = avformat_alloc_context();

    if (format_context == nullptr)
    {
        return -1;
    }

    int ret = avformat_alloc_output_context2(&format_context, ofmt, nullptr, nullptr);

    if (ret != 0)
    {
        return ret;
    }

    AVStream* stream = avformat_new_stream(format_context, nullptr);

    if (stream == nullptr)
    {
        return -1;
    }

    stream->id = (int)(format_context->nb_streams - 1);

    AVCodecContext* codec_context = avcodec_alloc_context3(codec);

    if (codec_context == nullptr)
    {
        return -1;
    }

    ret = av_opt_set(codec_context->priv_data, "preset", "medium", 0);

    if (ret != 0)
    {
        printf("Error: av_opt_set returned: %d\n", ret);
    }


    return 0;
}

上面的代码在每一步之后检查错误。

没有错误...


我的猜测是您的 FFmpeg 版本不包含 libx264 编码器。
avcodec_find_encoder(AV_CODEC_ID_H264)返回其他 H.264 编码器(不是libx264 )。
另一个编码器没有medium预设选项。

尝试通过替换AVCodec* codec = avcodec_find_encoder(AV_CODEC_ID_H264);来强制libx264编码器; 和:

AVCodec* codec = avcodec_find_encoder_by_name("libx264");

如果codec == nullptr ,您的 FFmpeg 不包含 libx264 编码器。


这只是一个猜测...

如果不是问题,请发布可执行代码示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM