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

7 thoughts on “Video Stabilization Using VidStab and FFmpeg on Linux

  1. Daniel

    You might want to do shallow git clones to save a lot of download time like so:

    git clone –depth 1 git://git.videolan.org/ffmpeg.git
    git clone –depth 1 https://github.com/georgmartius/vid.stab.git

    this way you only get the HEAD revision instead of the whole version history, which is a lot bigger.

    Reply
  2. Filip

    I tried this and found out that:

    1) following libraries turned out to be missing on my “Ubuntu Vivid” system: libebur128 librubberband libsnappy libx265 x11grab chromaprint

    Thus, the configure command for ffmpeg had to be changed to:
    ./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-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

    2) The $LD_LIBRARY_PATH had to be changed to point to /usr/local/lib to make new ffmpeg run

    3) The result of unshaking the videos recorded from VHS casettes was less than satisfactory – unfortunately, the motion blur introduced perhaps by the videocamera is much more apparent when the images are stabilized. Eventually, I had to turn the unshake function off for my videos. Either they were steady enough, or the camera was shaky and then I had serious trouble with motion blur.

    Thank you anyway for sharing! If you have some tip to alleviate the motion blur, I will be happy to try it out.

    Reply
  3. Andreas Mueller

    On Ubuntu 17.04 I get
    Unknown option “–enable-libebur128”
    Unknown option “–enable-libschroedinger”.
    Unknown option “–enable-x11grab”.

    I also needed to install libmodplug-dev, librtmp-dev

    But then
    ./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 –enable-avresample –enable-avisynth –enable-gnutls –enable-ladspa –enable-libass –enable-libbluray –enable-libbs2b –enable-libcaca –enable-libcdio –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-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-libdc1394 –enable-libiec61883 –enable-libzmq –enable-frei0r –enable-chromaprint –enable-libopencv –enable-libx264 –enable-pic –enable-nonfree –enable-libvidstab

    worked.

    Reply
  4. Andreas Mueller

    Hm still doesn’t work. ffmpeg needs libchromaprint , which needs libavcodec57, but the debian package generated by checkinstall conflicts with libavcodec57.

    Reply
  5. Scott Miller Post author

    Yeah this is a pain to get compiled and working on recent distros. I do not have a good solution for Ubuntu / Debian at the moment.

    Reply
  6. Areeb Soo Yasir

    I’ve tried this on some videos it works well but on some videos it seems to make things look a bit blurry and warped. Does anyone have some solutions or possibly better settings for accuracy, unsharp etc..?

    Cheers

    Areeb

    Reply

Leave a Reply

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

Notify me of followup comments via e-mail. You can also subscribe without commenting.