ffmpegvideo-trimmingapitutorial

How to Trim and Cut Video with FFmpeg (CLI + API Guide)

·Javid Jamae·7 min read
How to Trim and Cut Video with FFmpeg (CLI + API Guide)

You need to extract a 30-second clip from a 2-hour video. Or trim the first 5 seconds of dead air. Or cut everything after the outro. FFmpeg handles all of this, but the syntax trips people up more than it should.

This guide covers every way to trim and cut video with FFmpeg, from basic -ss/-t flags to keyframe-accurate cutting. Then we'll show how to do the same thing with a single API call using FFmpeg Micro.

How to Trim Video with FFmpeg Using -ss and -t

The two core flags for trimming are -ss (seek to a start time) and -t (set the duration).

To extract 30 seconds starting at the 1-minute mark:

ffmpeg -ss 00:01:00 -t 00:00:30 -i input.mp4 -c copy output.mp4

Breaking this down:

  • -ss 00:01:00 seeks to 1 minute into the video
  • -t 00:00:30 limits the output to 30 seconds
  • -c copy copies the streams without re-encoding (fast, but see the gotchas below)

You can also use seconds instead of timestamps. This does the same thing:

ffmpeg -ss 60 -t 30 -i input.mp4 -c copy output.mp4

FFmpeg -ss Before vs After -i (This Matters)

Where you place -ss relative to -i changes how FFmpeg seeks.

**Before -i (input seeking, fast):**

ffmpeg -ss 00:01:00 -i input.mp4 -t 30 -c copy output.mp4

FFmpeg seeks to the nearest keyframe before your timestamp, then starts decoding. This is fast because it doesn't decode the entire file from the beginning.

**After -i (output seeking, accurate):**

ffmpeg -i input.mp4 -ss 00:01:00 -t 30 -c copy output.mp4

FFmpeg decodes from the start and discards frames until it hits your timestamp. Slower, but frame-accurate.

For most use cases, put -ss before -i. It's fast and accurate enough. If you need frame-perfect cuts (like editing dialogue), put it after -i and drop -c copy so FFmpeg re-encodes:

ffmpeg -i input.mp4 -ss 00:01:00 -t 30 -c:v libx264 -c:a aac output.mp4

How to Cut Video with FFmpeg Using -to

The -to flag specifies an end timestamp instead of a duration. To extract from 1:00 to 1:30:

ffmpeg -ss 00:01:00 -to 00:01:30 -i input.mp4 -c copy output.mp4

The difference between -t and -to: -t 30 means "30 seconds of output." -to 00:01:30 means "stop at the 1:30 mark of the input." If you use -ss with -to, the -to timestamp is relative to the seeked position when -ss is before -i.

Stream Copy vs Re-encode: The Trim Quality Tradeoff

When you trim with -c copy, FFmpeg doesn't re-encode. It just copies the compressed data. This is 10-100x faster, but there's a catch.

Video is compressed in groups of pictures (GOPs), and each GOP starts with a keyframe. When you seek to a specific time, the nearest keyframe might be a few seconds earlier. With -c copy, your trim point snaps to that keyframe, so the actual start time might not match your -ss value exactly.

If precise cuts matter, re-encode:

ffmpeg -ss 00:01:00 -t 30 -i input.mp4 -c:v libx264 -crf 23 -c:a aac -b:a 192k output.mp4

This decodes and re-encodes every frame, giving you an exact cut at the cost of processing time and a slight quality change.

**When to use -c copy:** Quick rough cuts, splitting long recordings, removing dead air at the start or end. Speed matters more than frame accuracy.

When to re-encode: Editing dialogue, creating clips for social media, any time a few extra frames at the start would look wrong.

Common Gotchas When Trimming with FFmpeg

Audio drift with stream copy. Some containers store audio and video with slightly different timebase references. When you -c copy a trim, the audio can shift by a fraction of a second relative to the video. Re-encoding fixes this, or you can re-encode just the audio: -c:v copy -c:a aac.

