Merge and concatenate videos with FFmpeg

Write the files into list.txt as file 'a.mp4' lines and run ffmpeg -f concat -i list.txt -c copy out.mp4. That copies the streams and only works when every clip has the same codec, size, frame rate and audio format. When they differ, use the concat filter and bring every clip to one size, frame rate and sample rate first.

Fast Stream copy

Every clip came from the same camera or the same encoder settings. Nothing is re-encoded.

ffmpeg -f concat -i list.txt -c copy out.mp4

a.mp4 then b.mp4, 20.02 s, both streams copied. The output is 920 bytes smaller than the two inputs together.

Length
20.02 s from two 10 s clips
Quality
Identical to the inputs, streams copied
Run time
98 ms
Output
5.9 MB
Any inputs Re-encode

The clips differ in size, frame rate, sample rate or channels. Re-encodes everything.

ffmpeg -i a.mp4 -i phone.mp4 -filter_complex "[1:v]scale=1280:546:force_original_aspect_ratio=decrease,pad=1280:546:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=24[v1];[1:a]aresample=48000,aformat=channel_layouts=stereo[a1];[0:v][0:a][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

14.0 s at 1280x546, 24 fps, AAC 48 kHz stereo. The vertical clip sits in the middle with black bars on both sides.

Length
14.0 s from 10 s plus 4 s
Output format
1280x546, 24 fps, 48 kHz stereo
Run time
1.4 s
Output
3.0 MB

Proof

What each join looks like

a.mp4 and b.mp4 share every setting. phone.mp4 is a vertical 30 fps clip with mono 44.1 kHz audio, the kind of file a phone hands you.
-f concat, -c copy, matching clips Clean cut, nothing re-encoded
Frames 239 and 240 of the stream-copied join: the last frame of the girl in the alley, then the first frame of the dragon on the roof
frame 240 Frame 239 is the last of a.mp4 and frame 240 the first of b.mp4. The join lands exactly at 0:10.
concat filter, scaled and padded One size, one frame rate, one audio format
The vertical phone clip at 0:12 of the filtered join, centred in a 1280x546 frame with black bars left and right
0:12 scale fits the 720x1280 clip inside 1280x546, pad centres it, fps=24 and aresample=48000 match the first clip.
-f concat, -c copy, mixed clips No error, broken file
The vertical phone clip squashed flat into a wide frame, from the stream-copied join of mismatched files
17.5 s Frames after 0:10 are 720x1280 inside a track that declares 1280x546, drawn here at the declared size. The file claims 17.5 s for 14 s of footage and its audio stops at 12.9 s.
xfade + acrossfade Blended over 1 s
Halfway through a one-second crossfade: the girl in the alley and the dragon on the roof blended together
0:09.5 The middle of the crossfade task below. Both clips are on screen at half strength.

Footage: Sintel, © Blender Foundation, CC BY 3.0.

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: {
"a.mp4": "https://cdn.rendobar.com/examples/concat-videos/a.mp4",
"phone.mp4": "https://cdn.rendobar.com/examples/concat-videos/phone.mp4",
},
params: {
command: "ffmpeg -i a.mp4 -i phone.mp4 -filter_complex \"[1:v]scale=1280:546:force_original_aspect_ratio=decrease,pad=1280:546:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=24[v1];[1:a]aresample=48000,aformat=channel_layouts=stereo[a1];[0:v][0:a][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",
},
});
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": {
"a.mp4": "https://cdn.rendobar.com/examples/concat-videos/a.mp4",
"phone.mp4": "https://cdn.rendobar.com/examples/concat-videos/phone.mp4"
},
"params": {
"command": "ffmpeg -i a.mp4 -i phone.mp4 -filter_complex \"[1:v]scale=1280:546:force_original_aspect_ratio=decrease,pad=1280:546:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=24[v1];[1:a]aresample=48000,aformat=channel_layouts=stereo[a1];[0:v][0:a][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"
}
}'

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. Join two files without a list file

    Re-encode
    ffmpeg -i a.mp4 -i b.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

    The same 20 s join as the list file, re-encoded. Name every input with -i and set n to the number of clips.

    2.0 s run 6.0 MB 20.01 s long

  2. Join with a crossfade

    Re-encode
    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]" -c:v libx264 -crf 20 -c:a aac out.mp4

    19.0 s, one second shorter than a hard cut, because the clips overlap for the fade. offset is the first clip's length minus duration.

    2.4 s run 6.8 MB 19.01 s long

  3. Join a clip that has no audio track

    Re-encode
    ffmpeg -i a.mp4 -i mute.mp4 -f lavfi -t 10 -i anullsrc=r=48000:cl=stereo -filter_complex "[0:v][0:a][1:v][2:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

    20.0 s with sound for the first 10 s and silence for the second. anullsrc stands in for the missing track, cut to the silent clip's 10 s.

    1.9 s run 5.9 MB 20.01 s long

  4. Join only the audio of the clips

    Stream copy
    ffmpeg -f concat -i list.txt -vn -c:a copy out.m4a

    20.02 s of AAC from both files, copied, no video.

    92 ms run 325 KB 20.02 s long

Gotchas

Where it goes wrong

Why does concat say "Unsafe file name"?

The concat demuxer rejects any path in the list that starts with / or ., or holds a character other than letters, digits, _, -, . and /. Spaces, drive letters, absolute paths and URLs all count. So does the ./ that find . puts in front of every name:

ffmpeg -f concat -i list-find.txt -c copy out.mp4

Unsafe file name './a.mp4', then "Error opening input: Operation not permitted". No output.

Add -safe 0 before -i to accept any path. The same list then joined into a 20.02 s file. Or write plain relative names, as in list.txt, and leave the check on.

Can I join clips of different sizes or frame rates with -c copy?

No. The concat demuxer does not check, so it gives you a file instead of an error. Joining a.mp4 (1280x546, 24 fps, 48 kHz stereo) with phone.mp4 (720x1280, 30 fps, 44.1 kHz mono):

ffmpeg -f concat -i list-mixed.txt -c copy out.mp4

Exit code 0 and a run of "Non-monotonic DTS" warnings. The file claims 17.5 s for 14 s of footage, the audio stops at 12.9 s, and the vertical frames sit in a track that says 1280x546.

Re-encode with the concat filter instead, after bringing every clip to the first one's size, frame rate, sample rate and channel layout. That is the second command at the top of this page.

"Input link parameters do not match": what does concat want?

The concat filter needs every video input at the same size and sample aspect ratio. Feed it the raw clips and it stops before writing a frame:

ffmpeg -i a.mp4 -i phone.mp4 -filter_complex "[0:v][0:a][1:v][1:a]concat=n=2:v=1:a=1[v][a]" -map "[v]" -map "[a]" out.mp4

Input link in0:v0 parameters (size 720x1280, SAR 1:1) do not match the corresponding output link in0:v0 parameters (1280x546, SAR 1:1).

Same size is not enough. scale adjusts the sample aspect ratio to keep the shape, so a scaled clip can come out at 1280x546 with a SAR like 2457:2456 and fail the same way. That is why the command at the top ends the chain with setsar=1.

Does the concat: protocol work with MP4?

No, and it fails quietly. concat: glues the files byte for byte, which suits MPEG-TS but not MP4, where each file carries its own index.

ffmpeg -i "concat:a.mp4|b.mp4" -c copy out.mp4

Exit code 0 and a 10.0 s file: a.mp4 only. b.mp4 was dropped without a warning.

For MP4 files, use the concat demuxer with a list file, or the concat filter.

Related: Trim a video, Fade in and out, Resize a video, Change frame rate, Create a blank video.

Your command. Our infrastructure.

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