Once thought of as outdated art forms, animated GIF images have now come back. If you haven't noticed, quite a few online sharing and social networking sites are now supporting animated GIF images, for example, on Tumblr, Flickr, Google+, and partly on Facebook. Due to their ease of consumption and sharing, GIF-ed animations are now part of mainstream Internet culture.
So some of you may wonder how you can create such animated GIF images. There are various online or offline tools dedicated to create animated GIF images. Another option is to create an animated GIF image off of an existing video clip. In this tutorial, I will describe how to convert a video file to an animated GIF image on Linux.
As a more useful example, let me demonstrate how to convert a YouTube video to an animated GIF image.
Step 1: Download a YouTube Video
First, download a YouTube video that you would like to convert. You can use youtube-dl tool to save a YouTube video as an MP4 file. Suppose you saved your favorite YouTube video as "funny.mp4".
Step 2: Extract Video Frames from a Video
Next, install FFmpeg on your Linux system, which I will use to extract video frames from the video.
The following command will extract individual video frames, and save them as GIF images. Make sure to use the output file format ("out%04d.gif") as is. That way, individual frames will be named and saved properly.
ffmpeg -t <duration> -ss <starting position in hh:mm:ss format> -i <input_video> out%04d.gif
For example, if you want to extract the video frames of input video, for 5 seconds, starting at 10 seconds from the beginning, run the following command.
After FFmpeg is completed, you will see a list of GIF files created, which are named as "out[\d+].gif".
Step 3: Merge Video Frames into an Animated GIF
The next step is to merge individual GIF files into one animated GIF image. For that, you can use ImageMagick.
First, install ImageMagick on your Linux system if you haven't done so.
Then, run the following command to merge created GIF images into a single animated GIF file.
convert -delay <ticks>x<ticks-per-second> -loop 0 out*gif <output-gif-file>
In the command, "-delay" is an option that controls the animation speed. This option indicates that [ticks/ticks-per-second] seconds must elapse before the display of the next frame. The "-loop 0" option indicates infinite loops of animation. If you want, you can specify "-loop N", in which case the animation will repeat itself N times.
For example, to create an animated GIF image with 20 frames-per-second and infinite loop, use the following command.
Step 4 (Optional): Reduce the Size of an Animated GIF
The last (optional) step is to reduce the size of the created GIF file, by using ImageMagick's GIF optimizer.
Use the following command to reduce the GIF size.
Now you are ready to share the GIF image on your social networks. The following shows a sample GIF image that I created from a cute YouTube video.
Subscribe to Xmodulo
Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.
Did you find this tutorial helpful? Then please be generous and support Xmodulo!
Latest posts by Dan Nanni (see all)
- How to switch from NetworkManager to systemd-networkd on Linux - August 31, 2015
- How to set up a system status page of your infrastructure - August 25, 2015
- How to configure virtual hosts in Apache HTTP server - August 24, 2015