February 17, 2018

Stabilizing GoPro Video with FFmpeg and vid.stab

Here are commands to run vid.stab to stabilize shakey GoPro videos. vid.stab is free software that is integrated into ffmpeg, an open source video processing tool with a command line interface. Here’s a very impressive demo of stabilization from vid.stab’s site:

Video from http://public.hronopik.de/vid.stab/

And another video from another user:

Video from https://gist.github.com/maxogden/43219d6dcb9006042849

Get vid.stab with the right version of ffmpeg

brew install ffmpeg installed a build of ffmpeg without the vid.stab plugin compiled in. ffmpeg from the ffmpeg site includes vid.stab, so get it from there.

After linking the ffmpeg executable to the /usr/local/bin folder, running ffmpeg shows me this:

$ ffmpeg
ffmpeg version 3.4.2-tessus Copyright (c) 2000-2018 the FFmpeg developers
  built with Apple LLVM version 9.0.0 (clang-900.0.39.2)
  configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-libzvbi --enable-version3 --pkg-config-flags=--static --disable-ffplay
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
  libpostproc    54.  7.100 / 54.  7.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

I’m using ffmpeg 3.4.2, and the configration line includes --enable-libvidstab which is what I need.

Workflow suggestions

It took me lots of trial and error to find settings I liked. To speed up my process, I:

I skip these flags for the final encode once I find settings I like for a video.

Test different settings

vid.stab’s docs go into detail about what all the parameters like stepsize, shakiness, and accuracy do. Sometimes I wasn’t sure if changing them made a difference.

There are 2 seteps to the stabilization process:

  1. Generate info about shakes and save it to transform_vectors.trf

    • best improvement comes bigger stepsize. 32 is max
    ffmpeg -t 5 -i GOPR7182.MP4 -vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transform_vectors.trf -f null -
  2. Stabilize the video using data from the first pass

    • high smoothing (30) makes for more wobbly jello-y frames, 10 looks good to me
    • for best compression quality: remove scale filter, add ‘-preset slow’
    ffmpeg -t 5 -i GOPR7182.MP4 -y -vf vidstabtransform=input=transform_vectors.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4,scale=480:-1 -vcodec libx264 -tune film -an stabilized.mp4

After a bunch of test encodes, I found some settings that worked for my video, and tweaked the commands a bit for my final output.

Final Output

  1. Remove -t 5 so that the whole video is processed

    ffmpeg -i GOPR7182.MP4 -vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transform_vectors.trf -f null -
  2. For the final output:

    • remove scaling
    • use -acodec copy to copy the audio stream from the source video file to the stabilized video file
    • add -tune film and -preset slow which might help with x264’s compression quality
    • remove -y flag so ffmpeg confirms overwriting previous files
    ffmpeg -i GOPR7182.MP4 -vf vidstabtransform=input=transform_vectors.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4 -vcodec libx264 -tune film -acodec copy -preset slow stabilized.mp4

Other stuff