Video Stabilization Using VidStab and FFmpeg on Linux

By | 2016/09/17

FFmpeg with an optional library vidstab can stabilize and smooth out shaky video on Linux. Here is a quick how-to and example video!


Grab FFmpeg with libvidstab enabled

To use vid.stab and stabilize videos, an FFmpeg binary with this compile time option is required:

--enable-libvidstab

Most distros included this. However if not:

* One can compile FFmpeg and vid.stab from source (see below).

Other options:

* Use an FFmpeg static build from here which has vid.stab enabled:

https://www.johnvansickle.com/ffmpeg/

* For Debian, the deb-multimedia.org FFmpeg has vid.stab enabled as an option

* Other distros: many have vid.stab enabled in their FFmpeg build. Check by running $ ffmpeg with no options from the command line.


Compile from source on Debian / Ubuntu quick how-to:

First, install some dependencies

sudo apt install yasm nasm \
                build-essential cmake automake autoconf \
                libtool pkg-config libcurl4-openssl-dev \
                intltool libxml2-dev libgtk2.0-dev \
                libnotify-dev libglib2.0-dev libevent-dev \
                checkinstall libavcodec-extra57

Next, install the latest vid.stab repo which is what provides video stabilization for FFmpeg

git clone https://github.com/georgmartius/vid.stab.git
cd vid.stab/
cmake .
make -j4
sudo checkinstall

Next, compile FFmpeg

sudo apt build-dep ffmpeg

git clone git://git.videolan.org/ffmpeg.git

cd ffmpeg

./configure --prefix=/usr/local --extra-version=1+b3 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --shlibdir=/usr/lib/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --disable-stripping --enable-shared --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libebur128 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librubberband --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-chromaprint --enable-libopencv --enable-libx264 --enable-pic --enable-nonfree --enable-libvidstab

make -j4

sudo checkinstall


Example Stabilization Commands

Option A:

Stabilize a video using default settings for a quick fix:

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

Option B:

Better: First let FFmpeg analyze the video (No changes are made- this just makes a file called transform.trf)

1. Analyze with default values:

ffmpeg -i input.mp4 -vf vidstabdetect -f null -

Or analyze a very shaky video, on a scale of 1-10:

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

2. Next, use that generated file transform.trf to help better stabilize the video:

ffmpeg -i input.mp4 -vf vidstabtransform=smoothing=30:input="transforms.trf" output.mp4

Done.


Bonus:

Make a side by side video with:

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v]setpts=PTS-STARTPTS, pad=iw*2:ih[bg]; [1:v]setpts=PTS-STARTPTS[fg]; [bg][fg]overlay=w" side_by_side.mp4



Here is a side by side comparison of very shaky video stabilized with FFmpeg and vid.stab. Left is original and right is stabilized.


More links and info:

https://github.com/georgmartius/vid.stab

http://ffmpeg.org/ffmpeg-filters.html#vidstabdetect-1