Ffmpeg Cut Concat
Context: $ARGUMENTS
Quick start
- Fast trim (keyframe-aligned): → Step 1 → trim mode with
-c copy - Frame-accurate trim: → Step 1 → trim mode, re-encode (
--accurate) - Split one file into N pieces: → Step 1 → segment mode
- Join same-codec files: → Step 1 → concat-copy mode
- Join different-codec files: → Step 1 → concat-filter mode
When to use
- Extracting a clip between two timestamps (e.g.
00:01:30to00:02:00). - Splitting a long file into N-second chunks (podcast, HLS prep).
- Joining multiple recordings end-to-end.
- Removing the first/last N seconds of a video.
- Building a highlight reel from several source files.
Step 1 — Pick a strategy
| Goal | Mode | Command skeleton |
|---|---|---|
| Trim one clip from one file | trim | ffmpeg -ss T1 -i in -to T2 -c copy out |
| Split one file into equal pieces | segment | ffmpeg -i in -f segment -segment_time N out_%03d.mp4 |
| Join files with identical codecs | concat-copy | ffmpeg -f concat -safe 0 -i list.txt -c copy out |
| Join files with mismatched codecs | concat-filter | -filter_complex "concat=n=N:v=1:a=1" |
Do not use concat demuxer for files that differ in codec, resolution, SAR, fps, or sample rate — it will mux them but playback breaks silently. Use the concat filter instead.
Step 2 — Decide stream copy vs re-encode
- Stream copy (
-c copy) is ~100x faster and lossless, but cuts land on the nearest keyframe before the requested timestamp. A clip asked for at00:01:30.000may actually start at00:01:28.5. - Re-encode (drop
-c copy, add-c:v libx264 -c:a aac) is frame-accurate but slow and lossy. - Default rule: start with
-c copy; switch to re-encode only when the user says "frame-accurate," "exact," or the first frame looks wrong after a test.
Input-side vs output-side seek:
-ss T -i in.mp4(before-i) — input seek. Fast; ffmpeg seeks in the container to the keyframe at or before T, then starts decoding. With-c copy, output begins at that keyframe.-i in.mp4 -ss T(after-i) — output seek. Decodes from the start, discards frames until T. Slow but accurate. Rarely needed since modern ffmpeg does accurate input seek by default with-accurate_seek.
For accurate cut with re-encode, use input seek too — it is fast AND accurate once decoding is on.
Step 3 — Run the command
3a. Trim (stream copy, keyframe-aligned)
ffmpeg -ss 00:01:30 -i input.mp4 -to 00:02:00 -c copy -avoid_negative_ts make_zero out.mp4
-to is absolute input-time. If you wrote -ss 00:01:30 -to 00:02:00, you get 30 seconds out.
Use -t 30 instead of -to to specify duration directly (-t wins if both set).
3b. Trim (frame-accurate, re-encode)
ffmpeg -ss 00:01:30 -i input.mp4 -to 00:02:00 \
-c:v libx264 -crf 18 -preset veryfast \
-c:a aac -b:a 192k \
out.mp4
3c. Segment (split into N-second pieces)
ffmpeg -i input.mp4 -c copy -map 0 \
-f segment -segment_time 60 -reset_timestamps 1 \
out_%03d.mp4
Segments still break on keyframes. If you need exact 60.000s pieces, re-encode with forced keyframes: -force_key_frames "expr:gte(t,n_forced*60)".
3d. Concat demuxer (same codec params)
Create list.txt:
file 'clip1.mp4'
file 'clip2.mp4'
file 'clip3.mp4'
Run:
ffmpeg -f concat -safe 0 -i list.txt -c copy out.mp4
-safe 0 allows absolute paths and .. — needed for anything except plain filenames in CWD.
3e. Concat filter (different codecs / resolutions)
ffmpeg -i a.mp4 -i b.mp4 -i c.mp4 \
-filter_complex "[0:v][0:a][1:v][1:a][2:v][2:a]concat=n=3:v=1:a=1[v][a]" \
-map "[v]" -map "[a]" \
-c:v libx264 -crf 18 -c:a aac -b:a 192k \
out.mp4
Each input contributes v video streams and a audio streams — reference them in order [i:v][i:a] for i=0..n-1.
Step 4 — Verify duration
ffprobe -v error -show_entries format=duration -of default=nw=1:nk=1 out.mp4
For stream copy, the reported duration may be a bit shorter or longer than requested due to keyframe alignment. If the discrepancy is more than ~2s, your GOP size is large — re-encode.
Available scripts
scripts/cut.py— single entrypoint for all four modes (trim,segment,concat-copy,concat-filter) with--dry-runand--verbose.
Workflow
# Fast trim
uv run ${CLAUDE_SKILL_DIR}/scripts/cut.py trim \
--input in.mp4 --start 00:01:30 --end 00:02:00 --output clip.mp4
# Frame-accurate
uv run ${CLAUDE_SKILL_DIR}/scripts/cut.py trim \
--input in.mp4 --start 00:01:30 --end 00:02:00 --accurate --output clip.mp4
# Split into 60s chunks
uv run ${CLAUDE_SKILL_DIR}/scripts/cut.py segment \
--input in.mp4 --seconds 60 --pattern out_%03d.mp4
# Concat same-codec files
uv run ${CLAUDE_SKILL_DIR}/scripts/cut.py concat-copy \
--inputs a.mp4 b.mp4 c.mp4 --output joined.mp4
# Concat mixed-codec files (re-encodes)
uv run ${CLAUDE_SKILL_DIR}/scripts/cut.py concat-filter \
--inputs a.mp4 b.webm --output joined.mp4