Ffmpeg cut concat

From damionrashford/media-os · View interactive version

Ffmpeg cut concat is an AI agent skill from the GitHub repository damionrashford/media-os. Trim, cut, split, segment, and concatenate media with ffmpeg (stream copy when possible, re-encode across cut boundaries). Use when the user asks to trim a video, cut a clip, extract a segment by timestamps, remove a section, split into parts, join/merge videos, concatenate files, or build a segment

How to use this skill

Skill file

Ffmpeg Cut Concat

Context: $ARGUMENTS

Quick start

When to use

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

Input-side vs output-side 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

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

Reference docs

Gotchas

Examples

Example 1: cut a 30s clip, fast

ffmpeg -ss 00:01:30 -i lecture.mp4 -to 00:02:00 \
  -c copy -avoid_negative_ts make_zero \
  highlight.mp4

Example 2: remove first 10 seconds, frame-accurate

ffmpeg -ss 10 -i raw.mov \
  -c:v libx264 -crf 18 -preset veryfast -c:a aac -b:a 192k \
  trimmed.mp4

Example 3: split a podcast into 10-minute chunks

ffmpeg -i podcast.m4a -c copy -f segment -segment_time 600 \
  -reset_timestamps 1 part_%02d.m4a

Example 4: join three identical-format clips

cat > list.txt <<'EOF'
file 'intro.mp4'
file 'main.mp4'
file 'outro.mp4'
EOF
ffmpeg -f concat -safe 0 -i list.txt -c copy final.mp4

Example 5: merge a 1080p mp4 and a 720p webm

ffmpeg -i a.mp4 -i b.webm \
  -filter_complex "[0:v]scale=1280:720,setsar=1[v0];[1:v]setsar=1[v1];[v0][0:a][v1][1:a]concat=n=2:v=1:a=1[v][a]" \
  -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac mix.mp4

Troubleshooting

Error: `Unsafe file name`

Cause: concat demuxer refusing a path outside CWD without -safe 0.

Solution: add -safe 0 before -i list.txt, or use filenames relative to CWD.

Output starts before the requested `-ss`, or has a black first frame

Cause: keyframe alignment with -c copy + negative initial PTS.

Solution: add -avoid_negative_ts make_zero. For exact start, drop -c copy and re-encode.

Concat demuxer output has frozen video or audio desync

Cause: input files differ in codec/resolution/fps/sar/sample rate.

Solution: probe them (ffprobe -show_streams), confirm mismatch, switch to concat filter with re-encode, or normalize inputs first with -c:v libx264 -vf "scale=W:H,fps=F,setsar=1" -ar 48000 -ac 2.

`Stream specifier ':a' matches no streams`

Cause: one input has no audio track, but your concat filter expects a=1.

Solution: either generate silence (-f lavfi -i anullsrc=cl=stereo:r=48000) for that input, or set a=0 and handle audio separately.

Segments are uneven lengths despite `-segment_time 60`

Cause: segmenter only breaks at keyframes; your GOP is longer than 60s or irregular.

Solution: re-encode with -force_key_frames "expr:gte(t,n_forced*60)" before segmenting (or in the same pass).

`[concat @ ...] DTS ... < ... out of order`

Cause: overlapping or decreasing timestamps when concatenating.

Solution: add -fflags +genpts before -i list.txt, or re-mux each input through -c copy first to normalize timestamps.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.