How to add additional key frames to video using ffmpeg?

Solution 1:

force_key_frames has an option to mirror source stream's keyframe cadence (as yet undocumented - on my todo), however this can't be combined with other expressions in f_k_f.

However, what you can use, is

-force_key_frames source -x264-params keyint=20:scenecut=0

This will set a keyframe if the source frame was a keyframe. However keyint is always measured from the last keyframe set, so if your source had KFs at n=0,34,55,64 then result will have KFs at n=0,20,34,54,55,64,84 - note the lack of KFs at 40,60,80. Also note the consecutive KFs at 54,55, so it's best to ignore the source KFs and let scenecut work.

Solution 2:

Short answer: You can't

Long answer: When using FFMpeg with reencoding (i.e. any other codec than copy), every single image in the input stream will be decompressed and available as a full bitmap. This implies, that at this point in the pipeline, frames 1, 12 and 89 are no longer "special" - they are images like any other.

When now compressing via libx264, you create a video that has key frames - these will be put at the places you configure, in you case every 20 frames.