Create Side-by-Side Video Comparison Using FFmpeg
This guide explains how to use FFmpeg to create a side-by-side
comparison video showing a shaky original video next to its stabilized
version. You will learn how to analyze and stabilize your footage using
the vid.stab plugin, add text labels to distinguish the
videos, and merge them into a single split-screen output.
Step 1: Generate the Stabilization Vectors
Video stabilization in FFmpeg is a two-step process using the
vidstabdetect and vidstabtransform filters.
First, you must analyze the shaky video to generate a transform file
containing travel distance and speed vectors.
Run the following command to analyze your input video
(shaky.mp4):
ffmpeg -i shaky.mp4 -vf vidstabdetect=shakiness=10:accuracy=15:result=transforms.trf -f null -This command generates a file named transforms.trf in
your current directory.
Step 2: Create the Stabilized Video
Next, use the generated transforms.trf file to render
the stabilized video. It is important to set the zoom
parameter to 0 (or disable optimal zoom) so that the output
video retains the exact same dimensions as the original, which is
required for a clean side-by-side comparison.
Run this command to generate the stabilized video:
ffmpeg -i shaky.mp4 -vf vidstabtransform=input=transforms.trf:zoom=0:smoothing=30 -c:a copy stabilized.mp4You now have two files of identical resolution:
shaky.mp4 and stabilized.mp4.
Step 3: Combine the Videos Side-by-Side with Labels
To merge both videos into a single frame, use FFmpeg’s
hstack filter. To make the comparison clear, you can also
overlay “Original” and “Stabilized” text labels on the respective video
streams before combining them.
Execute the following command to create the final comparison video:
ffmpeg -i shaky.mp4 -i stabilized.mp4 -filter_complex \
"[0:v]drawtext=text='Original':x=20:y=20:fontsize=36:fontcolor=white:box=1:boxcolor=black@0.6:boxborderw=5[left]; \
[1:v]drawtext=text='Stabilized':x=20:y=20:fontsize=36:fontcolor=white:box=1:boxcolor=black@0.6:boxborderw=5[right]; \
[left][right]hstack=inputs=2[v]" \
-map "[v]" -map 0:a? -c:v libx264 -crf 18 -preset veryfast -c:a copy comparison_output.mp4How This Command Works:
-i shaky.mp4 -i stabilized.mp4: Takes both the original and stabilized videos as inputs.drawtext: Applies a text overlay to each video stream. It sets the text size to 36, colors it white, and places a semi-transparent black background box (boxcolor=black@0.6) behind the text for readability.hstack=inputs=2: Horizontally stacks the labeled video streams side-by-side.-map "[v]": Maps the combined video output to the final file.-map 0:a?: Copies the audio from the first input file (the original video), if audio exists.-c:v libx264 -crf 18: Encodes the output video using the H.264 codec at high quality.