Guide · How video works

Keyframes explained: why a video trim snaps

9 min read Updated By the ClipHeron team Sources cited

You drag the start handle to 12.5 seconds, press Trim, and the clip begins at 12.0. No tool is being sloppy; the video itself only allows cuts in certain places. Understanding why takes five minutes and makes every video editor you will ever use behave predictably. This guide explains keyframes and GOPs with a diagram, and shows when it is worth re-encoding a second or two to get a cut to the frame.

Three kinds of frame

A video codec such as H.264 does not store a sequence of pictures. It stores one complete picture now and then, and describes every other frame in terms of its neighbours.

  • I-frames (intra frames, keyframes) are complete pictures compressed on their own, like a JPEG. They are large, and a decoder can start from one with no other information.
  • P-frames (predicted frames) describe how a frame differs from an earlier one: this block moved three pixels left, that block is unchanged, this region has these new pixels. They are typically a fifth to a tenth the size of an I-frame.
  • B-frames (bi-directional frames) describe a frame using both an earlier and a later frame, and are smaller still. Because they refer forwards, the frames are stored in the file in a different order from the order they are shown, which is why video files carry two timestamps per frame.

A P-frame two seconds after the last I-frame can only be decoded if you have that I-frame and every frame between them, because each one builds on the last. Start reading the stream in the middle and the first frames you decode have nothing to stand on: the player shows grey blocks, smears, or nothing at all until the next I-frame arrives and the picture snaps into place. If you have ever skipped forward in a badly made stream and seen the picture dissolve for a moment, that is what you were watching.

The GOP, and how long it is

The stretch from one keyframe to the frame before the next is a group of pictures, or GOP. Its length is chosen by whatever encoded the video. Phone cameras write a keyframe every one to two seconds, so that scrubbing in the Photos app is quick. Streaming services use two to four seconds, balancing seek speed against size, since every I-frame costs many P-frames’ worth of bytes. Screen recording software often uses long GOPs of ten seconds or more, because a screen barely changes and long GOPs make much smaller files; some recordings have a single keyframe at the very start.

The GOP length is the worst-case distance between where you want to cut and where a lossless cut can begin. On phone footage that is a second or two; on a screen recording it can be the whole file.

Where a cut can land

Two GOPs with a requested cut point between keyframesA row of frames: a keyframe at 10 seconds followed by P and B frames, then a keyframe at 12 seconds followed by more P and B frames. A requested cut at 12.5 seconds falls between frames. A lossless cut snaps back to the keyframe at 12 seconds; an exact cut re-encodes the frames from 12.5 seconds up to the next keyframe at 14 seconds.GOP 1 (keyframe every 2 s)GOP 2IBBPBBPBBPBBIBBPBBPBBPBB10.0 s12.0 s14.0 s →requested cut 12.5 slossless: snap back 0.5 sexact: re-encode 1.5 s to the next keyframe, copy the rest
Frames stored in a video: I (keyframe), P (predicted) and B (bi-directional). A cut requested at 12.5 s cannot start on a P or B frame. Diagram by ClipHeron, reusable under CC BY 4.0 with a link to this page.

Reading the diagram: copying the stream from 12.5 s would start with a P-frame that refers to the keyframe at 12.0 s, which would not be in the new file. So a copy must start at 12.0 s. The alternative is to decode the frames from 12.5 s to the next keyframe at 14.0 s, encode them afresh as a new little GOP that begins with its own keyframe, and then copy the original stream from 14.0 s onwards. The end of the clip has the mirror-image problem, with B-frames that refer forward to frames you are discarding.

Lossless versus exact

A lossless trim snaps the start back to the keyframe at or before your requested time and copies the compressed frames into a new file, rewriting only the index and timestamps. No frame is decoded or encoded. It takes about as long as copying the kept bytes, the picture is identical to the original, and the start may be up to one GOP earlier than you asked. For most purposes, half a second of extra lead-in is a fair price for perfect quality in two seconds.

An exact cut re-encodes only the head segment (from your requested start to the next keyframe) and the tail segment (from the last keyframe before your requested end to the end) and copies everything in between. With settings matched to the source, the join is invisible and the quality change is confined to a second or two. It costs a short encode and needs an encoder for the source’s codec, which is why ClipHeron falls back to re-encoding the whole range, with a notice, when the stream cannot be matched.

Neither approach is better; they trade precision for time and purity. What matters is that the tool tells you which it did and where the cut actually landed, in words, not just as a tick on a timeline.

What happens to the audio

Audio has frames too: AAC packs 1,024 samples into each one, about 21 milliseconds at 48 kHz, and Opus uses 20 ms by default. A lossless cut starts the audio at the nearest audio frame boundary, which can be up to 21 ms away from the video’s start, and writes an edit list into the file that tells players to shift the audio by that amount so the two stay in sync. AAC also has a priming delay of 1,024 silent samples at the very start that the same edit list hides. Done properly, you will not hear or see the join; done carelessly, lips drift by a frame.

In practice

  • For sharing a moment from phone footage, use lossless. The snap is a second at most and the quality is untouched.
  • For a precise edit (a cut on a beat, a clip that must start on a specific word), use exact and accept a second or two of re-encoding.
  • For a long screen recording with one keyframe, lossless can only start at zero; use exact, or re-encode the whole range if the tool says it must.
  • Cut before you compress. A trimmed file hands the compressor fewer seconds to spread the bytes over, which keeps more resolution at the same target size.
  • For a GIF, the same timeline and the same snap rules apply, except that every frame is decoded anyway, so the GIF maker starts exactly where you asked.

The ClipHeron trimmer draws the keyframes as ticks on the timeline, prints the real start time under the handles (“starts 0.5 s earlier at 00:00:12.000”), and lets you switch between lossless and exact with one click.

Trim a video, with the snap shown

Frequently asked questions

What is a keyframe?

A frame stored as a complete picture, with no reference to any other frame. Also called an I-frame or intra frame. Every other frame in the stream is described relative to frames near it, so decoding can only begin at a keyframe.

How often are keyframes in my video?

Phone cameras typically write one every one to two seconds; streaming encoders every two to four; screen recorders sometimes only every ten seconds or at the very start. The timeline in the trimmer draws a tick for each one once the file is indexed.

Why does a lossless trim start earlier than I asked?

Because copying frames has to begin at a keyframe, and the trimmer picks the one at or before your start. The hint under the timeline tells you the exact time and the difference, so you can accept it or switch to the exact cut.

Does the exact cut lose quality?

Only in the frames it re-encodes: the stretch from your requested start to the next keyframe, and a similar stretch at the end. Those are encoded with settings matched to the source. Everything in between is copied byte for byte.

Can I force a keyframe where I want one?

Not without re-encoding; keyframe positions are decided when the video is encoded. Recording tools and encoders have a keyframe interval setting, so for footage you will cut often, a short interval (one to two seconds) makes lossless trimming more precise.

Sources

This guide explains how things work in general terms. Platform limits and codec support change; the figures were checked on the date shown above. Spotted something out of date? Email errors@clipheron.com and we will check it against the source.