Video stabilization using the program ffmpeg

Zápisník experimentátora

Each of us who has ever shot a video with a free hand knows the unpleasant feeling that will appear when you watch a recorded video. Without a tripod, the video is almost always shaky. And the shaked video does not affect the viewers well. Using ffmpeg, you can pretty well stabilize the image. This is for the Windows operating system.

Install ffmpeg into a directory and create two directories inside the directory bin. One directory name the input and the other directory name the output. Directories should look like this.

d:\video\ffmpeg-3.4.2-win64-static\bin>dir
Volume in drive D is Nový zväzok
Volume Serial Number is FEB7-3135

 Directory of d:\video\ffmpeg-3.4.2-win64-static\bin

10.04.2018  21:55    <DIR>          .
10.04.2018  21:55    <DIR>          ..
24.02.2018  21:40        45 422 592 ffmpeg.exe
24.02.2018  21:40        45 307 392 ffplay.exe
24.02.2018  21:40        45 330 432 ffprobe.exe
31.03.2018  23:31    <DIR>          input
10.04.2018  21:41    <DIR>          output
10.04.2018  21:50               270 stabilize.bat
               4 File(s)    136 060 686 bytes
               4 Dir(s)  674 146 713 600 bytes free

In the directory, create a file stabilize.bat with the following content.

@echo off
FOR /F "tokens=*" %%G IN ('dir /b input\*.mts') DO (
  del transforms.trf
  ffmpeg -i input\%%~nG.mts -vf vidstabdetect=shakiness=10:accuracy=15 -f null -
  ffmpeg -i input\%%~nG.mts -vf vidstabtransform=zoom=5,unsharp=5:5:0.8:3:3:0.4 output\%%~nG.mp4
  )

This example is written for a Sony camcorder that saves videos to files with the MTS extension. Upload the files from the camera to the input directory. Then run the stabilize.bat program and the result will appears in the output directory.

Using this program, two-phase image stabilization is performed. First, image shaking is detected and the result is saved to the file transforms.trf. Then the stabilization itself is performed, the resulting video gets a little bit bigger in order to keep the artifact from the stabilization removed and the image is slightly sharped. The result will be saved in the file with MP4 extension.


10.04.2018


Menu