Multi-Pass Video Stabilization with FFmpeg Vidstab

Unstable camera footage can degrade the quality of any video project. Utilizing FFmpeg alongside the vid.stab library allows you to perform highly precise, multi-pass video stabilization. This guide walks you through the exact process of analyzing shaky video files to generate motion data and then applying that data to produce smooth, professional-grade output.

Prerequisites

Before starting, ensure your FFmpeg installation is compiled with support for the vidstab library. You can verify this by running the following command in your terminal:

ffmpeg -filters | grep vidstab

If you see vidstabdetect and vidstabtransform in the output, your FFmpeg build is ready.


Step 1: The Analysis Pass (vidstabdetect)

The first pass scans the video to detect camera movement and writes this tracking data to a temporary file (usually named transforms.trf). It does not alter the video itself.

Run the following command to analyze your video:

ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=10:accuracy=15:result=transforms.trf -f null -

Parameter Breakdown:


Step 2: The Stabilization Pass (vidstabtransform)

Once the transforms.trf file is generated, you will run the second pass. This phase reads the tracking data and applies transformation matrices to smooth out the shaky camera paths.

Run the following command to generate the stabilized video:

ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:smoothing=30:optzoom=1 -c:v libx264 -crf 18 -c:a copy output.mp4

Parameter Breakdown:


Advanced Tuning for Maximum Precision

For complex shots, you can adjust these parameters to achieve even cleaner results:

1. Tripod Mode

If you want the camera to look as if it is mounted on a completely stationary tripod rather than a moving hand, add the tripod=1 option to the second-pass command:

ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:tripod=1:optzoom=1 output.mp4

2. Dynamic Zooming

Instead of a static zoom level throughout the entire video, you can use dynamic zooming. This continuously adjusts the zoom level based on how much correction is required at any given moment:

ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:optzoom=2:maxshift=0.1:maxzoom=2.0 output.mp4