Stabilize Shaky Video Using FFmpeg Deshake Filter
Shaky camera footage can ruin an otherwise great video, but you can
easily fix this using FFmpeg’s built-in deshake filter.
This guide provides a straightforward tutorial on how to use the
single-pass deshake video filter to smooth out camera
motion, explaining the essential command-line parameters and providing
practical examples to get steady results quickly.
The Basic Deshake Command
The deshake filter works by analyzing the differences
between consecutive frames to detect and compensate for horizontal and
vertical camera motion.
To stabilize a video using the default settings, use the following basic command:
ffmpeg -i input.mp4 -vf "deshake" output.mp4This default command works well for mild, low-frequency camera shakes. It automatically detects motion in the center of the frame and attempts to smooth out the transition between frames.
Customizing the Deshake Parameters
For more challenging footage, you can fine-tune the filter using
specific parameters. The syntax for applying parameters is
deshake=parameter1=value1:parameter2=value2.
Here are the most useful parameters for the single-pass
deshake filter:
rxandry(Search Range): These parameters set the maximum extent of search for motion in the X (horizontal) and Y (vertical) directions. The default is 16 pixels. For highly shaky video, increase this value (up to 64).edge(Edge Fill): When the filter shifts frames to compensate for shake, black bars or empty space can appear at the edges. Theedgeparameter determines how FFmpeg fills these gaps:mirror: Mirrors the pixels from the boundary (default).wrap: Wraps pixels from the opposite side.blank: Fills the gaps with black pixels.
blocksize(Search Block Size): Sets the size of the grid blocks used for motion estimation. The default is 8. Valid values are 4, 8, 16, and 32. Larger blocks are faster but less precise.
Advanced Example Command
If you have a video with significant camera shake, you can increase the search range and use mirroring to hide the edge artifacts:
ffmpeg -i input.mp4 -vf "deshake=rx=64:ry=64:edge=mirror" output.mp4Tips for Best Results
- Contrast is Key: The
deshakefilter relies on finding distinct features in the video to track movement. It works best on well-lit, high-contrast footage. - Performance: Because the
deshakefilter is a single-pass filter built directly into FFmpeg, it is highly efficient and does not require compiling FFmpeg with external libraries (unlike the alternativevidstabplugin). - Combine with Cropping: If the mirrored edges look
distracting, you can chain the
cropfilter afterdeshaketo cut out the outer boundaries of the stabilized video:
ffmpeg -i input.mp4 -vf "deshake=rx=32:ry=32:edge=mirror, crop=in_w-64:in_h-64" output.mp4