1

Note: I'd originally erroneously referred to an h.264 stream by the more common parlance of being an MP4 file, sorry


I'm aware of the usual need to start splitting an h.264 on a keyframe, so that the result video starts with a keyframe, but I'm wondering what it is about the stream structure/spec that makes it so?

Must a keyframe occur every X frames as a fixed parameter of the entire file, or can there be a varying interval between keyframes? If the player is just taking frames as they appear and drawing the whole thing (keyframe) or updating part of it (non keyframe), why does it matter about the keyframe interval?

If it doesn't matter about the interval, and we wanted to cut on frame 87 of a keyframe-every-100 stream, could we not use the 86 preamble frames to calculate a new keyframe for the new file, put the remaining 12 non key frames from the original file to get us to the next keyframe, then the next original keyframe (frame 100) and go from there?

i.e. The file ends up with "new key, 12 original non keys, original key, 99 original non keys, original key .."

3
  • (Couldn't decide whether to post here or videoproduction.se - let me know if people think it's OT here)
    – Caius Jard
    Commented Jul 16, 2020 at 10:32
  • I will admit I suspect you would get a more technical and complete answer at Video, but I don't think it is off topic here.
    – Mokubai
    Commented Jul 16, 2020 at 10:54
  • 1
    There is also some information on Stack Overflow at stackoverflow.com/questions/18444194/… where they seemingly have a problem based on the fact that they do not have a keyframe at their start point.
    – Mokubai
    Commented Jul 16, 2020 at 11:36

1 Answer 1

2

Keyframes can occur at any time, I cannot find a citation but imagine in a movie with many cutscenes. You want a keyframe to be the first frame after any drastic scene change in order to preserve the quality of the video, otherwise you end up with a storm of noise as the video compressor tries to create a new scene using lots of "delta" changes from the previous scene.

For a normal stream having consistent keyframe timing might be acceptable, but for end content better quality will be achieved by having a keyframe at a scene change boundary.

From Keyframes, InterFrame & Video Compression

The keyframe (i-frame) is the full frame of the image in a video. Subsequent frames, the delta frames, only contain the information that has changed. Keyframes will appear multiple times within a stream, depending on how it was created or how it’s being streamed.

Indeed it is adjustable and depends on your requirements for quality and bitrate.

The default for mp4 is a keyframe every 250 frames (so about every 10 seconds). This is bad for streaming, because it's harder to change resolutions. Youtube uses a keyframe interval of 2 seconds.

There's also some methods to extract only iframes (keyframes) as mentioned by BogoToBogo: THUMBNAILS (IFRAME / SCENE CHANGE) - 2020

i-frame thumbnails

The following command will create one thumbnail image every I-frame, named yi01.png, yi02.jpg, yi03.png...yi116

ffmpeg -i yosemiteA.mp4 -f image2 -vf "select='eq(pict_type,PICT_TYPE_I)'" -vsync vfr yi%03d.png
  1. select='eq(pict_type,PICT_TYPE_I)': extracts I-frame image
  2. vsync: video sync method.
  3. vfr: variable frame rate.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .