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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment