27

Is there any way to convert an animated gif to a video format (e.g. .avi) on Linux?

I have already tried

ffmpeg -i thegif.gif thevideo.avi

but all I get is the first image of the video.

David Foerster
  • 1,461
  • 1
  • 14
  • 23
john-jones
  • 7,490
  • 18
  • 53
  • 86
  • ffmpeg -i foo.gif foo.mkv works with current ffmpeg, and even works right with variable framerate animated gifs. With mkv output, you get a variable frame rate mkv. With mp4 output, it bumps up the fps as high as needed, and then duplicates frames as needed to have things display when appropriate. e.g. a gif with some 25fps and some 10 fps made a 50fps mp4, with 2x dups and 5x dup frames depending on which part. Storing duplicate frames doesn't take much space at all, IDK if they're even in the h.264 stream or just mp4 container. – Peter Cordes Nov 12 '14 at 13:58

5 Answers5

46

ffmpeg's gif input doesn't work too well. It's usually easier to unpack the gif frames with convert (from ImageMagick) and process these with ffmpeg:

convert some.gif some%05d.png  
ffmpeg -i some%05d.png some.avi  
rm some*.png
Nordic Mainframe
  • 28,058
  • 10
  • 66
  • 83
  • Thanks I was searching for your first command line : `convert some.gif some%05d.png` – Dorian Aug 08 '12 at 15:17
  • 3
    If some frames are broken because they only show difference with the base frame, use `-coalesce` option to `convert`. – Hnatt Jun 13 '16 at 08:39
  • when using `-coalesce` you may need to delete the first frame because it has not the same format as others (`PseudoClass` instead of `DirectClass`). It may exist a method to convert it... – xiawi Feb 25 '17 at 16:45
  • This doesn't take into account the timing information for each frame. All frames will be displayed with the same frame rate. – BarbaraKwarc Jul 26 '17 at 03:08
13

I can suggest combination of imagemagick and ffmpeg

do this to extract each frame as png/jpeg

$magick> convert 'images.gif[0]' image.png

Do this to convert images to movie sequence

ffmpeg -f image2 -i image%d.jpg video.mpg

More help on commands

http://www.imagemagick.org/script/command-line-processing.php

http://www.catswhocode.com/blog/19-ffmpeg-commands-for-all-needs

http://electron.mit.edu/~gsteele/ffmpeg/

Arshdeep
  • 4,281
  • 7
  • 31
  • 46
9

If you like to have a certain framerate as input because the resulting video is to fast or to slow

ffmpeg -r 'xx' -i some%05d.png some.avi  

where xx is the input framerate.

Crami
  • 406
  • 1
  • 4
  • 11
3

If you dont want temporary files, you can try mencoder:

mencoder myfile.gif -mf fps=25 -o myfile.avi -ovc lavc -lavcopts vcodec=mpeg4
user3507085
  • 700
  • 5
  • 17
0

If you like to have a loop as output

ffmpeg -loop 1 -t x -i some%05d.png some.avi

where x is the time the video should run in seconds.

Source

äxl
  • 11
  • 1