I have a program for educators (written with electronjs
framework) that plays videos, skipping to preset points in the video as defined in a companion JSON file. Sometimes there is a significant lag after a skip, and this answer suggests that the lag is there because of infrequent keyframes.
Assuming this is correct, I would like to insert keyframes at exact points in the mp4 file where I know the player will skip to. However, some of these video files are already quite large, so I do not want to decrease the keyframe interval, thereby making the files even larger. I only need to force a handful of keyframes in the entire file.
I would like to be able to pass an argument (to ffmpeg
, for example) with the times at which I want to force keyframes. Preferably, I would like to do this without re-encoding the entire video (just like this unanswered question), but if I have to reencode the video, I could do that too.
You can use ffmpeg to insert keyframes where you want them using the
-force_key_frames
option.eg:
will put a keyframe as close to the 5 and 10s mark as it can. The time values can be in hh:mm:ss format, or just seconds (single values, no colons).
You can check if there are frames inserted where you want them using: