« June 2007 | Main | September 2007 »

July 02, 2007

How to Convert a YouTube Video to an Animated GIF

With the right tools, it is very easy to convert a YouTube video to an animated GIF in a nearly automated way. This tutorial should work roughly the same way on any operating system as long as you have the required tools:

1. Make sure you have access to all the required tools. As far as command line tools go, you will need wget, grep, sed, and xargs. These come with almost all Linux installations, or on Windows, you can use Cygwin. In addition, you need to have MPlayer installed.

2. Create a script called youtube.sh on the file system with the following contents:

wget -q -O - http://www.youtube.com/watch?v=$1|grep "/watch_fullscreen?.*video_id=.*\&t="|sed "s/^.*\&t=//"|sed "s/\&.*//"|xargs -i wget -O $2 http://youtube.com/get_video?video_id=$1\&t={}

Make it executable by typing chmod +x youtube.sh

3. Find the YouTube video you would like to convert. Make sure it is short, as a 5 minute video will become an animated GIF that is about 36 MB. The URL for the video should look like the following:
http://www.youtube.com/watch?v=34Sb0hGUNIQ
Note the part after the ?v=, e.g. 34Sb0hGUNIQ. Copy this code to your clipboard or write it down.

4. Run the script as follows:

./youtube.sh 34Sb0hGUNIQ ducktales.flv

This will download the video to your computer in Flash Video format. The file will be called whatever you specified when you ran the script, e.g. ducktales.flv. Now we just need to use MPlayer to convert it to an animated GIF.

5. Do the conversion by typing:

./mplayer -vo gif89a ducktales.flv

It takes about as long as the video normally runs in order to finish converting. When it is done, there will be a new file in the same directory called output.gif. Try dragging it into a browser to see it run.

You have just successfully converted a YouTube video to an animated GIF! Click here to see the final output. Note that if it seems slow, it is most likely because the video (7.5 MB) is still loading.

Posted by Chuck at 03:51 PM | Comments (2)