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 a process that reduces unwanted camera movement and shakes in a video clip. This is particularly important, as unpredictable global movement (such as that from a handheld camera) can decrease overall encoding efficiency. The most popular way to stabilize video with FFmpeg is to use the VidStab library. To do this, you need a build of FFmpeg compiled with --enable-libvidstab
.
VidStab actually has 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 for the first pass, where the video transformations file (.trf
) is generated, and vidstabtransform
is used for the second pass, where transformations are actually applied. This implies that stabilization cannot be achieved in real-time.
Usageâ
To stabilize a video using default parameters, you need to run two FFmpeg commands:
ffmpeg -i input.mp4 -vf vidstabdetect -f null -
ffmpeg -i input.mp4 -vf vidstabtransform output.mp4
A file called transforms.trf
will be created in the directory where you run FFmpeg. After the second step is finished, you can safely remove it. The resulting output.mp4
video will now have reduced shakiness.
To stabilize a high-framerate video with strong camera movement, set its transformations filename to a.trf
, and increase the output field of view. You can use the following commands:
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 be transformed.
vidstabdetect Parametersâ
result
: Sets the output.trf
file locationshakiness
: The amount of movement reduction, where1
is the least reduction,10
is the most reduction (highest stabilization), and5
is the defaultaccuracy
: The accuracy of movement reduction, where1
is the least accurate,15
is the most accurate, and15
is also the default. This setting influences CPU usage during detection. FFmpeg does not allow setting a value lower than3
.3
gave a processing speed of21 fps
, and15
gave14 fps
. The process itself is rather CPU-intensive.
For a description of all possible parameters, take a look here.
vidstabtransform Parametersâ
input
: Sets the input.trf
file location created byvidstabdetect
smoothing
: The number of frames in the future and past used for movement estimation, where the default is10
(so 10 past and 10 future frames)zoom
: The amount of zoom expressed in percentage, where the default is0%
. It can be negative, which will create a zoom-out effect.interpol
: The type of interpolation used, wherebilinear
is the default.no
: No interpolationlinear
: Only horizontalbilinear
: Faster but can result in blurry outputbicubic
: Slower
For a description of all possible parameters, take a look here.
Notesâ
- The process of stabilization is lossy and can reduce the quality of the video, mainly due to the zoom and interpolation used.
- You might notice overall wobbliness in the resulting video file, especially with higher stabilization levels. This is just how this filter works.
- As you will need to run two passes anyway, you might also consider using two-pass encoding, depending on your use case.