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 vidstabIf 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:
shakiness=10: Represents how shaky the original video is on a scale of 1 to 10. A value of 10 represents highly unstable footage.accuracy=15: Sets the detection accuracy from 1 (low) to 15 (high). While a value of 15 is slower to process, it yields the most precise tracking data.result=transforms.trf: Specifies the name of the output file where the motion vectors will be saved.-f null -: Instructs FFmpeg to process the video without writing a physical output video file, which speeds up this phase.
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.mp4Parameter Breakdown:
input=transforms.trf: Tells the filter to read the motion vector file generated in Step 1.smoothing=30: Determines the number of frames to look ahead and behind to calculate the smoothed path. A higher number (e.g., 30–50) results in smoother video, but requires more zooming to hide the resulting black borders.optzoom=1: Enables optimal static zooming. The filter automatically zooms in just enough to hide the black edges introduced by shifting the video frames during stabilization.-crf 18: Ensures high-quality H.264 video encoding.-c:a copy: Copies the audio stream without re-encoding to preserve original audio quality.
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.mp42. 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.mp4optzoom=2: Enables dynamic zoom.maxshift: Sets the maximum percentage of frame shift allowed.maxzoom: Limits how far the filter can zoom in to compensate for extreme shakes.