Stabilize shaky video with vidstab
Handheld phone footage, action-cam clips, and drone passes without a gimbal are rarely stable. The vidstab filter set detects the camera motion in a first pass, then applies a smoothing transform in the encode pass. The result looks like the camera was on a tripod, with a small zoom-in to cover the edge motion.
The command
ffmpeg -i shaky.mp4 -vf vidstabdetect=shakiness=5:accuracy=15 -f null -
ffmpeg -i shaky.mp4 -vf vidstabtransform=smoothing=30 -c:v libx264 -crf 20 out.mp4What it does
The first command analyzes the video and writes a `transforms.trf` file with the per-frame motion vector. The second command applies the inverse transform to smooth the motion. The `shakiness` value (1-10) controls how aggressive the detection is; `smoothing` (1-100+) controls how much of the detected motion is removed. Higher smoothing gives a more tripod-like result, at the cost of more zoom-in.
Requirements
A two-pass workflow. vidstab is in libavfilter and works on the CPU. No special hardware, no special build flag, but the analysis pass adds a one-time cost equal to a real decode.
Things to avoid
- Skipping the analysis pass and applying vidstabtransform directly. The transform filter needs the `.trf` file the analysis pass produces.
- Setting smoothing very high on a clip that has intentional motion. Smooth-then-stabilize will fight pans and reveal them as tiny stutter.
- Forgetting the zoom cost. The transform slightly enlarges the frame so the smoothed edges fit. Compensate by scaling the output down a few percent, or by cropping to a slightly smaller target resolution.
- Running vidstab on already-stable footage. It cannot fix optical flow problems (rolling shutter, fast pans) — it only fixes translational and rotational shake.