Combine Three Videos Side-by-Side with FFmpeg hstack
This guide demonstrates how to use the FFmpeg hstack
(horizontal stack) video filter to combine three separate input videos
into a single side-by-side video. You will learn the exact command-line
syntax, how to handle videos of different heights, and how to mix the
audio tracks from all three inputs.
The Basic Command
To combine three videos that already share the exact same height and frame rate, use the following basic command:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "hstack=inputs=3" output.mp4How it works: *
-i input1.mp4 -i input2.mp4 -i input3.mp4: Specifies the
three source video files. *
-filter_complex "hstack=inputs=3": Calls the complex
filtergraph and tells the hstack filter to expect three
input streams. By default, it takes the video streams from the inputs in
the order they were defined. * output.mp4: The resulting
output file containing the three videos side-by-side.
Handling Videos with Different Heights
The hstack filter requires all input videos to have the
exact same height. If your videos have different dimensions, the command
will fail. To resolve this, you must scale the videos to a matching
height (for example, 720 pixels) within the filtergraph before stacking
them:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v]scale=-1:720[v0];[1:v]scale=-1:720[v1];[2:v]scale=-1:720[v2];[v0][v1][v2]hstack=inputs=3[outv]" -map "[outv]" output.mp4How it works: * [0:v]scale=-1:720[v0]:
Takes the video stream of the first input (0:v), scales its
height to 720 pixels while keeping the aspect ratio automatic
(-1), and labels the temporary output as [v0].
* [v0][v1][v2]hstack=inputs=3[outv]: Takes the three scaled
video streams and merges them horizontally into a stream named
[outv]. * -map "[outv]": Tells FFmpeg to write
the merged video stream to the output file.
Combining Video and Audio
The hstack filter only processes video streams. If you
want to merge the audio from all three videos as well, you must use the
amix filter to mix the audio tracks:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "[0:v][1:v][2:v]hstack=inputs=3[outv];[0:a][1:a][2:a]amix=inputs=3[outa]" -map "[outv]" -map "[outa]" output.mp4How it works: *
[0:a][1:a][2:a]amix=inputs=3[outa]: Takes the audio tracks
from all three inputs, mixes them down into a single audio stream named
[outa], and maps both the merged video and merged audio to
the final file.