Note: this is not Linux-specific. the commands are essentially the same on the windows version of mplayer. The GIMP is also cross platform. see the comments for more details.

MPlayer is a pretty powerful tool for processing video files. It has a built-in command line option that will export specified movie frames to a GIF. Such a feature seems like it would make the guide I'm presenting here obsolete, but the problem with the feature is that the resultant GIF looks terrible: too few colors. As far as I've seen, the best way to get good quality animated GIFs using only free software involves using a combination of MPlayer's command line tools and a little bit of elbow grease in The GIMP. We will export the desired segment of video to a series of JPEG files, then use The GIMP to combine those files into a nicely animating GIF that should look nearly as good as the original video. I believe that it is possible to get MPlayer and The GIMP together in a script that will allow the end user to simply point the script to the desired movie file and the desired segment and the script will do all the "dirty work" and create the GIF. This is my end goal, but I have not taken the time necessary to learn any of GIMP's scripting language. If there is a pre-existing solution that allows one to do what I am trying to show here, I'd love to know about it, so please let me know.

Step 1 (for Debian or Ubuntu users):

sudo apt-get install gimp mplayer

Step 2:

mplayer -ao null -loop 0 -ss 0:11:22 -endpos 5 file.avi

This command will display the segment of file.avi on your screen that runs from 11:22 to five seconds later (11:27). It will loop infinitely until you close the window or send a ctrl+c to the terminal window. This command is useful for figuring out what your GIF will look like before you make it. The audio output will not be heard (is set to null)

This command actually creates the jpeg files you will need to make your animated gif. It is similar to the other command, but nothing is displayed on screen, and the command will not loop forever. The command will output the segment to a series of jpeg files in a directory called "moviedirectory".

Step 4:

Now that we have our directory full of jpegs, we should open the first of these files in The GIMP. Then, open the remainder of the images in the directory as layers (File -> Open As Layers). Every image in that directory should now be a layer. Now save the file as a .gif and choose to "save as animation" as opposed to "flatten image." Click export. Lastly, it is important to make sure "loop forever" is checked if you want a GIF that loops forever. The other options here can drastically change the effect of your gif because they change the speed that the gif is displayed at. A relatively fast gif will have a 15 millisecond delay between frames. The default delay of 100 ms is a bit slow in my opinion. Under frame disposal where unspecified I select "one frame per layer." I check "Use delay entered above for all frames" and "Use disposal entered above for all frames." Here are examples of the end result:

If you find that your image is too large, it is often helpful to resize it to be a bit smaller. This can help with the image's performance.

As Firefox is often the platform where people will be viewing your animated gif files, it is also a good tool to use to test them to see what the final product looks like.

If you liked this or thought it was cool, you should read my guide on using FFmpeg.

Thanks for reading! If you have any questions or suggestions, please leave a comment.

151
comments:

Aww, I wish I knew more about computers. I managed to install mplayer for windows, and to get an avi file to play in mplayer, but I'm stuck at how to execute that stuff that pulls the gifs out ... anyone care to tell someone who knows nothing about the command line where to type what?

IMPORTANT - be sure that the -loop 0 option is NOT included when you do step 3 (the step that saves the jpgs). I left that command looping (by mistake, of course) overnight, and woke up to find my hard drive full, all the free space taken up by images.

natonelbronx: have you found that the quality of gifs made with that method is as high as the quality if you output to jpeg first? the first gifs I made with this method about 9 months ago when I discovered it were very ugly (not many colors).

I've been using GIMP for a while and actually forgot I could use it for that. Though instead of MPlayer, I used KMPlayer, the capture options are complicated and require another guide just to explain that lol.

I made this.http://i26.tinypic.com/2d2acn9.gif

