Using ffmpeg in Ubuntu 18.04 to stabilize video shake.
There are a number of internet resources, but they did not make life easier. So here are few simple comments and instructions about the basics. I do assume a simple knowledge of installing software and the command line.
Get the right ffmpeg.
The stabilization is done by filters in ffmpeg - not my idea of what a filter is, but that is just an aside. Some versions of ffmpeg do not contain these filters and then you have to add them (how, I have given up to find out).
One way of getting the relevant ffmpeg version is to add a PPA link to the repositories and then to update the software on your pc. If you use Synaptic, the process is quite simple as you are prompted to update your software, search then for ffmpeg, mark it for installation and apply it.
In any event, here is a more complete explanation doing it entirely via the command line in the terminal.
In this section we will provide step by step instructions about how to install FFmpeg version 4.x on Ubuntu 18.04. The FFmpeg version 4.0 adds a number of new filters, encoders, and decoders. This version is available from the Jonathon F’s PPA.
The steps below describe how to install FFmpeg 4.x on Ubuntu 18.04:1. Start by adding the jonathonf/ffmpeg-4 PPA:
sudo add-apt-repository ppa:jonathonf/ffmpeg-4
If you get an error message saying add-apt-repository command not found then you will need to install the software-properties-common package.
2. Once the PPA is added to your system install the FFmpeg package by typing: sudo apt install ffmpeg
3. Verify the FFmpeg installation by running the ffmpeg -version command: ffmpeg -version
The output should look something like this:
ffmpeg version 4.0.3-1~18.04.york0 Copyright (c) 2000-2018 the FFmpeg developers built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
FFmpeg 4 is now installed on your system and ready to be used.
The next step is to get hold of the video to be stabilized and its location on your pc.
1. Navigate to the folder in the terminal
Navigate with the command line terminal to the relevant folder.
2. Convert to mp4 format (optional, but recommended to get the feel for the process)
In my case, I first converted my video to mp4 format, using the following command line instruction, after having navigated to the folder where my video is. Perhaps it is not necessary but I got the impression that ffmpeg did not like the .MOV file extension. In any event, just to get the feel for the process, keep to the names I used and do a similar conversion where your target video is used in the place of my MVI_0103.MOV video.
ffmpeg -i MVI_0103.MOV shaky.mp4
The converted video will now be found under shaky.mp4 in the same folder.
3.The two core command line instructions
Next run the following command in the terminal, in the folder where the video is (ie the folder you already have navigated to).
This creates two extra files: dummy.avi and mytransforms.trf The dummy.avi apparently shows the stabilization process . The mytransformations.trf file contains instruction the next command line instruction operates on.
So then, in the terminal, in the same folder, you run the following command:
Analyze video stabilization/deshaking. Perform pass 1 of 2, see vidstabtransform for pass 2.
This filter generates a file with relative translation and rotation transform information about subsequent frames, which is then used by the vidstabtransform filter.
This filter accepts the following options:
Set the path to the file used to write the transforms information. Default value is transforms.trf.
Set how shaky the video is and how quick the camera is. It accepts an integer in the range 1-10, a value of 1 means little shakiness, a value of 10 means strong shakiness. Default value is 5.
Set the accuracy of the detection process. It must be a value in the range 1-15. A value of 1 means low accuracy, a value of 15 means high accuracy. Default value is 15.
Set stepsize of the search process. The region around minimum is scanned with 1 pixel resolution. Default value is 6.
Set minimum contrast. Below this value a local measurement field is discarded. Must be a floating point value in the range 0-1. Default value is 0.3.
Set reference frame number for tripod mode.
If enabled, the motion of the frames is compared to a reference frame in the filtered stream, identified by the specified number. The idea is to compensate all movements in a more-or-less static scene and keep the camera view absolutely still.
If set to 0, it is disabled. The frames are counted starting from 1.
Show fields and transforms in the resulting frames. It accepts an integer in the range 0-2. Default value is 0, which disables any visualization.
Video stabilization/deshaking: pass 2 of 2, see vidstabdetect for pass 1.
Read a file with transform information for each frame and apply/compensate them. Together with the vidstabdetect filter this can be used to deshake videos. See also public.hronopik.de/vid.stab. It is important to also use the unsharp filter....
Set path to the file used to read the transforms. Default value is transforms.trf.
Set the number of frames (value*2 + 1) used for lowpass filtering the camera movements. Default value is 10.
For example a number of 10 means that 21 frames are used (10 in the past and 10 in the future) to smoothen the motion in the video. A larger value leads to a smoother video, but limits the acceleration of the camera (pan/tilt movements). 0 is a special case where a static camera is simulated.
Set the camera path optimization algorithm. Accepted values are: ‘gauss’ gaussian kernel low-pass filter on camera motion (default) ‘avg’
averaging on transformations
Set maximal number of pixels to translate frames. Default value is -1, meaning no limit.
Set maximal angle in radians (degree*PI/180) to rotate frames. Default value is -1, meaning no limit.
Specify how to deal with borders that may be visible due to movement compensation.
Available values are: ‘keep’ keep image information from previous frame (default) ‘black’
fill the border black
Invert transforms if set to 1. Default value is 0.
Consider transforms as relative to previous frame if set to 1, absolute if set to 0. Default value is 0.
Set percentage to zoom. A positive value will result in a zoom-in effect, a negative value in a zoom-out effect. Default value is 0 (no zoom).
Set optimal zooming to avoid borders.
Accepted values are: ‘0’ disabled ‘1’ optimal static zoom value is determined (only very strong movements will lead to visible borders) (default) ‘2’ optimal adaptive zoom value is determined (no borders will be visible), see zoomspeed
Note that the value given at zoom is added to the one calculated here.
Set percent to zoom maximally each frame (enabled when optzoom is set to 2). Range is from 0 to 5, default value is 0.25.
Specify type of interpolation. Available values are: ‘no’ no interpolation ‘linear’ linear only horizontal ‘bilinear’ linear in both directions (default) ‘bicubic’
cubic in both directions (slow)
Enable virtual tripod mode if set to 1, which is equivalent to relative=0:smoothing=0. Default value is 0.
Use also tripod option of vidstabdetect.
Increase log verbosity if set to 1. Also the detected global motions are written to the temporary file global_motions.trf. Default value is 0.
4. Odds and Ends
Some video editors (eg Blender, Kendenlive) provide built-in stabilization options. Perhaps that makes it easier, but Blender is a nightmare whatever one does. Stabilization in ffmpeg cries out for a GUI to make life simpler as the insertion of various parameters in the course of experimenting with the best results is time consuming and mind numbing.
Two more changes can be made via the command line of the Terminal to enhance the shaky video.
With the one command line entry the frames per second (fps) of the video can be changed. The altered video is found under the name framerate.mp4
ffmpeg -i inp_stabilized.mp4 -r 40 framerate.mp4
This entry changes the fps to 40 (changing the number 40 in the above example to some other value, would affect the fps correspondingly). Major effects are difficult to detect when conservative changes in the range 20-40 are made. In a video, which I will show later in another post, I increased the fps from 29 to 40 and my impression is that the video is smoother. Note that in this example, the newly enhanced video will be found in the file framerate.mp4
The second change affects the bitrate and within reason a higher bitrate improves the quality of the video (at the expense of an increase in file size)
This entry takes the previously created file (framerate.mp4), increases the bitrate (in the case of my video to be posted later, up from some 10 000 kb/s to some 37 000 kb/s , with a huge increase in file size. The changed file is found under the name bitrate.mp4