Compare Two Videos Side-by-Side with FFmpeg hstack

This article provides a quick and practical guide on how to use the FFmpeg hstack filter to combine two different video files into a single, side-by-side comparison video. You will learn the basic command for identical videos, how to scale videos of different sizes to prevent errors, and how to manage the audio tracks for the final output.

The Basic Command

If both of your input videos have the exact same height, you can merge them horizontally using a simple filter complex:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex hstack output.mp4

Handling Videos with Different Heights

The hstack filter requires all input videos to have the exact same height. If your videos have different resolutions, FFmpeg will throw an error.

To fix this, you must scale the videos to a matching height. The command below scales both inputs to a height of 720 pixels while maintaining their original aspect ratios before stacking them:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]scale=-1:720[v0];[1:v]scale=-1:720[v1];[v0][v1]hstack[v]" -map "[v]" output.mp4

Managing Audio

By default, the basic hstack command may drop the audio or only include it from the first input depending on your FFmpeg version. You can explicitly define how to handle audio:

Option 1: Use audio from the first video

To keep only the audio track from input1.mp4 and copy it without re-encoding:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]hstack[v]" -map "[v]" -map 0:a -c:a copy output.mp4

Option 2: Mix audio from both videos

To merge and play the audio from both videos simultaneously, use the amix filter:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]hstack[v];[0:a][1:a]amix=inputs=2[a]" -map "[v]" -map "[a]" output.mp4

Handling Different Video Lengths

If one video is longer than the other, the output video will freeze on the last frame of the shorter video while the longer one keeps playing. To force the output video to stop as soon as the shortest input file ends, add the -shortest flag:

ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]hstack[v]" -map "[v]" -shortest output.mp4