Skip to content
systemcoding.dev
Media

Trim a Video Accurately With FFmpeg -ss, -to and -t

Cut a clip from a video with FFmpeg and know when -ss, -to and -t land on the exact frame, and when stream copy snaps to the nearest keyframe.

6 min read
Summarize in AI
Flat illustration of video frames passing through a codec, with the FFmpeg, C and VLC media player logos.

Trimming looks like the simplest FFmpeg job, and it is also where people get the wrong clip. The cut starts a few seconds early, a clip has a frozen frame at the start, or the audio drifts. All three come from the same three options, -ss, -to and -t, and from where you place them relative to -i. This post explains what each option means in FFmpeg 7.1, shows the fast copy cut and the accurate re-encode cut side by side, and gives you a rule for choosing between them.

The three options

OptionMeaning
-ss POSITIONStart position: seek to this time
-to POSITIONStop at this timestamp
-t DURATIONStop after this much time has been written

Times are written as plain seconds (90, 12.5) or as HH:MM:SS.mmm (00:01:30). -to and -t are mutually exclusive, and FFmpeg's manual says -t takes priority if both are given. Use one of them.

The difference between -to and -t is the difference between an end time and a length. "From 1:00 to 1:30" is -ss 60 -to 90. "Thirty seconds starting at 1:00" is -ss 60 -t 30.

Where to place -ss

The same option does two different things depending on position, as the stage model suggests.

As an input option, placed before -i, it seeks inside the demuxer before decoding:

Shell
ffmpeg -ss 60 -i input.mp4 -t 30 -c:v libx264 -crf 20 -c:a aac clip.mp4

As an output option, placed after -i, it decodes from the start of the file and throws away everything before the position:

Shell
ffmpeg -i input.mp4 -ss 60 -t 30 -c:v libx264 -crf 20 -c:a aac clip.mp4

Both produce a 30 second clip, but the input form is much faster on a long file, because FFmpeg jumps to the nearest keyframe before the target and does not decode the first minute. Since FFmpeg 2.1, input seeking with re-encoding is also frame-accurate: FFmpeg seeks to the keyframe, decodes forward, and discards frames until the exact position. The output form has the same accuracy and is slow for large offsets. For re-encoded clips, put -ss before -i.

One consequence: with input seeking, timestamps in the output restart from zero, so -to refers to the output timeline. In ffmpeg -ss 60 -i input.mp4 -to 90, the clip is 90 seconds long, not 30, because -to 90 is read as a position in the output. This surprises almost everyone. Use -t 30 with an input -ss, which cannot be misread, or place -to after -i and -ss before it and remember that the timeline began at the seek point.

The fast cut: stream copy

If you do not need to re-encode, copy the streams:

Shell
ffmpeg -ss 60 -i input.mp4 -t 30 -c copy -avoid_negative_ts make_zero clip.mp4

This runs in seconds and loses no quality. The catch is the keyframe. Compressed video stores a full picture only at keyframes; the frames between depend on earlier ones. A stream copy cannot start in the middle of a group of pictures, so the cut begins at the keyframe on or before your -ss position. If keyframes sit every ten seconds, your clip may start up to ten seconds early.

FFmpeg handles the frames before your start time in a copy as follows: they are kept in the file but flagged so that players skip them, which can show as a frozen first frame, or a short black start in some players. The -avoid_negative_ts make_zero option shifts timestamps so the clip starts at zero and removes most of that glitch.

You can find the keyframes of a file with ffprobe, which helps you choose a start time that lands on one:

Shell
ffprobe -v error -select_streams v:0 -skip_frame nokey -show_entries frame=pts_time -of csv=p=0 input.mp4
Text
0.000000
10.010000
20.020000
30.030000

Your list will differ. Each line is the presentation time in seconds of one keyframe. If you cut at 10.01 with -c copy, you get a clean start. If you cut at 14, you get the clip from 10.01.

The accurate cut: re-encode

To start at any frame you choose, re-encode the video and, if you like, copy the audio:

Shell
ffmpeg -ss 00:01:14.250 -i input.mp4 -t 8.5 -c:v libx264 -crf 18 -preset medium -c:a copy clip.mp4

FFmpeg decodes from the previous keyframe, drops the frames before 1:14.250, and encodes the rest, so the first frame of clip.mp4 is the frame at that time. The cost is encoding time and a small generational loss, which a low CRF such as 18 keeps minor.

Mixing the two gives a good compromise on long files: cut with -c copy to a rough segment that begins slightly early, then trim that small file accurately. Two steps, and the slow one works on a few seconds.

A decision rule

You needUse
Fastest, no quality loss, a few seconds of slack at the start-ss before -i, -c copy
Frame-exact start and end-ss before -i, re-encode with libx264
Cut at known keyframes-c copy with times from ffprobe
Remove a section from the middleTwo clips and the concat demuxer

Checking the result

Use ffprobe to read the duration of what you made:

Shell
ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 clip.mp4
Text
8.500000

A re-encoded clip should report about the length you asked for, within a frame or two. A copy clip may differ by the keyframe slack described above. We keep that check in our trimming scripts, because a quiet drift of a few seconds is easy to miss when you only watch the first ten seconds of a video.

Common mistakes

  • Writing -ss 1:30 and meaning something else. FFmpeg reads 1:30 as one minute thirty seconds, but 90 is clearer and cannot be misread. Prefer plain seconds in scripts.

  • Placing -to after an input -ss and expecting an absolute time. Use -t for clips.

  • Using -c copy and expecting a frame-exact start. Copy snaps to keyframes.

  • Forgetting -avoid_negative_ts make_zero on a copy cut, then finding a frozen first frame.

Trimming several clips from one file

If you need many clips from one long recording, run one command per clip and keep the source open only for reading. Each command seeks on its own, so the cost of the first minute is not paid again. A small shell function keeps the times readable:

Shell
cut() { ffmpeg -hide_banner -ss "$2" -i "$1" -t "$3" -c:v libx264 -crf 20 -c:a aac "$4"; }
cut talk.mp4 00:05:10 45 intro.mp4
cut talk.mp4 00:41:00 90 demo.mp4

The function takes the input, the start, the length and the output name. Both calls re-encode, so each clip starts exactly where you said. Replace the encoder options with -c copy for rough cuts.

Audio needs care when you copy. AAC packets are short, so audio usually starts within a frame of the point you asked for, but video snaps to a keyframe, and the two can start at slightly different times. Players then show a small offset at the start of the clip. Re-encoding the video fixes it, and that is the reason we re-encode every clip that will go to an editor.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.