How to Stack 3 Videos Vertically Using FFmpeg vstack

This article provides a quick, step-by-step guide on how to combine three separate video files into a single, vertically stacked video using FFmpeg’s vstack filter. You will learn the exact command-line syntax for stacking videos with matching widths, how to handle audio, and how to scale videos of different sizes so they can be merged successfully.

The Basic vstack Command

To stack three videos vertically, they must share the exact same width. If your input videos already have matching widths, you can use the following basic FFmpeg command:

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]vstack=inputs=3[v]" -map "[v]" output.mp4

How it works: * -i input1.mp4 -i input2.mp4 -i input3.mp4: Imports the three source video files. * -filter_complex: Calls FFmpeg’s complex filtergraph, which is required when handling multiple inputs. * [0:v][1:v][2:v]: Selects the video streams from the first, second, and third input files. * vstack=inputs=3: Applies the vertical stack filter and specifies that there are 3 input streams. * [v]: Labels the output video stream. * -map "[v]": Maps the processed video stream to the final output file output.mp4.


How to Include Audio

The basic command above only outputs video. If you want to include audio in your stacked video, you have two primary options:

Option 1: Keep audio from the first video only

To use the audio track from the first input video (input1.mp4), add the -map 0:a flag:

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]vstack=inputs=3[v]" -map "[v]" -map 0:a output.mp4

Option 2: Mix audio from all three videos

To combine and play the audio from all three videos simultaneously, use the amix filter:

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]vstack=inputs=3[v];[0:a][1:a][2:a]amix=inputs=3[a]" -map "[v]" -map "[a]" output.mp4

Stacking Videos with Different Widths

The vstack filter will fail if the input videos have different widths. To resolve this, you must scale the videos to a uniform width before stacking them.

The command below scales all three videos to a width of 1080 pixels while automatically calculating the height to maintain each video’s original aspect ratio (using -2 to ensure the height is divisible by 2, which is required by most video codecs):

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v]scale=1080:-2[v1];[1:v]scale=1080:-2[v2];[2:v]scale=1080:-2[v3];[v1][v2][v3]vstack=inputs=3[v]" -map "[v]" output.mp4