Video stabilization using ffmpeg

Written by

in

If you want to stabilize videos and remove camera shake, the `ffmpeg` tool offers a powerful solution. Thanks to the built-in filters `vidstabdetect` and `vidstabtransform`, you can achieve professional results without using complex video editors.

Preparing for work

Before you start, make sure your `ffmpeg` supports the `vidstab` library. On Linux you can check this with the command:

bash  
ffmpeg -filters | grep vidstab  

If the library is not installed, you can add it:

sudo apt install ffmpeg libvidstab-dev  

Installation for macOS via brew:

brew install libvidstab
brew install ffmpeg

Now let’s move on to the process.

Step 1: Motion Analysis

First you need to analyze the motion of the video and create a file with stabilization parameters.

ffmpeg -i input.mp4 -vf vidstabdetect=shakiness=10:accuracy=15 transfile=transforms.trf -f null -  

Parameters:

shakiness: Video shake level (default 5, can be increased to 10 for more complex cases).
accuracy: Analysis accuracy (default 15).
transfile: File name to save the motion parameters.

Step 2: Apply Stabilization

Now you can apply stabilization using the transformation file:

ffmpeg -i input.mp4 -vf vidstabtransform=input=transforms.trf:zoom=5 output.mp4

Parameters:

input: Points to the file with transformation parameters (created in the first step).
zoom: Zoom factor to remove black edges (e.g. 5 – auto zoom until artifacts are removed).

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *