VeryEasyTools

Tech notes

What Is a Keyframe? Why a Trimmed Video Starts Early or Freezes at the Beginning

VeryEasyTools · 6 min read

47 of 90 framesWhat survived a stream copy mid-GOP

How this was measured

Files
A 20-second test video (640×360, 30 fps, H.264, a keyframe every 4 s, 3 B-frames); a 20-second H.264 AVI with B-frames (600 frames)
Cuts
3 seconds from the 2.5 s mark with ffmpeg 6.1: stream copy (-c copy) seeking before and after the input, and re-encoding (libx264)
Counted
Length, frames that actually decode, keyframe positions and packet order, with ffprobe
Measured
November 16, 2026

Programs that trim video usually offer two ways to do it: a “keep the original” cut that finishes in seconds, and a “precise” cut that takes much longer. With the fast one, the clip sometimes starts a second or two before the point you chose, or the picture freezes for the first few seconds while only the sound plays. It isn’t a bug in the program. It comes from how video is stored, and the name for it is the keyframe.

Video doesn’t store every picture

Storing 30 separate photos for every second of video would be far too big. So video compression (H.264, HEVC and so on) divides frames into three kinds.

Kind What it holds Can it be drawn alone? Size
I-frame (keyframe) The whole picture Yes Large
P-frame What changed from an earlier frame Needs the earlier frame Small
B-frame What differs from frames before and after it Needs both Smallest

A keyframe is a complete photo; the others are correction notes such as “this part moved 3 pixels to the right since the last frame”. A keyframe and the correction notes that follow it are called a GOP (group of pictures). How far apart keyframes are depends on the device or program that made the video and its settings.

I made a test video and opened it up. Keyframes sat at 0, 4, 8, 12 and 16 seconds, and the frame types at the start, in display order, looked like this:

I B B P B B B P B P ...

B-frames: storage order is not display order

A B-frame refers to a frame that comes after it, so the later frame has to be decoded first. That makes the order in the file (decoding order) differ from the order on screen. Here are the first eight packets of the test video in the order they are stored.

Stored order Shown at (pts) Decoded at (dts) Keyframe
1 0.000 s −0.067 s ✓
2 0.100 s −0.033 s
3 0.033 s 0.000 s
4 0.067 s 0.033 s
5 0.233 s 0.067 s
6 0.167 s 0.100 s
7 0.133 s 0.133 s
8 0.200 s 0.167 s

The frame stored second (0.100 s) sits ahead of the third and fourth (0.033 s and 0.067 s) in the file, even though it is shown after them, because those two are B-frames that refer to it. Decoding times start below zero (−0.067 s) to make this order work. This structure, with two timestamps per frame, is what makes cutting tricky.

What happens when you cut without re-encoding

A stream copy doesn’t decode the compressed packets. It picks out the ones it needs and moves them into a new file, so it is fast and the quality is untouched. But it can only start cleanly at a keyframe. Start anywhere else and there is no earlier picture to build on.

I cut 3 seconds of the test video, starting at 2.5 s, in three ways. The 2.5 s mark falls between keyframes (0 s and 4 s).

Method Length Frames that decode What happened
Stream copy, backing up to a keyframe 5.59 s 167 Started from the keyframe at 0 s, 2.5 s earlier than chosen
Stream copy, starting at the chosen point 3.06 s 47 Frames from 2.5 to 4 s had no keyframe and were lost
Re-encoding (precise) 3.00 s 90 Exactly 90 frames from 2.5 s

Three seconds at 30 fps should be 90 frames.

Backing up to a keyframe loses nothing, but the extra 2.5 seconds back to the previous keyframe come with it, making a 5.59-second clip. Many programs do this, and it’s why a clip “starts a second or two early”. The farther apart the keyframes, the earlier it starts.

Cutting right at the chosen point gives roughly the right length, but the 1.5 seconds from 2.5 s to the next keyframe (4 s), or 45 frames, are nothing but correction notes that can’t be decoded. Only 47 frames survived. Depending on the player, the screen stays frozen, black or garbled during that time. Audio has no keyframes in this sense, so it plays from the start. That’s the “sound but frozen picture” effect.

Re-encoding decodes the frame at your chosen point, turns it into a new keyframe and compresses everything after it again. It is precise, but compressing takes time, and going through compression once more changes the quality very slightly.

Which one should you use?

  • Trimming a few seconds with room to spare (picking a part to send in a chat, dropping the start of a long recording): stream copy. It takes seconds and keeps the quality. A slightly early start rarely matters.
  • Starting on an exact frame (the first word of a line, a piece to splice into an edit): precise cut. You wait, but it starts where you want.
  • Videos with few keyframes: the longer the interval, the earlier a stream copy will start. Check where the result begins.

A real bug: frames missing from a B-frame AVI

Trim Video on this site keeps the original by default, and offers a Trim precisely button when the start moves too far. The stream copy command originally looked like this:

// The old command (shortened): seeking before the input = backing up to a keyframe
return ['-ss', start.toFixed(3), '-t', length.toFixed(3), '-i', '{input}', '-c', 'copy', output]

On November 15, while fixing a different bug, I found something odd. Cutting a 20-second H.264 AVI with B-frames (600 frames) from the very beginning produced 350 frames. With a start of 0 seconds there was nothing to back up to, yet frames were missing. The ffmpeg log had this line:

[avi] could not seek to position 0.000

AVI wasn’t designed to carry B-frames, so timing information for B-frame video gets written inaccurately. The seek to 0 s failed, and the packets at the front that couldn’t be located were thrown away. Running it again with ffmpeg 6.1 on this computer produced only 540 of 600 frames, with the same message (ffmpeg.wasm in the browser produced 350). MP4 had no problem with the same command.

The fix is simple. If the start is within 0.5 seconds, the cut will begin at the first keyframe anyway, so don’t seek at all; just copy from the beginning to the end point.

/** Below this start time, don't seek before the input */
export const nearStart = (start: number) => start < 0.5

export function cutArgs(start: number, length: number, output: string): string[] {
  // Seeking near the start fails on AVI with B-frames and drops the first frames.
  // The cut starts at the first keyframe anyway, so copy from the beginning to the end point
  const seek = nearStart(start)
    ? ['-t', (start + length).toFixed(3)]
    : ['-ss', start.toFixed(3), '-t', length.toFixed(3)]
  return [
    ...seek,
    '-i',
    '{input}',
    '-map',
    '0:v?',
    '-map',
    '0:a?',
    '-c',
    'copy',
    '-avoid_negative_ts',
    'make_zero',
    ...(MP4_FAMILY.test(output) ? ['-use_editlist', '0'] : []),
    output,
  ]
}

With the new command, the same AVI kept all 600 frames. The -use_editlist 0 near the end is there because of B-frames too. As the table above shows, in B-frame video the first frame is displayed slightly after zero. If an MP4 gets an “edit list”, some players show that gap as a black screen, so it is left out.

For the same reason, the precise cut moves the seek after the input when the start is near the beginning. Seeking after the input makes ffmpeg decode from the start and count the timestamps itself, so it doesn’t miss frames even in an AVI with unreliable timing.

In short

  • Video is stored as keyframes (whole pictures) and correction notes (P- and B-frames). The notes can’t be drawn on their own.
  • A stream copy starts cleanly only at a keyframe. Back up and the clip starts early; don’t, and the picture freezes until the next keyframe.
  • B-frames make storage order differ from display order, and when that meets an old format like AVI, it can turn into missing frames.
  • Use a stream copy when you have room to spare, and re-encode when you need an exact frame.