Combine 6 Videos into a 3x2 Grid with FFmpeg
This guide explains how to combine six separate video files into a
single 3x2 grid layout using the powerful xstack filter in
FFmpeg. You will learn the exact command-line syntax required, how the
grid coordinate system works, and how to handle videos of different
sizes by scaling them prior to merging.
The Basic Command (For Same-Sized Videos)
If all six of your input videos already share the exact same resolution (for example, 1920x1080 or 1280x720), you can merge them directly using the following command:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -i input5.mp4 -i input6.mp4 \
-filter_complex "xstack=inputs=6:layout=0_0|w0_0|w0+w1_0|0_h0|w0_h0|w0+w1_h0" \
-c:v libx264 output.mp4Understanding the Layout Grid
The layout option in the xstack filter
defines the starting X and Y coordinates (separated by an underscore
_) for each input video. Each video position is separated
by a pipe | character.
Here is how the 3x2 grid coordinates are calculated:
- Top-Left (Input 0):
0_0(Starts at the top-left corner) - Top-Middle (Input 1):
w0_0(Positioned to the right of the first video;w0is the width of Input 0) - Top-Right (Input 2):
w0+w1_0(Positioned to the right of the second video; width of Input 0 + width of Input 1) - Bottom-Left (Input 3):
0_h0(Positioned directly below the first video;h0is the height of Input 0) - Bottom-Middle (Input 4):
w0_h0(Positioned below the second video) - Bottom-Right (Input 5):
w0+w1_h0(Positioned below the third video)
The Advanced Command (For Different-Sized Videos)
If your input videos have different dimensions, they must be scaled
to matching sizes before being passed to xstack. The
command below scales all six inputs to a uniform resolution of 640x360
pixels before arranging them into the 3x2 grid:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -i input5.mp4 -i input6.mp4 \
-filter_complex \
"[0:v]scale=640:360[v0]; \
[1:v]scale=640:360[v1]; \
[2:v]scale=640:360[v2]; \
[3:v]scale=640:360[v3]; \
[4:v]scale=640:360[v4]; \
[5:v]scale=640:360[v5]; \
[v0][v1][v2][v3][v4][v5]xstack=inputs=6:layout=0_0|w0_0|w0+w1_0|0_h0|w0_h0|w0+w1_h0" \
-c:v libx264 output.mp4Key Elements of This Command:
[0:v]scale=640:360[v0]: Takes the video stream of the first input ([0:v]), scales it to 640x360, and labels the temporary output stream as[v0].[v0][v1][v2][v3][v4][v5]: Feeds the six newly scaled video streams into thexstackfilter.-c:v libx264: Encodes the output video using the H.264 video codec for broad compatibility.
Handling Audio
By default, the commands above will only map the audio from the first
input file. If you want to combine the audio tracks from all six videos
so they play simultaneously, add the amix filter to your
command:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -i input5.mp4 -i input6.mp4 \
-filter_complex \
"[0:v]scale=640:360[v0];[1:v]scale=640:360[v1];[2:v]scale=640:360[v2];[3:v]scale=640:360[v3];[4:v]scale=640:360[v4];[5:v]scale=640:360[v5]; \
[v0][v1][v2][v3][v4][v5]xstack=inputs=6:layout=0_0|w0_0|w0+w1_0|0_h0|w0_h0|w0+w1_h0[outv]; \
[0:a][1:a][2:a][3:a][4:a][5:a]amix=inputs=6[outa]" \
-map "[outv]" -map "[outa]" -c:v libx264 output.mp4