# How to stabilize a video using FFmpeg and vid.stab

Submitted by Nicola Rainiero on 2019-07-22 (last updated on 2019-07-23)

Recently I have refurbished my HTC Re Camera, an old instant camera, because I wanted to make some videos. The results were very devastating: I discovered that I have a shaky hand! Fortunately there are FFmpeg and vid.stab that helped me to stabilize them. In this post I explain how to install this plugin and use it, then showing two comparison videos.

HTC Re Camera was an excellent instant camera (produced by the 2014 and now out of production) with the portability as trump card. Furthermore it has a video stabilization but it doesn't help as much when I am running or walking. So I needed to remove the shaking from my videos.

As usual Internet had already a solution and its name is vidstab an optional library for FFmpeg1 or Transcode2. The library can stabilize and smooth out shaky video on Linux. Below there is a summary and my adaptation of two awesome articles that have solved my problem:

## Installation

In Ubuntu 18.04, FFmpeg has not enabled vidstab, so you can compile it or better use an FFmpeg static build from here which has vid.stab enabled: FFmpeg Static Builds. I download it according to my system, extract it in a directory and linked in a bash script that has inside all the needed settings.

## Stabilization Commands

There are many combination and choices, I report the options that I use, called: one step and two steps. In the first FFmpeg doesn't analyze the video stream, it is more fast but the quality is better in the second.

### One step

ffmpeg -i input.mp4 -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 output.mp4

### Two steps

ffmpeg -i input.mp4 -vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transforms.trf -f null -
ffmpeg -y -i input.mp4 -vf vidstabtransform=input=transforms.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4 -vcodec libx264 -tune film -acodec copy -preset slow output.mp4

Finally this is the script that I usually launch inside the directory with the video files3:

#!/bin/bash
# Sources:
# https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos
# https://stackoverflow.com/questions/11552565/vertically-or-horizontally-stack-several-videos-using-ffmpeg
# https://ffmpeg.org/pipermail/ffmpeg-user/2017-August/037057.html

if [ -z "$1" ] then echo "Usage:" echo "stabilizator.sh filename.mp4" exit 0 fi # one step$path/ffmpeg -y -i $1 \ -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4${1%.*}_oneStep.mp4

# two steps
$path/ffmpeg -y -i$1 \
-vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transforms.trf -f null -
$path/ffmpeg -y -i$1 \
-vf vidstabtransform=input=transforms.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4 \
-vcodec libx264 -tune film -acodec copy -preset slow  \

## Related Content:

### Nicola Rainiero

A civil geotechnical engineer with the ambition to facilitate own work with free software for a knowledge and collective sharing. Also, I deal with green energy and in particular shallow geothermal energy. I have always been involved in web design and 3D modelling.