How to add additional key frames to video using ffmpeg?
I want to add key frames on the base of a specific interval (20 frames) to my video.
Therefore I used the following command in ffmpeg:
ffmpeg -i <input> -vcodec libx264 -x264-params keyint=20:scenecut=0 -acodec copy out.mp4Output is, that I get key frames every 20 frames.
But the original video had a key frame e.g. at frame 1, 12, 89....
Now I have key frames at frame 1, 21, 41....
I don't want to replace the key frames which were already there. I want to add additional key frames, so it looks like this:
frame 1, 12, 21, 41, 89....How can I do this?
2 Answers
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.
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.