DEV Community

Joining video clips with ffmpeg: when you can copy and when you have to re-encode

Joining two videos sounds like the simplest thing ffmpeg can do, and sometimes it is. But the same command that works perfectly for two clips from your phone produces a broken file, a frozen picture or an error for clips from different sources.

Here's why, and what to use in each case.

Case 1: the clips match, so copy them

If every clip has the same video codec, resolution, frame rate, pixel format and audio format, you can use the concat demuxer and copy the streams. Nothing is re-encoded, so it's lossless and fast.

# list.txt
file 'clip1.mp4'
file 'clip2.mp4'
file 'clip3.mp4'
ffmpeg -f concat -safe 0 -i list.txt -c copy joined.mp4

Before trusting it, check the clips really match:

for f in clip*.mp4; do ffprobe -v error -select_streams v:0 \
-show_entries stream=codec_name,width,height,r_frame_rate,pix_fmt \
-of csv=p=0 "$f" done

If any line differs, stream copy can still "succeed" and give you a file that stutters, freezes or plays the second clip at the wrong size. Don't copy mismatched clips.

Case 2: the clips differ, so re-encode through the concat filter

The concat filter decodes everything and joins frames, which means every input must have the same size and sample aspect ratio first. Scale and pad each clip into one frame:

ffmpeg -i a.mp4 -i b.mov -filter_complex " [0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30[v0]; [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30[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 joined.mp4

A few things that trip people up:

  • A clip with no audio track breaks concat=...:a=1. Give it a silent track first, for example with anullsrc, trimmed to the clip's length.
  • Upright phone clips are usually stored landscape with a rotation flag. ffmpeg applies the rotation when it decodes, so check the real displayed size with ffprobe rather than assuming.
  • Different audio sample rates are fine in the filter graph, but you may want aresample=48000 on each input to keep things predictable.

Crossfades

For a crossfade instead of a hard cut, xfade (video) and acrossfade (audio) replace concat. The offset is when the fade starts, which is the first clip's length minus the fade length:

# clip a is 10 s long, 1 s fade
ffmpeg -i a.mp4 -i b.mp4 -filter_complex " [0:v][1:v]xfade=transition=fade:duration=1:offset=9[v]; [0:a][1:a]acrossfade=d=1[a]" -map "[v]" -map "[a]" out.mp4

Both inputs to xfade must already share size, frame rate and pixel format, so the scale/pad/fps step from case 2 still applies.

The rule of thumb

Probe first. If everything matches, copy. If anything differs, normalise size, aspect ratio and frame rate, then re-encode once.

Disclosure: I run GoToolsOnline. If you'd rather not build filter graphs by hand, its free Merge Videos tool does this in the browser: it joins 2 to 20 clips into one MP4, copies without re-encoding when the clips match, and otherwise fits them into 16:9, 9:16 or square with optional crossfades: https://gotoolsonline.com/tools/merge-videos

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.