Video Stabilization with FFmpeg

by Carsten Lohrmann last modified 2017-08-22T23:49:19+02:00
How to stabilize shaky camera footage (for example from Cellphones, GoPros or Drones) with free Open Source tools.

Stabilizing shaky video footage is not an easy task. I tried various solutions, and most of them did not turn out good enough. Some even made it worse.

One requirement for me was: I wanted to find a solution which can provide me

  • good quality
  • is freely available and preferable Open Source


The most promising solution seemed to be the library vid.stab, which is integrated into the Linux command line tool transcode.

Transcode however does not allow me to output to H264 directly. There is melt which solves this...however my melt version was missing the right algorithm, therefor didn't work as good as expected.

You can do (maybe even quite reasonable) video stabilization with the 3D modeller Blender, however it involves quite some manual work and understanding of Blender itself.

Then i found out, that vid.stab is integrated into least in my version (N-74248-g107026e).

So, after reading the man-pages, pulling information from vid.stab website and various other resources, i came up with the following two command lines:

Step 1. Calculating the stabilization vectors.

In this step, we calculate the stabilization vectors with some parameters which define how much we want to stabilize. These parameters will be saved in the file "transform_vectors.trf" (file can be deleted, after both steps are completed and output video delivers expected quality). The following example is for extreme stabilization tasks. If you only need to do soft stabilization, reduce shakiness to 3-4.

ffmpeg -i SHAKY_INPUT_VIDEO.mp4 -vf vidstabdetect=stepsize=6:shakiness=8:accuracy=9:result=transform_vectors.trf -f null -

Step 2. Transcoding the video with the data from Step 1 into a nice and smooth output video file.

This step transcodes the video based on the stabilization vectors calculated in Step 1. Again this is for an extreme example of shaky footage. If you only want to correct a bit, reduce values of zoom and smoothing. Zoom is also responsible for cutting away the outer edges, which are used for the stabilization. In any case you will loose content near the border of the video...but thats the cost for the stabilization.

ffmpeg -i SHAKY_INPUT_VIDEO.mp4 -vf vidstabtransform=input=transform_vectors.trf:zoom=1:smoothing=30,unsharp=5:5:0.8:3:3:0.4 -vcodec libx264 -preset slow -tune film -crf 18 -acodec copy SMOOTH_OUTPUT_VIDEO.mp4


During transcoding, in this step, i do NOT transcode the audio track, but simply copy it from input to output. Why make something worse through recoding, if there is no need for it.

If you have any better tips or recommendations for parameters available, or if above tips worked for you, please let me know in the comments.


For further details and the possible parameter settings, take a look here:

FFmpeg is available here (or through your normal package manager in your OS):

fredddy says:
Oct 20, 2015 07:20 PM

I will try those commands and ley you know! Thanks for the post!

Dennis Coonich
Dennis Coonich says:
Jan 10, 2016 07:58 PM

Thanks for this article. It helped me a lot! I now have fairly advanced ffmpeg scripts that stabilize NTSC videos and the results are very very good.

Robin Hahn
Robin Hahn says:
Mar 12, 2016 09:11 PM

Thanks for this: it works a treat. I've googled high and low for exactly this workflow: thank you for explaining it so clearly. I can build on from what I've learned. I have a Sony A6000 which produces .MTS files. These I copied to .mp4 with this command (insert your own filename in the place of 00001):

avconv -i 00001.MTS -c:a copy -c:v copy 00001.mp4

The other thing I've done is make the videos smaller format so they are viewable on FaceBook using this:

ffmpeg -i 00001.mp4 -vf "scale=640x360" -c:v libx264 -preset ultrafast -an 00001.proxy.mp4

Just sharing what's worked for me... :)

William Astle
William Astle says:
Jun 03, 2016 10:40 PM

You don't need to repackage the MTS file into an MP4 file first. You can just feed the MTS file into ffmpeg directly.

Sami Jumppanen
Sami Jumppanen says:
Mar 06, 2017 02:45 PM

This is awesome, thank you! Have to try soon.

Thank you
Thank you says:
Aug 22, 2017 10:16 PM

Awesome tutorial

However you say your example is for extreme cases and zoom should be reduced but your zoom is set to 1
is zoom a number between 0-1 can I enter 0.5 for example ?

thanks !

Carsten Lohrmann
Carsten Lohrmann says:
Aug 22, 2017 11:55 PM

@Thank you: Zoom is defined as follows:
zoom = percentage to zoom >0: zoom in, <0 zoom out (def: 0)
Using a value of .5 should cause 50% zooming out (That should give you a big black area around your video and your video shrinking smaller). A value of "1" should zoom inwards 1%, giving the stabilization more room to move the video in each direction for stabilization. I would just play around and test it on a short video clip to observe how it will effect your video. The best settings might be different depending on the actual video-footage.

Michael says:
Nov 21, 2017 05:08 PM

Thank you, works great!

Ray says:
Aug 25, 2018 11:19 PM

This worked amazingly well! My only change was to set the shakiness to 4. Thanks so much!

Elco says:
Oct 29, 2018 09:06 AM

Thanks for sharing this!
I had some issues finding an ffmpeg version with vidstab, so I decided to use an ffmpeg docker container instead to not have to compile ffmpeg myself.

This bash script will perform the stabilization without having to install anything (except docker)

docker run -it --device /dev/dri:/dev/dri --rm -v $(pwd):/mnt jrottenberg/ffmpeg:vaapi -i /mnt/${input_file} -vf vidstabdetect=stepsize=6:shakiness=8:accuracy=9:result=/mnt/transform_vectors.trf -f null -
docker run -it --device /dev/dri:/dev/dri --rm -v $(pwd):/mnt jrottenberg/ffmpeg:vaapi -i /mnt/${input_file} -vf vidstabtransform=input=/mnt/transform_vectors.trf:zoom=1:smoothing=30,unsharp=5:5:0.8:3:3:0.4 -vcodec libx264 -preset slow -tune film -crf 18 -acodec copy /mnt/${input_file}_stabilized.mp4
rm -rf transform_vectors.trf

Add comment

You can add a comment by filling out the form below. Plain text formatting. Comments are moderated.