The PCLOG

Grab a plunger, it's gonna be ugly

Video Stabilization

Awesome image from Dave Coleman (https://dribbble.com/shots/1846204-Shake-It-Off)

 

I recently shot some video of some friends.  Before I get it to them, I’d really like to make it not only cleaner, but stabile.  Not wanting to spend a ton of money, I’m going to see what is free and out there.

Enter the library vid.stab

While it looks like it can run on it’s own, I’m using it with FFMPEG on a Mac with OSX.  First Install both of these tools.  Assuming you’re using a Mac, that you want to code and you’re fairly lazy (well done), you should be using a package manager.  I use brew (https://brew.sh/).

  • Can this be done with other package managers or manually?  Emphatically YES!
  • Will I be talking about that at all?  Not in the least!

Assuming you have previously (or just now) installed Brew, I will continue as thought it is already working on your computer.  I am also going to assume you know how to use the command line.  If not… try to keep up.

Install FFMPEG with VID.STAB

Update brew then install FFMPEG with brew

# Update your brew repo.  If you just installed this will be fast 
# otherwise...
>> brew update

# Install ffmpeg with libvidstab (stabilization). If you already 
# have ffmpeg installed, you will get an error. Do the below
>> brew install ffmpeg --with-libvidstab

# If ffmpeg is already installed, run the below command which will 
# reinstall / rebuild ffmpeg with libvidstab
>> brew reinstall ffmpeg --with-libvidstab

# I think it automatically updates the path, but if it complains 
#when you run, you may have to run the below line.
>> export LD_LIBRARY_PATH=/usr/local/Cellar/libvidstab/1.1.0/:$LD_LIBRARY_PATH

** WARNING: If you do the reinstall, I’m guessing that it will remove any extra libraries you may already have.  You should check which are installed and include all of them on the command line.  Nope. I don’t know how to do that off the top of my head ***

Use FFMPEG with VID.STAB

Generate a Mapping:

(https://github.com/georgmartius/vid.stab)  Per the project page, you will be doing two pass encoding.  The first pass is the scan pass.  The second pass re-encodes the video.  I’ve been using this for all of 30-60 minutes and here is what I’ve used so far.

I’ve specifically found that flash photography pisses off the stabilization library.  In efforts to resolve that, I’m using the contrast option (set really high) to try and ignore those frames.

ffmpeg -i test.mov -vf vidstabdetect=tripod=1:shakiness=1:accuracy=15:mincontrast=.7:result="mytransforms.trf" -f null -

 

Based on several reading of the docs and how I think the library works:

  • shakiness=1 (low)
    • I was not moving and the camera really shouldn’t shake much
  • accuracy= 15(high/max?)
    • I want this to be good
  • mincontrast = .7 (high)
    • The scale is from 0.0 to 1.0.  I’ve done a couple tests and did not get all the flashes at .5.  I may go higher.
  • tripod=1
    • Use frame 1 as the reference frame to try and remove all motion

A whole bunch of things will appear on scren.  When this is done running, you will have a text file named mytransforms.trf that details all the detection motion by frame.

Test the Mapping

The below command will take the motion mapping and overlay the motion on top of the video so you can see what was detected.  It outputs to a different file

ffmpeg -i test.mov -vf vidstabdetect=show=1 dummy.mov

  • show=1
    • show=0 shows nothing
    • show=1 shows boxes and the estimated pixel movement
    • show=2 shows more lines (no clue

 

Results so far are bloody well not great.  I’ve gotten tripod to work, but have not filtered out the flashes (bloody flash).

 

 

Notes for rest of post

The tripod option is different in the two different modes.  In the first call, tripod tells the frame to use as the reference. “tripod=500” uses frame 500 as the motion reference.  In the second call, tripod is a boolean to let it know its in tripod mode… so only 0 and 1 are valid items.  It also has to be on a line by itself.

 

 

Published by

2 responses to “Video Stabilization”

  1. I appreciate your post a lot, however when I run the line to install (or reinstall) ffmpeg with libvidstab and I get the error: invalid option: –with-libvidstab
    Any idea how I can resolve this? I’m running on Mac OS Mojave

    Like

    1. I haven’t touched this in a while, so this is more of a guess than a solid answer. I believe that error means that the library hasn’t been installed yet. This link has a bit more detail about installing. https://brewinstall.org/Install-libvidstab-on-Mac-with-Brew/

      Like

Leave a comment