FFmpeg: Video Stabilisation using 'libvidstab'

It is possible to stablise video with standard FFmpeg using the 'deshake' filter, which can produce satisfactory results¹. Another option is to use FFmpeg with the 'vid.stab' library.

In the video above, a side-by-side comparison is made between the original video, and the 'vid.stab' stablised version. The subject matter remains still, while the video content floats around the frame. This is achieved by setting the 'zoom' to a negative value, 'optzoom' to 0, and setting 'relative' to 1. This is not typically desired in most instances, as it creates unusual framing. However, it does mean that no picture information is lost in the process. Note also, how missing information is replaced by the content of previous frames². The other option is to leave these areas black. Further settings information found at Georg Martius's website.

I've created a Bash script to aid in setting the values for video stabilisation (link at end of post). It was intended as a way of getting to grips with different settings, rather than a final application. It initialises a crude interface using Zenity, however all options can be set with this quickly, and it will build complete filters for the first and second pass. It also creates a video using FFmpeg's default values for MKV files. It produces a rudimentary log file, as follows:

**** Wed Jul  6 17:02:07 IST 2016 ****
ARRAY VALUES: |10||||||||200|||0|||1|-50|0||||

# Info:
# 1: Time and date of specific filtering. The filter choices of each run on a video gets added to the same log file.
# 2: Clearly shows the user specified values for filtering (blanks between '|' symbols indicate default value used)
# 3: Filtergraph used for first pass
# 4: Filtergraph used for second pass

To use 'vib.stab' features in FFmpeg, FFmpeg must be compiled using the following procedures (correct as of this post's date)

# Using the FFmpeg compilation method for GNU/Linux, found here
# https://trac.ffmpeg.org/wiki/CompilationGuide/Ubuntu

# Add (or complete) the following to pre-compile procedures
# ---------------------------------------------------------
cd ~/ffmpeg_sources
wget -O vid-stab-master.tar.gz https://github.com/georgmartius/vid.stab/tarball/master
tar xzvf vid-stab-master.tar.gz
cd *vid.stab*
cmake .
sudo make install
# ---------------------------------------------------------

# When compiling FFmpeg, include '--enable-libvidstab' in './configure PATH'

# Create necessary symlinks to 'libvidstab.so' automatically by running
sudo ldconfig

On a final note, vid.stab refuses to work with videos of certain pixel formats, so I encoded all test video as 'yuv420p' which worked without a problem.

¹ Deshake has an advantage over vid.stab, in that it allows setting a region for motion search.
² In some other systems (like 'Deshaker' for VirtualDub ) missing frame information can be interpolated from bi-directional frame analysis.

bash script: http://oioiiooixiii.blogspot.com/p/context-download-binbashset-e-x-script.html

vid.stab home: http://public.hronopik.de/vid.stab/features.php
vid.stab github: https://github.com/georgmartius/vid.stab
initial reading: https://www.epifocal.net/blog/video-stabilization-with-ffmpeg
source video: えんそく (Ensoku) https://www.youtube.com/watch?v=jWdQMgBlXEo