How To Create Animated GIFs With FFmpeg
Learn to install and use this free video converter
I use animated GIFs in my articles to show commands at work in my terminal, see here for an example. The beauty of an animated GIF is that it is embeddable on a webpage like any other image. There are no plugins or video tags needed, so it works on many devices and platforms.
If you want to teach or show your readers something, and don’t want to resort to YouTube to host a video of it, consider an animated GIF. Just keep in mind that animated GIFs are not the most efficient at storing video, so the file size can grow very quickly if you’re not taking the proper precautions.
So how do you create an animated GIF? You can find a plethora of video converters out there. I abandoned all these programs for an open-source and free tool called FFmpeg. FFmpeg is actually at the core of many video tools and websites.
The name is anything but attractive. That it is a command-line tool might put off even more people. But it’s not that difficult, and I’m convinced you can be up and running with FFmpeg by the end of this tutorial!
What is FFmpeg?
The definition given by the project itself describes it better than I can:
“FFmpeg is the leading multimedia framework, able to decode, encode, transcode, mux, demux, stream, filter and play pretty much anything that humans and machines have created. It supports the most obscure ancient formats up to the cutting edge. No matter if they were designed by some standards committee, the community or a corporation.”
Nothing about the above statement is a lie or exaggerated marketing speech. FFmpeg is in fact that awesome and feature-rich.
I’ll take a non-linear approach here and first show you what you can do with FFmpeg. If that convinces you, I’ll help you with the installation at the end of this article.
How to create animated GIFs
On a Mac, you can create a video of your screen by pressing: Shift-Command (⌘)-5. You can record the entire screen or just a portion you select. I highly recommend selecting a small portion, to keep the file size small.
Windows 10 also includes a way to record your screen, read more about it here. On Linux, you can use a tool like Kazam or SimpleScreenRecorder.
While writing this article, I created a small video of myself typing. It resulted in an 887K MOV file. I converted that to a GIF:
ffmpeg -i me-writing.mov me-writing.gif
Now that’s not too complicated, right? The
-i option stands for “input file”. FFmpeg detected the input type and guessed the requested output format based on the extension
The result is a 387K GIF file. That’s not bad. We can reduce the file size even more by reducing the frame rate to 4 frames per second, which is enough for a simple video with very little action:
ffmpeg -i me-writing.mov -r 4 me-writing-r4.gif
As you may have guessed, -r means “frame rate”. The result is a 146K file. Now we’re getting somewhere! We can reduce it further, with some down-scaling:
ffmpeg -i me-writing.mov -r 4 -vf scale=640:-1 me-writing-r4–640.gif
We scaled the video to a width of 640. FFmpeg calculates the height for us because we entered -1. It will keep the aspect ratio of the original video.
Our file is now 110K. Not bad! We went from an 887K video to a 110K animated GIF, without losing information. Here’s the result:
You now have the basics to create animated GIFs and make your articles stand out! You can play with the parameters to find the sweet spot for the video you made. A frame rate of 4 will not work for everything, but perhaps 8 is enough to get the point across and still reduce your file considerably.
If you’re convinced, you can use these pointers to install FFmpeg on your computer.
If you’re on macOS, use Homebrew. If you don’t have Homebrew yet, read my tutorial on it. Once you have it, use the following command to install FFmpeg:
brew install ffmpeg
On Linux, it depends on the distro you are running. FFmpeg will often be part of the base package repository. If it’s not, there’s usually some repository available that you can easily add. Something not known to many: you can use Homebrew on Linux too! It’s an easy way to get the latest, bleeding-edge version of the software.
I don’t use Windows, but you can download the latest FFmpeg build for Windows on this site.
With very basic commands, you can create compact animated GIFs for your articles. Just take these three points in mind:
- Capture only that which is necessary to get the point across. The smaller the area, the smaller your GIF will be
- Reduce the frame rate as much as possible
- Rescale to video to smaller dimensions