How to Use the xstack Filter in FFmpeg

The xstack filter in FFmpeg is a powerful tool used to combine multiple input video streams into a single, highly customizable grid or mosaic layout. Unlike the older hstack and vstack filters, which only allow simple horizontal or vertical stacking, xstack lets you position videos anywhere on a 2D coordinate system. This article provides a direct, step-by-step guide on how to use the xstack filter, explaining its layout syntax and providing practical command-line examples.

Understanding the Basic Syntax

The basic syntax for the xstack filter requires specifying the number of input streams and defining their exact spatial coordinates on the output canvas.

-filter_complex "[0:v][1:v]xstack=inputs=2:layout=0_0|w0_0[out]"

Practical Examples

1. Side-by-Side (2 Videos)

To place two videos side-by-side, the first video is placed at the top-left corner (0_0). The second video is placed directly to the right of the first video (w0_0), meaning its X-coordinate starts where the width of the first video ends, and its Y-coordinate is 0.

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]xstack=inputs=2:layout=0_0|w0_0[out]" -map "[out]" output.mp4

2. 2x2 Grid (4 Videos)

To create a 2x2 grid using four videos of identical dimensions, you map them to the four corners of the canvas:

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -i input4.mp4 -filter_complex "[0:v][1:v][2:v][3:v]xstack=inputs=4:layout=0_0|w0_0|0_h0|w0_h0[out]" -map "[out]" output.mp4

3. Vertical Stack (3 Videos)

To stack three videos vertically on top of each other, keep the X-coordinate at 0 for all videos and stack their heights progressively:

ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]xstack=inputs=3:layout=0_0|0_h0|0_h0+h1[out]" -map "[out]" output.mp4

Important Usage Tips