[FFmpeg-devel] Modified force_key_frames option to accept frame numbers

Sylvester Zaluga sylwekzff at outlook.com
Sun Nov 16 22:30:51 CET 2014

Thank you for your reply.

> Date: Mon, 10 Nov 2014 14:18:25 +0100
> From: george at nsup.org
> To: ffmpeg-devel at ffmpeg.org
> CC: sylwekzff at outlook.com
> Subject: Re: [FFmpeg-devel] Modified force_key_frames option to accept frame numbers
> Le nonidi 19 brumaire, an CCXXIII, Sylvester Zaluga a écrit :
> > ***Sending again, this time with diff***
> Better use git format-patch (or git send-email), so that you have authorship
> and date information.
> > Attached a patch which allows for passing in frame numbers
> > at which key frames should be forced. 
> > 
> > The current modes in which force_key_frames operate are:
> > 
> > * force_key_frames 1,2,3,4,5  - parameter is interpreted as timestamps
> > * force_key_frames expr:gte(t,n_forced) - parameter is an expression
> > 
> > The patch adds support for third mode:
> > * force_key_frames n:0,25,50,75,100  - interpret parameter as frame numbers
> > 
> > I find this useful for avoiding float precision-related
> > issues that surface when key frames are forced at specified timestamps.
> I will not actually oppose the feature, but every time people insist on
> working with frame numbers instead of timestamp, I feel they are doing
> something incorrectly.
> In this particular case, you can easily work around the floating point
> rounding issues, since the rounding behaviour of -force_key_frames is
> specified: "more precisely at the first frames after each specified time".
> You just have to make sure your timestamps are rounded down. For example,
> with 24000/1001 FPS: 4.21 4.25 4.30* 4.34 4.38, you need to write 4.29, not
> 4.30, but you can also write any value between 4.25425 and the actual
> 4.295958333. (4.25+4.30)/2 = 4.275 should always work.

You are right, this should probably work, although, in use cases like mine
(when the key frames have to be inserted at exact indexes, not times, in multiple videos), 
I still find it more natural and practical to just use frame numbers 
(because all the frame time transformations you describe essentially boil down to 
doing exactly that).

This would also make it more consistent with other option "-segment_frames" which
already accepts frame numbers.

> > My use case is as follows:
> > * generate output video with settings for HD video
> > * extract key frame numbers from HD video
> > * generate SD video with key frames at identical positions
> > * In my application, do video LODing by switching HD/SD video 
> >   depending on bandwith
> What happens if, in order to lower even more the SD bitrate, you decide to
> add the decimate filter? Frame numbers are jumbled, while timestamps are
> preserved. Working with frame numbers is fragile.

You are right, in such cases working with timestamps makes more sense.
Obviously different scenarios require different approaches. In my case I need
to generate multiple videos of different quality, but with the same number of
key frames at the same indexes (hence I would avoid the decimate filter).

> (Also, note that with some codecs, forcing a lot of keyframes will have a
> dramatic effect on the efficiency of the encoding.)

I am aware of that. I am letting ffmpeg decide where to put key frames in the first video,
and only force key frames in lower resolution videos, at the same indices as in the first one
(so I am not forcing too many of them, I believe).

> Regards,
> -- 
>   Nicolas George

Sylwester Zaluga

More information about the ffmpeg-devel mailing list