FFmpeg hstack: How to Stack 3 Videos Horizontally
This article provides a quick, step-by-step guide on how to combine
three separate video files side-by-side into a single row using FFmpeg’s
hstack filter. You will learn the exact command-line syntax
required to merge these inputs, how to handle videos of different
heights, and how to manage the accompanying audio streams.
The Basic Command for Equal Height Videos
If your three input videos already have the exact same vertical
resolution (height), you can stack them horizontally using a simple
filter_complex command. Run the following command in your
terminal:
ffmpeg -i input1.mp4 -i input2.mp4 -i input3.mp4 -filter_complex "hstack=inputs=3" output.mp4In this command: *
-i input1.mp4 -i input2.mp4 -i input3.mp4 specifies the
three input video files. *
-filter_complex "hstack=inputs=3" tells FFmpeg to use the
horizontal stack filter and expects three video inputs. *
output.mp4 is the resulting video file.
Handling Videos with Different Heights
The hstack filter requires all input videos to have the
identical height. If your videos have different heights, the command
will fail. To resolve this, you must scale the videos to a uniform
height before stacking them.
The following command scales all three videos to a height of 720 pixels while automatically adjusting the width to maintain their original aspect ratios:
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" output.mp4[0:v]scale=-1:720[v0]takes the video stream of the first input (0:v), scales its height to720, automatically calculates the proportional width (-1), and labels the temporary output as[v0].- This process is repeated for the second (
[v1]) and third ([v2]) videos. [v0][v1][v2]hstack=inputs=3takes those three scaled video streams and stacks them side-by-side.
Combining Audio Streams
By default, the commands above may only copy the audio from the first
input file or omit audio entirely depending on your system
configuration. If you want to mix the audio from all three videos so
they play simultaneously, use the amix filter:
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];[0:a][1:a][2:a]amix=inputs=3[outa]" -map "[outv]" -map "[outa]" output.mp4[0:a][1:a][2:a]amix=inputs=3[outa]takes the audio tracks from all three inputs and mixes them into a single audio stream labeled[outa].-map "[outv]"and-map "[outa]"explicitly tell FFmpeg to use the newly created stacked video and mixed audio in the final output file.