How to stabilize a video using FFmpeg and vid.stab
Recently I have refurbished my HTC Re Camera, an old instant camera, because I wanted to make some videos. The results were very devastating: I discovered that I have a shaky hand! Fortunately there are FFmpeg and vid.stab that helped me to stabilize them. In this post I explain how to install this plugin and use it, then showing two comparison videos.
HTC Re Camera was an excellent instant camera (produced by the 2014 and now out of production) with the portability as trump card. Furthermore it has a video stabilization but it doesn't help as much when I am running or walking. So I needed to remove the shaking from my videos.
As usual Internet had already a solution and its name is vidstab an optional library for FFmpeg
- Video Stabilization Using VidStab and FFmpeg on Linux
- Stabilizing GoPro Video with FFmpeg and vid.stab
Installation
In Ubuntu 18.04, FFmpeg has not enabled vidstab, so you can compile it or better use an FFmpeg static build from here which has vid.stab enabled: FFmpeg Static Builds. I download it according to my system, extract it in a directory and linked in a bash script that has inside all the needed settings.
Stabilization Commands
There are many combination and choices, I report the options that I use, called: one step and two steps. In the first FFmpeg doesn't analyze the video stream, it is more fast but the quality is better in the second.
One step
ffmpeg -i input.mp4 -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 output.mp4
Two steps
ffmpeg -i input.mp4 -vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transforms.trf -f null - ffmpeg -y -i input.mp4 -vf vidstabtransform=input=transforms.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4 -vcodec libx264 -tune film -acodec copy -preset slow output.mp4
Finally this is the script that I usually launch inside the directory with the video files
#!/bin/bash # Sources: # https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos # https://stackoverflow.com/questions/11552565/vertically-or-horizontally-stack-several-videos-using-ffmpeg # https://ffmpeg.org/pipermail/ffmpeg-user/2017-August/037057.html path="/absolute_path_of_the_downloaded_FFmpeg" if [ -z "$1" ] then echo "Usage:" echo "stabilizator.sh filename.mp4" exit 0 fi # one step $path/ffmpeg -y -i $1 \ -vf vidstabtransform,unsharp=5:5:0.8:3:3:0.4 ${1%.*}_oneStep.mp4 # two steps $path/ffmpeg -y -i $1 \ -vf vidstabdetect=stepsize=32:shakiness=10:accuracy=10:result=transforms.trf -f null - $path/ffmpeg -y -i $1 \ -vf vidstabtransform=input=transforms.trf:zoom=0:smoothing=10,unsharp=5:5:0.8:3:3:0.4 \ -vcodec libx264 -tune film -acodec copy -preset slow \ ${1%.*}_twoSteps.mp4
Examples
The first example is a panoramic view of a rose:
The second one is a short running:
Add new comment