Took me ages to add a stroke because I needed to duplicate the Stroke layer, then move it and merge it like 50 times for each layer. :(

I used it with Windows fine! All you have to do is go in the command prompt (Windows key + r [run], and type cmd) and navigate to the folder that you installed MPlayer, and execute the commands from there. worked a treat!

When make gif-animation the Gimp it is hard to open the images as layers one by one if there are many of them. Is there a way to open them all at once?Is it better to resize images by the MPlayer or somehowthe Gimp? I know how it happen the mcplayer: e.g. "-vf scale=160:120"

Im having hard time putting step 2 and step 3 I don't know how does that work? where should I go?! I have mplayer and gimp up and i don't know how to start! I have 3 video that i wanna all 3 short quickie video clip together..

Trying to get this to work in Windows, but I'm having some troubles. I have the Command Prompt open in the mplayer folder.

I copy the first command to preview what the .gif will look like. I replaced the "0:11:22" with "0:00:03" (I want it to start at the three second mark). I changed the "5" to a "3" as well, and of course made sure the file name was correct. I hit enter and either the video box comes up, but is just green, or it will not even start at the indicated starting point. It's as if my time intervals are off or something. Any ideas?

@anonymous using windows (2 comments above): have you tried it with a variety of intervals? if you can't get it to work at all, post again. but sometimes, in my experience, it won't start/stop where you tell it to for some intervals.

@anonymous who needs blog help: do the pics stop for anyone using the site? in different browsers? you might have limited the number of times the animation loops (see step 4 above)

Thanks this guide I've been able to create animate gif using mplayer and GIMP on windows. But as a windows user I later found out the kmplayer and photoshop as a better combo. Kmplayer provides a GUI so it might be a bit more user friendly.

Hi there and thanx for the gif guide. A note to Windows7 (Vista) users: run the mplayer command as administrator or it will output nothing ;). Why does the gifs I made do not play in mediaplayer classic and acdsee too? its says bad or unrecogniced image header ... but works fine in firefox

I'm sorry, but once again, a great idea gets flushed down the toilet by the rest of the internet. I went to the Mplayer site only to get blasted by BINARIES! that aren't even for windows or the option of getting unauthorized compilations from God know what consisting of a hug list of addresses. I picked the 2nd one for windows.

And while the playback function worked as intended, the jpeg extraction FAIL MISERABLY. I adjusted the line for the actual value of when to start and how long to play, etc, but it was totally useless. There is no working way way to create gifs from videos you see on the Internet. I've tried! It's an illusion as well crafted as the idea of a free flow chart program I'd want to use to build programs I want to make.

I'm sorry! I'm glad the illusion did not exist for some of you (not really no I'm not glad for you) and you got the frames, but this is one of the many jokes of the internet where someone says it works! and there isn't any way to repeat the experiment.

Oh sorry >_<. It should have been explained that the output source can only be one string depicting the folder name that will be used in the directory where mplayer was installed. It didn't like my out dir on the desktop path.

Um oh well I couldn't put my name up when I tried, so I guess I can only offer my appologies :S.

On the up hand, my brother really likes GIMP even though I believe Paint.net is superior. So if he likes it, I shouldn't fail too hard with it.

Your Blog is very useful indeed. creating animated gif images is a fun way to customize every profiles for social journal. so enjoy doing it.Want to do more with your digital camera? WEgif create, view and share animated pictures. Auto align images, animate pictures, photo with similar content with one click of a button.

thank you for such comprehensive post... the step by step process was understandable and easy to follow... GIF animation is quite complex that's why posts like this are useful for newbies on this matter... for much more easier way of doing GIF animation there are actually a lot of sites on the net that teaches an individual on how to do this like http://www.wegif.com/ and it is very easy....Gif Photo Animation

-a working, updated linux installation;-a DVD or a video file containing the image sequence;-a DVD drive if you are ripping from DVD;

software tools :

-vlc (player,to rip the videoclip from DVD or to cut the sequence from an existing clip,the result in MPEG format) -avidemux (video encoder,useful to rip the frames in jpg format)-mogrify (image transforming command line tool, part of the imagemagick suite)-gifsicle (command line tool to create and manipulate GIF images: it will create the actual file. it has loads of useful parameters i didn't mention, read the manpage for details)

all those programs are likely to be already installed, except gifsicle. to install gifsicle under ubuntu or debian the command is :

sudo apt-get install gifsicle

( http://www.lcdf.org/gifsicle/ )

or use your system package manager to install the missing software tools.

Once that you have all the software installed and working, and your dvd or videoclip file, here how to make youranimated GIF in nine steps.

2) in the timeline, go to the starting point of the sequence you want to capture ;

3) play the video in vlc and when the part to capture comes in, press RECORD (the red dot button) .press it again to stop recording.

4) in the file browser go to your home directory. you'll find there a .mpg file, name prefix "vlc-record" .

5) open it with avidemux. select the points A and B (start-stop points in selection, keys at the bottom of the window) at the desired time .

6) in avidemux go to menu file --> save --> save selection as JPEG images. a dialog opens. go to the folder chosen to store the single frame images and enter a file name. avidemux will add to the name a numeric suffix (0000, 0001, 0002 etc). click "save". a popup will show at the end telling you the overall number of snapshots saved.

7) go to the folder containing the snapshots in jpg format and open a shell window in that location. type in the following commands:

mogrify -format gif *.jpg

and (example)

mogrify -resize 100x80 *.gif

mogrify is a part of the imagemagick suite. this will convert all your pics in GIF format and resize them to the desired value, set for _example_ to 100x80 .optionally you can delete all the old jpg files with the command :

rm *.jpg

8) in order to reduce the file size, lower the number of frames: in the file browser select all the odd or even gif files by clicking on them while holding the CTRL key. your window will look like a chessboard ... now delete or move them to another folder.

9) now that you have all the frames converted in GIF format, scaled and reduced by half in number it is time to actually make the animated GIF with gifsicle. in the previous shell window type the following sample command that will generate the myavatar.gif file :

gifsicle --delay=10 --loop --colors 128 *.gif > myavatar.gif

note: you can change the colorspace by editing the --colors parameter. --loop is obvious . --delay sets the delay between frames to time in hundredths of a second.

voila, check the file size and if it's too large try using --color=64 , remove more frames, shorten the clip by removing frames or size down all the frames as shown in step #7.

Useful tips. I've often wondered why some GIFs of movie clips were much better quality than others.

But regarding the speed, the author states that 15 ms delay is preferred. That's about 66 frames per second; well above the average used in movies (24fps). That may explain the hyper pace of the samples rendered on Firefox. Windows IE still renders all GIFs at 10 fps (100ms delay). The samples are in slow-mo on IE. Unfortunately the only way for consistency across browsers to set it up for 10 fps which means you'd have to crunch down every other frame or so in the case of movies.

For anyone else who's been having trouble getting output from MPlayer on Windows 7, I managed to finally get my jpgs by invoking mplayer from a different folder (i.e. not inside Program Files). Here's what I did:

I'm still confused about Step 3. I'm just not quite sure where the jpeg files are supposed to end up/how to get them. It all works fine up until that point...I just don't know where the jpegs are. Very confused, can anyone help?

I saw these same steps on the Ubuntu forum. http://ubuntuforums.org/showthread.php?t=745643However, I ran into an issue. There is more detail from my last post on the thread but essentially between the conversion and the process in Gimp my gif quality is not as good as the source. Why is that?

A lot of people already suggested it, but I will complement anyway: I follow all the steps of the post except the Gimp use. Instead, I used ImageMagick as described in http://www.tjhsst.edu/~dhyatt/supercomp/n401a.html

I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Thanks for sharing. Keep up the good work!

I'm a windows 7 user and i have no idea how to do this. How exactly do i do the steps 2 & 3? Maybe i'm stupid but i don't have a clue how to use the command prompt.

Do i need to change directory in the command line? Does it matter where the video file is that i'm trying to use to make the gif? I just had the video in desktop when i tried to do this but the commands weren't accepted.

I hope someone can clarify this to me. I'd appreciate the help. I can't believe this is that hard...

I am usually to writing a blog and i actually appreciate your material. The material has really mountains my interest. I am going to save your website and keep verifying for product new information. seo companies in bangalore

I was recommended this blog site by my comparative. I’m no surer whether this put up is released through him as no one else recognizes such specific about my problems. You are incredible! Thanks! web design services company bangalore

Hello!What can you tell me about this lockmsith company?http://24locksmithnewark.com/ I need some professional opinion. Thanks in advance for your attention. If it's possible please email me as quickly as possible. Thanks in advance!

You really make it seem so easy with your presentation however I in finding this topic to be actually something which I believe I might by no means understand. It sort of feels too complex and extremely large for me. I am looking forward on your subsequent put up, I’ll try to get the hang of it! FCPX