How to Stabilize Video with FFmpeg vidstabtransform

Shaky camera footage can be easily smoothed out using FFmpeg’s powerful video stabilization filters. This guide provides a straightforward, step-by-step tutorial on how to use the vidstabdetect and vidstabtransform filters to analyze and stabilize your videos, including the exact commands and parameters you need for optimal results.

Stabilizing video in FFmpeg is a two-pass process. In the first pass, FFmpeg analyzes the video’s motion and writes this tracking data to a file. In the second pass, FFmpeg reads that data and applies the stabilization to generate the final video.

Step 1: Analyze the Video (Pass 1)

Before you can stabilize the video, you must run the vidstabdetect filter to analyze the camera’s shakes and movements. This step generates a dummy output and saves the motion data to a file named transforms.trf.

Run the following command in your terminal:

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

Parameter Breakdown: * shakiness=10: Sets how shaky the original video is on a scale from 1 (mild) to 10 (severe). The default is 5. * accuracy=15: Sets the accuracy of the detection process from 1 (low) to 15 (high). Higher values provide better results but take longer to process. * result=transforms.trf: Specifies the path and file name where the transform data will be saved. * -f null -: Tells FFmpeg not to write a video file for this pass, as we only need the tracking data.

Step 2: Stabilize the Video (Pass 2)

Once the transforms.trf file is generated, you can apply the stabilization using the vidstabtransform filter.

Run the following command to output your stabilized video:

ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:zoom=2:smoothing=30 -c:v libx264 -c:a copy output.mp4

Parameter Breakdown: * input=transforms.trf: Tells the filter where to read the motion data generated in Step 1. * smoothing=30: Determines the number of frames to use for low-pass filtering. A higher number (e.g., 30 or more) results in a smoother video, while a lower number keeps more of the original camera pan. * zoom=2: Zooms into the video by a percentage (in this case, 2%) to hide the black borders that appear when the frame is shifted to compensate for the shake. If you set this to 0, you may see shifting black edges around your video. * -c:v libx264: Re-encodes the video using the H.264 codec. * -c:a copy: Copies the audio directly without re-encoding to save time and preserve quality.

Advanced Tweaking

If your video still has visible black borders or looks too shaky, you can adjust these settings: