How to Stack Two Videos Vertically with FFmpeg
This guide explains how to combine two separate video files into a
single, vertically stacked video using the FFmpeg vstack
filter. You will learn the basic command for stacking videos of equal
width, how to handle videos with different widths by resizing them, and
how to merge their audio tracks.
Basic Vertical Stacking
To stack two videos vertically, they must have the exact same width. If your videos already share the same width, use the following basic command:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex vstack=inputs=2 output.mp4-i input1.mp4 -i input2.mp4: Specifies the two input video files.-filter_complex vstack=inputs=2: Invokes the complex filter graph and uses thevstackfilter, specifying that there are2input streams to stack.output.mp4: The name of the resulting stacked video file.
Stacking Videos with Different Widths
If your input videos have different widths, the basic
vstack command will fail with an error. You must scale the
videos to a matching width before stacking them.
The command below scales both videos to a width of 1280 pixels while automatically adjusting the height to maintain the original aspect ratio:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=1280:-1[v0];[1:v]scale=1280:-1[v1];[v0][v1]vstack=inputs=2[v]" -map "[v]" output.mp4[0:v]scale=1280:-1[v0]: Takes the video stream of the first input (0:v), scales its width to 1280 (calculating the height automatically with-1to preserve aspect ratio), and labels the output stream as[v0].[1:v]scale=1280:-1[v1]: Performs the same scaling operation on the second input (1:v) and labels it[v1].[v0][v1]vstack=inputs=2[v]: Takes the scaled streams[v0]and[v1], stacks them vertically, and labels the combined video output as[v].-map "[v]": Directs FFmpeg to write the mapped vertical video stack to the output file.
Combining Audio Tracks
By default, FFmpeg will only copy the audio from the first input
video. To merge the audio from both videos so they play simultaneously,
use the amix filter alongside vstack:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]vstack=inputs=2[v];[0:a][1:a]amix=inputs=2[a]" -map "[v]" -map "[a]" output.mp4[0:a][1:a]amix=inputs=2[a]: Takes the audio streams from both inputs and mixes them into a single audio stream labeled[a].-map "[v]" -map "[a]": Maps both the stacked video and the mixed audio into the final output file.