FFmpeg Video Processing and Conversion
Introduction
Sometimes, I would need to generate GIFs from videos of certain file size limits, convert from one video format to another. While there are lots of online conversion tools available, they don’t provide privacy for the data and usually they don’t have too many options. FFmpeg, which is open source and has been used in all those online conversion tools, has become my companion.
In this blog post, I am going to introduce some basic usages of FFmpeg for video processing and conversion.
Installation
Ubuntu 18.04 LTS
1 | $ sudo apt-get update |
Examples
Video Samples
Pixabay provides a lot of royalty-free images and videos. Let’s get a 4K squirrel video from Pixabay.
1 | $ wget https://pixabay.com/videos/download/video-948_source.mp4 |
This 10-second 4K MP4-format video takes 52MB.
Convert Video to GIF
We use the default settings to convert with multithreads to accelerate the process.
1 | $ ffmpeg -i video-948_source.mp4 -threads 16 video-948_source.gif |
Holy! The size of the generated GIF is 1.4GB! We need to do something to reduce the size.
There are generally a couple of ways to achieve this:
- Reduce resolution
- Reduce FPS (frame per second)
- Clip
In this case, I did:
1 | $ ffmpeg -i video-948_source.mp4 -ss 1 -t 5 -vf fps=10,scale=480:-1 -threads 16 video-948_source.gif |
-ss
is the starting time of the video clip from the original video. In this case, it is 1 second.-t
is the duration of the video clip from the original video. In this case, it is 5 seconds.-vf
is the filter_graph. In this case, I set the FPS to be 10 and reduce the resolution to be 3840 × 2160 to 480 × 270.
The size of the generated GIF this time is 3.1MB. This is quite suitable for many purposes.
More Options
FFmpeg could do conversions between different formats, such as MP4 to AVI, etc. Simply change the gif
in the command to avi
and you are all set.
There are also more sophisticated options from FFmpeg, please check ffmpeg --help
for more details.
References
FFmpeg Video Processing and Conversion