[FFmpeg-user] Video encoding from C++ with variable frame rates and variable width/height

Julian Smith jules at op59.net
Fri Nov 19 22:51:18 EET 2021

I'm trying to generate compressed video from within an OSG programme
(the Flightgear flight simulator), getting RGB24 frames for the main
window using osg::Image::readPixels(), converting to YUV420P using
sws_scale() and then sending to an ffmpeg encoder+muxer similarly to
the code in ffmpeg/doc/examples/muxing.c.

However i'm having trouble finding an encoder/container combination
that generates video that VLC can cope with, and works with the variable
frame rate and also copes with the user resizing the window size while
video is being generated.

To cope with the variable frame rate i write to .pts and call
av_packet_rescale_ts() as in the ffmpeg examples.

To cope with changes to the frame size, i flush the current
AVCodecContext's output, delete it and create a new AVCodecContext
using the new width and height. I haven't seen this done in any
examples, but it seems to work in at least a couple of cases. I'd be
grateful if anyone could suggest a better way.

So far i've found only three codec/container combinations that come
close to what i want:

    codec=mpeg2video container=mpeg
        Works perfectly on Linux and OpenBSD, coping with variable frame
        rate and frame size, but compression appears to be very poor -
        needs 2Mb/s for decent quality output.

    codec=libx264 container=mpeg
        Appears to work with variable frame rate and size, but vlc
        always seems to freeze after a few seconds when replaying the

    codec=libvpx container=ogv
        vlc-3.0.12 on OpenBSD works fine except it opens a new window
        whenever the frame size changes. vlc-3.0.16 on Linux complains
        about 'more than 5 seconds of late video', pauses and shows
        corrupted frames.

Does anyone have any advice on how i can make things work better?

One other issue - i often see lots of output lines looking like:

    buffer underflow st=0 bufi=489 size=1689

Does anyone know what i can do to stop these messages appearing? Could
they be related to the problems that vlc has for some of the generated

Just in case it's useful, here is my test code:

It has more info on how various other codec/container combinations don't
work for me.

Many thanks for any help,

- Jules


More information about the ffmpeg-user mailing list