Container format matters. MP4 and MKV handle seeking differently. MP4 stores its index (moov atom) either at the start or end of the file. If the moov atom is at the end, FFmpeg has to read the entire file before it can seek. Use -movflags +faststart when outputting MP4 to move the moov atom to the front.

Negative timestamps. When trimming with -c copy and -ss before -i, FFmpeg might produce frames with negative timestamps. Add -avoid_negative_ts make_zero to fix this:

ffmpeg -ss 00:01:00 -t 30 -i input.mp4 -c copy -avoid_negative_ts make_zero output.mp4

Duration mismatch with -to and -ss before -i. When -ss appears before -i, the -to timestamp is relative to the seeked position, not the original file. So -ss 60 -to 90 gives you 30 seconds, not the segment from 1:00 to 1:30 as you might expect. If that's confusing, just use -t for duration instead.

How to Trim Video with FFmpeg Micro's API

All the examples above work great locally, but running FFmpeg on a server means managing binaries, scaling infrastructure, and handling edge cases yourself. FFmpeg Micro is a cloud API that handles all of this with a single HTTP request.

To trim a video to 30 seconds starting at the 1-minute mark:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/video.mp4"}],
    "outputFormat": "mp4",
    "options": [
      {"option": "-ss", "argument": "00:01:00"},
      {"option": "-t", "argument": "30"},
      {"option": "-c", "argument": "copy"}
    ]
  }'

You can also pass -ss and -t as input-level options to get the fast input-seeking behavior:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{
      "url": "https://example.com/video.mp4",
      "options": [
        {"option": "-ss", "argument": "00:01:00"},
        {"option": "-t", "argument": "30"}
      ]
    }],
    "outputFormat": "mp4",
    "options": [
      {"option": "-c", "argument": "copy"}
    ]
  }'

For frame-accurate trimming with re-encoding, drop the -c copy and specify codecs:

curl -X POST https://api.ffmpeg-micro.com/v1/transcodes \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "inputs": [{"url": "https://example.com/video.mp4"}],
    "outputFormat": "mp4",
    "options": [
      {"option": "-ss", "argument": "00:01:00"},
      {"option": "-t", "argument": "30"},
      {"option": "-c:v", "argument": "libx264"},
      {"option": "-crf", "argument": "23"},
      {"option": "-c:a", "argument": "aac"}
    ]
  }'

FFmpeg Micro handles the infrastructure, scaling, and edge cases. You get the full power of FFmpeg's trimming capabilities without managing servers.

For a deeper dive on FFmpeg encoding fundamentals, check out our Learn FFmpeg: Transcoding lesson.

FAQ

What's the difference between FFmpeg -t and -to?

-t sets a duration (how long the output should be). -to sets an end timestamp (where to stop). If you use -ss 60 -t 30, you get 30 seconds of output starting at the 1-minute mark. If you use -ss 60 -to 90, you also get 30 seconds, but -to 90 is interpreted relative to the seek position when -ss is placed before -i.

Can I trim video without re-encoding in FFmpeg?

Yes. Use -c copy to copy streams without re-encoding. This is fast but trims to the nearest keyframe, so the start point might be off by a few frames. For exact cuts, you need to re-encode with -c:v libx264 -c:a aac.

How do I trim the last 10 seconds off a video with FFmpeg?

First, get the total duration with ffprobe -v error -show_entries format=duration -of csv=p=0 input.mp4. Then use -t with the duration minus 10 seconds. There's no native "trim from end" flag in FFmpeg.

Does FFmpeg Micro support -to for trimming?

FFmpeg Micro supports -ss (seek) and -t (duration) for trimming. The -to flag is not currently in the supported options list. Use -t with the desired duration instead, which achieves the same result.

How do I trim multiple sections from a video?

You can't do it in a single FFmpeg command without -filter_complex. The simplest approach: run separate trim commands for each section, then concatenate the results. With FFmpeg Micro, you'd make separate API calls for each segment.

Ready to process videos at scale?

Start using FFmpeg Micro's simple API today. No infrastructure required.

Get Started Free