Combine Four Videos into 2x2 Grid with FFmpeg xstack
This guide provides a straightforward tutorial on how to merge four
separate video files into a single 2x2 grid layout using the powerful
xstack filter in FFmpeg. You will learn the exact
command-line syntax, how to define the coordinates for each video pane,
and how to handle video scaling and audio mixing for a seamless final
output.
The Basic 2x2 Grid Command
To combine four videos of the exact same resolution into a 2x2 grid,
use the xstack filter. The filter requires you to specify
the number of inputs and their coordinate positions on a 2D grid.
Here is the basic command:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 \
-filter_complex "xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0" \
-c:v libx264 output.mp4Understanding the Layout Coordinates
The layout parameter defines the anchor points (X and Y
coordinates) for the top-left corner of each input video:
0_0(Top-Left): Positioned at X=0, Y=0.w0_0(Top-Right): Positioned at X=width of the first video (w0), Y=0.0_h0(Bottom-Left): Positioned at X=0, Y=height of the first video (h0).w0_h0(Bottom-Right): Positioned at X=width of the first video (w0), Y=height of the first video (h0).
Note: This layout syntax works perfectly when all four source videos have identical dimensions.
Handling Videos of Different Resolutions
If your input videos have different dimensions, you must scale them
to matching resolutions before passing them to the xstack
filter. Failing to do so will result in gaps or cropped videos.
The command below scales all four inputs to a uniform size of 960x540 pixels, resulting in a clean 1920x1080 (Full HD) output:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 \
-filter_complex \
"[0:v]scale=960:540[v0]; \
[1:v]scale=960:540[v1]; \
[2:v]scale=960:540[v2]; \
[3:v]scale=960:540[v3]; \
[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" \
-map "[v]" -c:v libx264 output.mp4Managing the Audio
By default, the commands above do not process audio. You can choose to either mix the audio from all four videos or select the audio track from a single video.
Option 1: Mix audio from all four videos
To hear the audio from all four tracks simultaneously, use the
amix filter:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 \
-filter_complex \
"[0:v]scale=960:540[v0]; [1:v]scale=960:540[v1]; [2:v]scale=960:540[v2]; [3:v]scale=960:540[v3]; \
[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]; \
[0:a][1:a][2:a][3:a]amix=inputs=4[a]" \
-map "[v]" -map "[a]" -c:v libx264 -c:a aac output.mp4Option 2: Use audio from only the first video
To keep only the audio track from input1.mp4 and discard
the rest:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 \
-filter_complex \
"[0:v]scale=960:540[v0]; [1:v]scale=960:540[v1]; [2:v]scale=960:540[v2]; [3:v]scale=960:540[v3]; \
[v0][v1][v2][v3]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[v]" \
-map "[v]" -map 0:a -c:v libx264 -c:a copy output.mp4