Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Video stabilization using VidStab and FFMPEG (Mac OS X)

Video stabilization using VidStab and FFMPEG

Examples here use the default settings, see the VidStab readme on GitHub for more advanced instructions.

Here's an example video I made

Install ffmpeg with the vidstab plugin from homebrew

brew install ffmpeg --with-libvidstab

Analyze your video to create a .trf file

This won't modify your video or create a new video, but it should create a new file called transform.trf

ffmpeg -i clip.mov -vf vidstabdetect -f null -

Use the .trf file to create a stabilized video

ffmpeg -i clip.mov -vf vidstabtransform=smoothing=5:input="transforms.trf" clip-stabilized.mov

This should create a new stabilized video called clip-stabilized.mov

Bonus: create a side by side comparison video

Found on a forum here

ffmpeg -i Clip8.mov -i Clip8-vidstab.mov -filter_complex "[0:v:0]pad=iw*2:ih[bg]; [bg][1:v:0]overlay=w" merged.mov
@paulirish

This comment has been minimized.

Copy link

paulirish commented Jul 6, 2017

for convenience.. the last side-by-side command with "corrected" filenames:

ffmpeg -i clip.mov -i clip-stabilized.mov -filter_complex "[0:v:0]pad=iw*2:ih[bg]; [bg][1:v:0]overlay=w" merged.mov

After researching, it appears you can now simplify this to use the hstack or vstack filter, depending on if you want them side-by-side or stacked vertically:

# side-by-side
ffmpeg -i clip.mov -i clip-stabilized.mov  -filter_complex hstack merged.mov

# vertically stacked
ffmpeg -i clip.mov -i clip-stabilized.mov  -filter_complex vstack merged.mov

And here's a my two-liner for creating a stablized video and then a side-by-side comparison one (in fish shell):

set VID "sourcevideo.m4v"
set VIDST "$VID-stablized.mov"; ffmpeg -i $VID -vf vidstabdetect -f null -; ffmpeg -i $VID -vf vidstabtransform=smoothing=5 $VIDST; ffmpeg -i $VID -i $VIDST -filter_complex "[0:v:0]pad=iw*2:ih[bg]; [bg][1:v:0]overlay=w" "$VID-merged.mov"
@mrgloom

This comment has been minimized.

Copy link

mrgloom commented Nov 12, 2018

What is -f null - do?

@eladkarako

This comment has been minimized.

Copy link

eladkarako commented Nov 16, 2018

@mrgloom ..that is the syntax of the 1st-phase,
here is an extended version, that I use in a Windows-compatible batch-file.

ffmpeg -y -i "%~1" -to "00:10:00.000" -an -vf "setpts=PTS-STARTPTS,vidstabdetect=result=data.trf"   -f null  nul
ffmpeg -y -i "%~1" -to "00:10:00.000" -an -vf "setpts=PTS-STARTPTS,vidstabtransform=input=data.trf" out.mkv

::---------------------------------------------------------------------------------------
::advance features (slower), in additional, the use of the 'unsharp' filter.
::  https://ffmpeg.org/ffmpeg-filters.html#toc-vidstabdetect-1
::  https://ffmpeg.org/ffmpeg-filters.html#toc-vidstabtransform-1
::ffmpeg -y -i "%~1" -to "00:10:00.000" -an -vf "setpts=PTS-STARTPTS,vidstabdetect=result=transforms.trf:shakiness=3:accuracy=10:stepsize=12:mincontrast=0.5:show=0"   -f null  nul
::ffmpeg -y -i "%~1" -to "00:10:00.000" -an -vf "setpts=PTS-STARTPTS,vidstabtransform=input=transforms.trf:smoothing=20:optalgo=gauss:maxshift=-1:maxangle=-1:crop=black:interpol=bicubic:zoom=5,unsharp=5:5:0.8:3:3:0.4" out.mkv

the null is a ffmpeg-preserved phrase, for forced-format encoding, to essentially do not output anything from the encoder-phase, but to still run it, it is used in various filters that need to 'walk over' the video-frames, the output-file is usually handled by the filter, as one of its-given variables. In the example above the output is actually written by the filter, using the file name, given in the result= part (and then again in the input= part for the 2nd-phase).

The nul at the end (missing l) is Windows-related and essentially same as /dev/null on *nix systems, it keeps the standard/error streams empty (less I/O).

@diehl

This comment has been minimized.

Copy link

diehl commented Nov 23, 2018

So I've installed ffmpeg as indicated above using homebrew on a Mac and run a bunch of tests to stabilize a video. The problem is that none of my attempts have yielded any visible stabilization. Wondering if anyone else has encountered this?

@kumowoon1025

This comment has been minimized.

Copy link

kumowoon1025 commented Nov 23, 2018

So I've installed ffmpeg as indicated above using homebrew on a Mac and run a bunch of tests to stabilize a video. The problem is that none of my attempts have yielded any visible stabilization. Wondering if anyone else has encountered this?

Do you mean the output looks identical to input?

@ArneAnka

This comment has been minimized.

Copy link

ArneAnka commented Jan 26, 2019

Having a hard time with ffmpeg version 4.1, doesn't support either --with-libvidstab or --enable-libvidstab :(

EDIT: using brew on MacOS

@davidschlachter

This comment has been minimized.

Copy link

davidschlachter commented Jan 27, 2019

@ArneAnka Homebrew is removing options for installing customizing packages, including FFMPEG with libvidstab.

@ArneAnka

This comment has been minimized.

Copy link

ArneAnka commented Jan 27, 2019

@ArneAnka Homebrew is removing options for installing customizing packages, including FFMPEG with libvidstab.

That was a bummer...

@pnbv

This comment has been minimized.

Copy link

pnbv commented Feb 13, 2019

@ArneAnka Regarding current state of macOS homebrew installation (with options), feel free to use the following:

brew remove ffmpeg

brew tap pnbv/homebrew-ffmpegvidstab

brew install pnbv/homebrew-ffmpegvidstab/ffmpeg

Check brew tap docs and manage your own custom formula (for vid.stab only you can just duplicate mine, above)

@kpennell

This comment has been minimized.

Copy link

kpennell commented Feb 26, 2019

@pnbv Thanks for making this.

https://docs.brew.sh/Taps

@kibotu

This comment has been minimized.

Copy link

kibotu commented Aug 23, 2019

https://github.com/varenc/homebrew-ffmpeg#installation-and-usage

brew tap varenc/ffmpeg
brew install varenc/ffmpeg/ffmpeg --with-libvidstab
@mkormendy

This comment has been minimized.

Copy link

mkormendy commented Oct 8, 2019

@kibotu what is the difference between varenc's version over pnbv's?

@kibotu

This comment has been minimized.

Copy link

kibotu commented Oct 8, 2019

well not quite sure exactly, but that one worked for me and the other didn't :/ just wanted to drop this info in case someone else couldn't get the main example to work :3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.