Stabilize Shaky Video with FFmpeg Vidstab
This guide explains how to use the FFmpeg vidstabdetect
and vidstabtransform filters to stabilize shaky video
footage. You will learn how to run a two-pass stabilization process and
configure virtual camera panning to create smooth, professional-looking
video transitions.
To stabilize a video using FFmpeg’s vid.stab library,
you must perform a two-pass process. The first pass analyzes the video’s
motion, and the second pass applies the stabilization using virtual
camera panning to smooth out the shakes.
Step 1: Analyze the Video (First Pass)
The first step uses the vidstabdetect filter to analyze
the video frames and generate a file containing motion data (usually
named transforms.trf).
Run the following command:
ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=10:accuracy=15:result=transforms.trf -f null -shakiness=10: Sets how shaky the original video is (on a scale from 1 to 10). A higher value is better for very shaky footage.accuracy=15: Determines the accuracy of the detection (from 1 to 15). Higher values yield better results but take longer to process.result=transforms.trf: The path where the motion data file will be saved.-f null -: Tells FFmpeg not to write an output video file during this analysis pass, saving processing time.
Step 2: Apply Stabilization and Virtual Panning (Second Pass)
Once the transforms.trf file is generated, you use the
vidstabtransform filter to read this data and render the
stabilized video. This is where you configure virtual camera panning to
smooth out transitions.
Run the following command:
ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:smoothing=30:maxshift=100:maxangle=-1:crop=black:zoom=5 output.mp4input=transforms.trf: Directs the filter to the motion data file created in the first step.smoothing=30: Controls the virtual camera panning. This value represents the number of frames to look ahead and behind to smooth the camera path. A higher number (e.g., 30–50) results in a smoother, more gradual “pan” effect, while lower numbers keep the camera closer to the original movement.maxshift=100: Sets the maximum number of pixels the image can be shifted to compensate for shakiness.maxangle=-1: Sets the maximum angle in radians for camera rotation. A value of-1allows the filter to calculate the optimal rotation limit automatically.crop=black: Specifies what to do with the empty borders created by shifting the video. Setting this toblackleaves black borders, which allows you to see the stabilization boundary.zoom=5: Zooms into the video by 5% to crop out the black borders generated by the camera stabilization movement. Adjust this value depending on how much shifting occurs.