by Paul Arnote (parnote)
The GIF graphic format was, at one time, avoided like the plague. Compuserve, which created and patented the one-time popular graphics format, along with Unisys (who owned the patent of the LZW compression algorithm that the GIF format employed), defended their "property" like a hunger-ravaged junk yard guard dog. As a result, developers steered away from creating programs that output GIF files, fearful of being slapped with cease and desist orders and forced into having to pay exorbitant licensing fees. Some developers incited open rebellion against the GIF format, with such activities as "Burn The GIFs" campaigns. In fact, there are still some computer users who feel bitter about the "GIF War" of nearly 20 years ago.
Meanwhile, users tended to shy away from using GIF files for other reasons. First, standard GIF files are limited to a 256 color palette. There are other formats out there that provide a much larger palette that contributes not only to a much higher color resolution, but that also appear much more lifelike in appearance. Second, GIF files -- especially animated GIF files -- tend to be relatively large in file size, especially when compared to the sizes produced by other graphic file formats.
As a result, the PNG graphics format was created to help circumvent the patent issues that encumbered the GIF format. Ironically, Compuserve helped spearhead the development of the PNG format. The PNG format offered a larger color palette and avoided the LZW licensing trap, as well as offering more options than the original GIF format. Most importantly, the PNG format was created to be an open format, free of patents.
Fast forward to today. GIF files are experiencing a resurgence in popularity, and they can be found all over the internet. Animated GIF files are now being used as a replacement for video clips in a lot of cases, especially on social media sites. The resurgence can also be partially linked to the fact that the patents that initially made GIF files taste like soured milk have now expired in 2003 in the U.S., and in 2004 in most of the rest of the world. As a result, the GIF format can now be freely used without fear of licensing headaches.
Fortunately, there are a few ways you can create your own animated GIFs from video files. Most of these methods involve using the command line, but their use is relatively simple and straightforward.
Method 1: Free Online Conversion Service
Using the search term "free video to gif conversion" in Google, you will find over 305,000,000 hits. Wow! That's a whole lot of online services (nearly all serviced by ads that undoubtedly want to track you) that are lined up to convert your video to an animated GIF. If nothing else, it's a testament to how popular animated GIFs from video files really are.
The requirements may vary from site to site, however. Some require you to upload the video file you want to convert. Others require you to provide a link (e.g., a YouTube video URL) for the video you want to convert. For what it's worth, you can use the youtube-dl command line utility or the Firefox Video DownloadHelper plugin to download videos from YouTube (under both utilities) or just about any streaming video site (in the case of the latter). The youtube-dl utility is available in the PCLinuxOS repository, and the Firefox Video DownloadHelper plugin is available from the Firefox plugins page.
In either case, you will be provided a link to download your newly created animated GIF file.
Method 2: Using ffmpeg
With ffmpeg (installable from the PCLinuxOS repository, if you don't already have it installed), you have all that you need ... well, almost. You'll use ffmpeg, followed by the ImageMagick convert command, to create the animated GIF.
My test file is this YouTube video, which I downloaded with the Video DownloadHelper plugin. I chose to excerpt 10 seconds of the video, starting at the 00:01:28 mark. Here is the command I used (entered all on one line):
ffmpeg -i FunnyAmeriquest.flv -ss 00:01:28 -t 00:00:10 animated-gif2jpg
The -i FunnyAmeriquest.flv part of the command specifies the name of the video file I want to use as the input file. The -ss 00:01:28 part of the command tells ffmpeg to start the conversion at that particular point in the video. The -t 00:00:10 part of the command tells ffmpeg that I want to convert the next 10 seconds of video. Finally, the animated-gif2jpg part of the command tells ffmpeg the filename to use as the output file.
Now, we could simply stop right there, as we definitely have an animated GIF file. However, the file size for that 10 second animated GIF is 6.3 MB. One way to make a smaller file is to reduce the frame rate. Add -r 13 to the command, just before the output file name, and the frame rate will be reduced from the typical 24 fps, to 13 fps. Another way is to resize the output by adding -s 320x240, for example, to resize the output image. Again, place the resize command right before the output file name.
However, we can get it even smaller, and that is where using the ImageMagick convert command comes into play. Enter the following command, all on one line:
convert -layers Optimize animated-gif2jpg ani-gif-opt2.gif
This causes ImageMagick to optimize the GIF file. The result is a significant reduction in the file size of the animated GIF. The file size is reduced from a hefty 6.3 MB, to a much more svelte 3.4 MB.
Below is the final result of my conversion. Readers of the web version of the magazine will be able to see the file without any problems. Readers of the PDF version can download the animated GIF from here (3.4 MB) and view it in their default web browser. Reducing the frame rate to 8 fps caused a further reduction in the file size to 2.1 MB, after the ImageMagick optimization.
Method 3: Using mplayer
Ffmpeg isn't the only game in town, when it comes to managing and manipulating multimedia files. Fans of mplayer can use their favorite media player to make the conversion from video to animated GIF.
The command to convert to animated GIF from a video file is as follows (entered all on one line):
mplayer FunnyAmeriquest.flv -ao null -ss 0:01:27 -vo gif89a:fps=8:output=animated-gif3.gif -vf scale=320:240 -endpos 11
The FunnyAmeriquest.flv part of the command specifies the input video file to use. The -ao null part of the command tells mplayer to forget about outputting any audio. The -ss 0:01:27 part of the command tells mplayer to start at the 1 minute 27 seconds spot in the video. The -vo gif89a:fps=8:output=animated-gif3jpg part of the command tells mplayer to output the image portion of the video file as an animated GIF, at 8 frames per second, and to write the output to the animated-gif3jpg file. The -vf scale=320:240 part of the command tells mplayer to rescale the output to 320 x 240 resolution. Finally, the -endpos 11 part of the command tells mplayer that the output will be 11 seconds in length.
Below is the result. Readers of the PDF file can click this link to view the animated GIF in their default web browser. The file is 2.9 MB.
I skipped using the ImageMagick convert command on this file. In all of my tests, the optimized version of the GIF ended up larger than the GIF file output by mplayer. Perhaps it was my choice of videos, but who knows. Your mileage may vary. I would recommend that you try the optimization of the GIF file with the ImageMagick convert command, then choose the smaller file, whichever one that may be.
Comparisons & Caveats
There are other methods to create animated GIFs. One I explored involved installing gimp-gap from the PCLinuxOS repository. Gimp-gap is designed to make animated graphics from video files. However, I found it to be so clunky and unreliable during my testing that I decided against including it in this article. It couldn't even find mplayer, which is installed on my system. There are also other programs in the wild world of Linux, such as gifsicle, but they were excluded because they are not in the PCLinuxOS repository -- and we all know (or should know) by now how installing items from outside the official PCLinuxOS repository is frowned upon.
For whatever reason, the animated GIF files created with mplayer are bigger than the files produced by ffmpeg, even when the same frame rate is used. Notice that in our mplayer example above, the frame rate has been decreased to 8 fps. The lowered frame rate makes the animation a bit choppier and less fluid. But even at this slow frame rate -- which is one-third the normal frame rate -- the animation is still acceptable, albeit barely. The file created with ffmpeg at the same frame rate of 8 fps -- and subsequently optimized with the ImageMagick convert command -- is only 2.1 MB, still a significant 0.8 MB smaller than the animated GIF file created with mplayer.
Also, if you look closely at the two animated GIFs, you will notice that the ffmpeg image is dithered, while the mplayer image lacks the dithered appearance. Even with the dithering, ffmpeg appears to have a quality edge. One video file I was experimenting with produced a very acceptable animated GIF with ffmpeg, but the mplayer result was horrendous. The dithered image produced by ffmpeg definitely has more forgiveness and latitude to work with a greater range of video image quality than mplayer, where the image colors look blocky and sometimes smeared.
Already, the animated GIF files produced with ffmpeg have a definite edge when it comes to producing significantly smaller file sizes. This is significant, since most sites restrict the size of animated GIFs to 5 MB or less. Also, be careful, as it's very easy to create animated GIF files of VERY large size.
Of course, the old adage "garbage in, garbage out" definitely applies when creating animated GIFs from video files. Starting with higher quality image files will produce higher quality animated GIF files. Whenever possible, opt for higher quality MP4 files, over lower quality FLV files. Also, video files that utilize higher bitrates are going to inherently be of higher quality.
As is usual under Linux, there is more than one way to accomplish a task. Creating animated GIFs under Linux is no different. If you think this is complicated, think again. It's actually quite easy to create animated GIFs under Linux. Have fun with this, and try your hand at creating some animated GIFs of some of your favorite, short video clips.