Stabilise video using FFmpeg

Posted: | Tags: til ffmpeg

FFmpeg has the ability to stabilise a video using a 2 pass approach. I first learnt about this from a post on Paul Irish’s website and later also shared the link. Their post also includes a useful single command to stabilise and create a comparison video to see the before and after. The purpose of my entry is to add some references and commands that I’ve found useful mostly for me to look up later.

Pass 1: Video detection

The first pass uses vidstabdetect to analyse the video and output stabilisation data to a file called transforms.trf. Parameters such as shakiness, accuracy, and stepsize can be set to describe how shaky the video is, how accurate the detection process needs to be and the step size in detection.1 The defaults work well enough for me.

# Example given in the docs to analyse very shaky video. 
ffmpeg -i clip.mp4 -vf vidstabdetect=shakiness=10:accuracy=15 -f null -

# Example using the default parameter values.
ffmpeg -i clip.mp4 -vf vidstabdetect -f null -

Pass 2: Video stabilisation

The second pass uses vidstabtransform which takes the file created during the first pass and outputs a stabilised video. This filter also has many parameters to adjust smoothing, interpolation, and zooming. I stick with the defaults which generate decent results. The FFmpeg recommend using this step together with the unsharp video filter.

# Example to use the default .trf file generated to stabilise a video.
ffmpeg -i clip.mp4 -vf vidstabtransform stabilised-clip.mkv

Combining both passes

By combining both passes into a single command we end up with the command below.

export vid="clip.mkv"
ffmpeg -i "$vid" -vf vidstabdetect -f null -; ffmpeg -i "$vid" -vf vidstabtransform "stabilised-$vid"

  1. Kdenlive also expose some of the same parameters if you use that to stabilise video. ↩︎


Related ramblings