Insert keyframe into m4v/mp4 at specific times

2019-08-25 06:06发布

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.

1条回答
贼婆χ
2楼-- · 2019-08-25 06:54

You can use ffmpeg to insert keyframes where you want them using the -force_key_frames option.

eg:

./ffmpeg -i input.mp4 -c:a copy -c:v copy -force_key_frames 0:05:00,0:10:00 out.mp4

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:

./ffprobe -select_streams v:0 -skip_frame nokey -show_entries frame=pkt_pts_time out.mp4
查看更多
登录 后发表回答