[Libav-user] How to control ffmpeg's key-frame generation?

Alex Cohn alexcohn at netvision.net.il
Wed Jan 2 12:28:32 CET 2013

On Wed, Jan 2, 2013 at 8:30 AM, Quy Pham Sy <phamsyquybk at gmail.com> wrote:
> I'm making a segmenter that intervene ffmpeg's write_frame function and
> write output data to separate files. Each segmented file contains segment of
> about 3 seconds video.
> The code does following:
> 1 - Get transcoded packet
> 2 - Check if it contains key frame data, if yes goto 3.
> 3 - Check the duration of current segment, if it exceed 3 seconds, goto 4
> 4 - Close file, and create new segment, write packet to segment file, goto-1
> General speaking, every segment contains at least 3 seconds video data, and
> it starts with a key frame.

Note that your step 4 does something you don't intend: it should never
write the packet to the new segment file. Or maybe your description is

> The problem is that the output video's duration are very different, some
> contain 3 seconds, some contain 5 or 6.
> I suspect that the problem due to how ffmpeg generate key frames during
> transcoding. If the "distance" between two adjacent keyframes are 6s, i got
> 6 seconds segment.
> Here is my questions:
> is that true that ffmpeg generate keyframes at irregular intervals (and
> interval time can be up to few second (eg. 6)?

This depends on the codec.

> How can we control the ffmpeg key frame generation? (i guess there should be
> a ffmpeg command's argument for this, -force_key_frames maybe, but I'm not
> sure)

This depends on the codec, too.


More information about the Libav-user mailing list