Skip to main content

Stabilizing

Help Wanted

This section is in need of contributions. If you believe you can help, please see our Contribution Guide to get started as a contributor!

Overview​

Stabilizing is the process of reducing unwanted camera movement and shakes in video clips using FFmpeg. This improves overall encoding efficiency by minimizing unpredictable global movement, such as that from handheld cameras. The recommended method for stabilizing videos with FFmpeg is to use the VidStab library, which requires a build of FFmpeg compiled with --enable-libvidstab.

VidStab offers two filters within FFmpeg:

ffmpeg -hide_banner -filters | grep vidstab
... vidstabdetect V->V Extract relative transformations, pass 1 of 2 for stabilization (see vidstabtransform for pass 2).
... vidstabtransform V->V Transform the frames, pass 2 of 2 for stabilization (see vidstabdetect for pass 1).

The vidstabdetect filter is used in the first pass to generate a video transformations file (.trf), while vidstabtransform is employed in the second pass to apply those transformations.

Usage​

To stabilize a video using default parameters, follow these two steps:

ffmpeg -i input.mp4 -vf vidstabdetect -f null -
ffmpeg -i input.mp4 -vf vidstabtransform output.mp4

After running the first command, a transforms.trf file will be created in the directory where you executed FFmpeg. Once the stabilization process is complete, you can safely delete this file. The resulting output.mp4 video will have reduced shakiness.

For stabilizing high-framerate videos with strong camera movement:

ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=8:result=a.trf -f null -
ffmpeg -i input.mp4 -vf vidstabtransform=smoothing=30:zoom=-5:input=a.trf output.mp4
tip

Remember to set appropriate video/audio codec parameters in the command before output.mp4. You must not use -c:v copy, as the video will undergo transformations.

vidstabdetect Parameters​

  • result - Sets the output .trf file location
  • shakiness - Adjusts movement reduction, with 1 being the least and 10 the most reduction (highest stabilization). Default is 5.
  • accuracy - Controls movement reduction accuracy. Lower values use less CPU but may be less accurate. FFmpeg's minimum allowed value is 3. Processing speed was approximately 21 fps at 3 and 14 fps at 15.

For a complete list of parameters, refer to the vidstabdetect documentation.

vidstabtransform Parameters​

  • input - Specifies the input .trf file created by vidstabdetect
  • smoothing - Determines the number of frames considered for future and past movement estimation. Default is 10.
  • zoom - Adjusts the zoom percentage, with 0% being the default. Negative values create a zoom-out effect.
  • interpol - Sets the type of interpolation used:
    • no - No interpolation
    • linear - Only horizontal
    • bilinear - Faster but may result in blurry output (default)
    • bicubic - Slower

See the vidstabtransform documentation for more details.

Notes​

  • Stabilization is a lossy process that can reduce video quality due to zoom and interpolation effects.
  • Some users may notice overall wobbliness in stabilized videos, especially at higher stabilization levels. This is an inherent characteristic of this filter.
  • Depending on your use case, consider employing two-pass encoding along with these stabilization steps.