Stabilizing
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
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 locationshakiness
- Adjusts movement reduction, with1
being the least and10
the most reduction (highest stabilization). Default is5
.accuracy
- Controls movement reduction accuracy. Lower values use less CPU but may be less accurate. FFmpeg's minimum allowed value is3
. Processing speed was approximately21 fps
at3
and14 fps
at15
.
For a complete list of parameters, refer to the vidstabdetect documentation.
vidstabtransform Parametersâ
input
- Specifies the input.trf
file created byvidstabdetect
smoothing
- Determines the number of frames considered for future and past movement estimation. Default is10
.zoom
- Adjusts the zoom percentage, with0%
being the default. Negative values create a zoom-out effect.interpol
- Sets the type of interpolation used:no
- No interpolationlinear
- Only horizontalbilinear
- 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.