如何使用 avcodec 和 libx264 设置固定(const)fps

How to set fixed (const) fps using avcodec and libx264

我正在使用这个项目https://github.com/apc-llc/moviemaker-cpp 我想知道如何为我的视频设置固定帧率 现在,当我输入 30FPS 作为 c->framerate 时,结果类似于 33.6 38.1 35.3 我尝试了网上找到的所有东西,但没有机会。

我尝试了什么: 在 https://github.com/apc-llc/moviemaker-cpp/blob/319be27849fc3d55a9dc3b0180a5d4ac84e24121/src/writer.cpp#L54

#define FRAMERATE  20
#define FRAME_TIME AVRational{ 1 , FRAMERATE }
#define FRAME_RATE AVRational{ FRAMERATE , 1 }
...

    // Setting up the codec.
    AVCodec* codec = avcodec_find_encoder_by_name("libx264"); //libx264 works too!
    AVDictionary* opt = NULL;
    av_dict_set(&opt, "preset", "slow", 0);
    av_dict_set(&opt, "cfr", "30", 0);
    stream = avformat_new_stream(fc, codec);
    c = stream->codec;
    c->width = width;
    c->height = height;
    c->pix_fmt = AV_PIX_FMT_YUV420P;
    c->time_base = FRAME_TIME;
    c->framerate = FRAME_RATE;
    stream->avg_frame_rate = FRAME_RATE;

感谢

解决方法是:

What you're looking for is fixed gop and fps! to achieve that just set stream avg_frame_rate and tune to zerolatency, that's all.

有效!