Trim a video with FFmpeg

Put -ss before -i. Add -c copy for an instant cut that starts on the nearest keyframe, or re-encode for a cut that starts on the exact frame. Every command on this page ran on the same 2-minute clip, and the results below are what came out.

Fast Stream copy

Instant and lossless. Starts on the nearest keyframe, which players hide.

ffmpeg -ss 65 -i in.mp4 -t 30 -c copy out.mp4

Plays from 1:05. The file also holds the 5 s back to the 1:00 keyframe, hidden by an edit list.

First frame
1:05 in players, 1:00 in the file
Run time
101 ms
Output
13.6 MB
Exact Re-encode

Starts on the exact frame everywhere. Re-encodes, so it takes longer.

ffmpeg -ss 65 -i in.mp4 -t 30 -c:v libx264 -crf 20 -c:a aac out.mp4

Starts on frame 1560, exactly 1:05.000, and runs exactly 30.000 s.

First frame
1:05.000, frame 1560
Run time
3.0 s
Output
12.5 MB

Proof

The first frame of each cut

The test clip has its timecode burned in and a keyframe every 10 seconds. Each cut asked for 30 seconds from 1:05.
Re-encode, -ss before -i Starts on the frame you asked for
First frame of the re-encoded cut: timecode 1:05.000, frame 1560
1:05 1:05.000 exactly. FFmpeg decodes from the 1:00 keyframe and throws away the frames before 1:05.
-c copy, -ss before -i Starts on the keyframe before
First frame stored in the stream-copied cut: timecode 1:00.000, frame 1440
1:00 The file begins at the 1:00 keyframe. An edit list tells players to skip 5 s, so Chrome starts at 1:05. A tool that ignores edit lists shows the extra 5 s.
-c copy, -ss after -i Frozen for the first 5 s
First picture of the cut with -ss after -i: timecode 1:10.000, frame 1680
1:10 The audio starts at 1:05 but the video cannot start before a keyframe, so the first picture is 1:10. Chrome holds that frame for 5 s while the audio plays.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Timecode added by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/trim-video/in.mp4",
},
params: {
command: "ffmpeg -ss 65 -i in.mp4 -t 30 -c:v libx264 -crf 20 -c:a aac out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/trim-video/in.mp4"
},
"params": {
"command": "ffmpeg -ss 65 -i in.mp4 -t 30 -c:v libx264 -crf 20 -c:a aac out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Cut from one time to another

    Stream copy
    ffmpeg -ss 65 -to 95 -i in.mp4 -c copy out.mp4

    The same file as -t 30, byte for byte. -to before -i is a position in the input.

    90 ms run 13.6 MB 30.21 s long

  2. Keep the first 10 seconds

    Stream copy
    ffmpeg -i in.mp4 -t 10 -c copy out.mp4

    0:00 to 0:10. The start is a keyframe, so stream copy is exact here.

    79 ms run 1.8 MB 10.08 s long

  3. Keep the last 10 seconds

    Stream copy
    ffmpeg -sseof -10 -i in.mp4 -c copy out.mp4

    1:50 to 2:00. -sseof counts back from the end, so you never need the duration.

    77 ms run 2.2 MB 10.00 s long

  4. Remove a section from the middle

    Re-encode
    ffmpeg -i in.mp4 -filter_complex "[0:v]trim=0:30,setpts=PTS-STARTPTS[v0];[0:a]atrim=0:30,asetpts=PTS-STARTPTS[a0];[0:v]trim=60,setpts=PTS-STARTPTS[v1];[0:a]atrim=60,asetpts=PTS-STARTPTS[a1];[v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

    0:30 to 1:00 gone, 90.0 s left, picture and sound still in step.

    8.1 s run 26.6 MB 90.00 s long

  5. Join several clips into one

    Re-encode
    ffmpeg -i in.mp4 -filter_complex "[0:v]trim=10:15,setpts=PTS-STARTPTS[v0];[0:a]atrim=10:15,asetpts=PTS-STARTPTS[a0];[0:v]trim=40:45,setpts=PTS-STARTPTS[v1];[0:a]atrim=40:45,asetpts=PTS-STARTPTS[a1];[v0][a0][v1][a1]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

    0:10 to 0:15, then 0:40 to 0:45, in one 10.0 s file.

    1.2 s run 1.9 MB 10.00 s long

  6. Cut the audio only

    Stream copy
    ffmpeg -ss 65 -i in.mp4 -t 30 -vn -c:a copy out.m4a

    30.0 s of AAC, copied, no video. Audio frames are 21 ms, so copy cuts close to exact.

    80 ms run 579 KB 30.02 s long

  7. Split into equal parts

    Stream copy
    ffmpeg -i in.mp4 -map 0 -c copy -f segment -segment_time 30 -reset_timestamps 1 part_%03d.mp4

    Four 30 s files, part_000 to part_003. Each part starts on a keyframe, so each cut is exact.

    114 ms run 29.9 MB 120.00 s long

Gotchas

Where it goes wrong

Put -ss before -i or after it?

Before. With -ss before -i, FFmpeg jumps straight to the time in the input. With it after, FFmpeg reads from the start of the file and drops everything until it gets there.

With -c copy the difference shows up in the picture. Copied video can only start on a keyframe. Before -i, FFmpeg starts at the keyframe before your time and writes an edit list so players skip ahead. After -i, the audio starts on time and the video waits for the next keyframe. On the test clip the sound started at once and the picture 5 s later, at 1:10.

With a re-encode, -ss before -i is exact as well as fast. FFmpeg decodes from the keyframe and discards the frames before your time, so the first frame written is the one you asked for.

-t or -to?

-t 30 is a duration: keep 30 seconds. -to 95 is a position, and which timeline it counts on depends on where you put it.

Before -i, -to is a time in the input. -ss 65 -to 95 -i in.mp4 gave the same file as -ss 65 -i in.mp4 -t 30, byte for byte. After -i, it counts on the output, which -ss has already reset to zero:

ffmpeg -ss 65 -i in.mp4 -to 95 -c copy out.mp4

55 s, not 30. It ran to the end of the clip, because 95 s of output was more than was left.

Use -t when you know how long the clip is, and put -to before -i when you know where it ends.

Why does my cut start black, frozen or out of sync?

Almost always a -c copy cut that does not start on a keyframe. The fix depends on what you need:

  • A clip that plays right in a browser or player: keep -c copy and move -ss before -i. It skips decoding and encoding, so it finishes long before the re-encode.
  • A clip that starts on the exact frame everywhere, including editors and tools that ignore edit lists: re-encode. That is the only way to start between keyframes.

To see where a copy cut can start, list the keyframes first:

ffprobe -select_streams v -skip_frame nokey -show_entries frame=pts_time -of csv in.mp4

On the test clip: 0:00, 0:10, 0:20 and every 10 s to 1:50. A copy cut starting on any of those times is exact.

Related: Merge videos, Extract a thumbnail, Extract audio.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.