How to Stabilize Video with FFmpeg Deshake
Shaky video footage can be easily corrected using FFmpeg’s built-in
deshake filter. This guide provides a straightforward,
single-pass approach to stabilizing your videos directly from the
command line, explaining the essential parameters and providing
ready-to-use commands to improve your playback quality without the need
for complex, multi-pass workflows.
The Basic Deshake Command
The deshake filter works by analyzing motion between
frames and applying a counter-shift to minimize camera shake. For a
quick, automatic stabilization using default settings, use the following
basic command:
ffmpeg -i input.mp4 -vf deshake output.mp4This command takes your shaky input file, applies the default single-pass stabilization, and exports the corrected video.
Fine-Tuning Deshake Parameters
While the default settings work well for mild camera shake, you can customize the filter parameters to achieve better results for highly unstable footage. The syntax for adding parameters is:
-vf "deshake=parameter1=value1:parameter2=value2"Here are the most useful parameters you can adjust:
rxandry(Search Range): These define the maximum extent of horizontal (rx) and vertical (ry) motion search in pixels. The default is 16. Increase these values (up to 64) for faster, more violent camera shakes.- Example:
deshake=rx=32:ry=32
- Example:
edge(Edge Fill): Because the filter shifts the frame to compensate for shake, black borders can appear at the edges of the video. Theedgeparameter determines how FFmpeg fills these gaps.mirror(Default): Mirrors the adjacent pixels to fill the gap.blank: Fills the gaps with black borders.original: Keeps the original, un-stabilized edge pixels.clamp: Clamps the edge pixels.
blocksize(Grid Size): Sets the size of the blocks used for motion estimation. The default is 8. Smaller numbers increase accuracy but require more processing power.contrast(Contrast Threshold): Sets the contrast threshold for identifying tracking points. The default is 125. Lowering this value makes the filter more sensitive to motion in low-contrast scenes.
Advanced Single-Pass Example
If you are dealing with a handheld video with moderate shake and want to ensure the edges are cleanly mirrored while widening the motion search area, use this optimized command:
ffmpeg -i input.mp4 -vf "deshake=rx=24:ry=24:edge=mirror:blocksize=16" -c:a copy output.mp4In this command: * rx=24:ry=24 increases the search
window to catch larger shakes. * edge=mirror seamlessly
blends the shifting edges. * blocksize=16 balances
processing speed and motion tracking accuracy. * -c:a copy
copies the audio stream without re-encoding to save processing time and
preserve original audio quality.