Sunday, September 8, 2019

FFMpeg Deshake Filter



While  I can't speak for others, I've got a pretty shaky hand when shooting videos.  As a result, I tend to set up a tripod and leave the stability to good old mother Earth.

But, if you've got a shaky video, perhaps due to an over-abundance of caffeine then this post may be for you.

Ffmpeg provides a deshake video filter which may reduce the shakiness of a video.  According to the source code, it applies a SAD block-matching motion compensation.  In other words, it compares sequential frames, attempts to detect the vector of the motion, and correct it.

This won't, by any means, eliminate the shakiness entirely (in most cases), but it certainly reduces it substantially, perhaps enough to remove it as a distraction.

Let's go through an example; starting with  a static image, generate a short video, add artificial shakiness, then run it through the deshake video filter.  Lastly, we'll lay the shaky and deshaky videos side-by-side for comparison.

Let's get started.

$ ffmpeg -loop 1 -i image.jpg -c:v libx264 -t 30 -pix_fmt yuv420p -vf scale=640:480 input.mp4

Apply some shakiness;

$ ffmpeg -i input.mp4 -filter:v "crop=in_w/2:in_h/2:(in_w-out_w)/2+((in_w-out_w)/2)*sin(0.015*random(1)):(in_h-out_h)/2 +((in_h-out_h)/2)*sin(0.015*random(n))" shake.mp4

Pass the shaky video through the deshake video filter;
$ ffmpeg -i shake.mp4 -vf deshake deshake.mp4

Finally, lay these two side by side;
$ ffmpeg -i shake.mp4 -i deshake.mp4 -filter_complex '[0:v]pad=iw*2:ih[int];[int][1:v]overlay=W/2:0[vid]' -map [vid] -c:v libx264 -crf 23 -preset veryfast compare.mp4

Resulting in the following video;





No comments:

Post a Comment