c++ffmpegavcodec

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


I'm using this project https://github.com/apc-llc/moviemaker-cpp I'm wondering how can I set fixed framerate on my video for now it results like 33.6 38.1 35.3 when I enter 30FPS as c->framerate I tried everything found on the net but no chance.

What I tried : at 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;

Solution

  • Thanks to π•³π–Šπ–Žπ–˜π–Šπ–“π–‡π–Šπ–—π–Œ

    Solution is :

    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.

    It works!