Video Stabilization with FFmpeg

by Carsten Lohrmann last modified 2017-01-31T07:31:23+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 FFmpeg...at 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.

References

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

http://public.hronopik.de/vid.stab/features.php

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

http://www.ffmpeg.org/

fredddy
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.

Add comment

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