Converting GIFs to Video

I’m still working on the digital picture-frame.  The frame downloads animated GIFs from the internet onto a Raspberry Pi and displays them on a screen.  The GIF should loop for a period of time before move onto the next image.  The script currently pulls the top-ten animated gifs from Reddit’s GIFs forum.  For an example, I am using this image, pulled at random:

Downloaded from Reddit
Downloaded from Reddit

I am having severe performance issues displaying the GIFs on the Raspberry Pi, and it seems my best option is to convert the GIF to a video format.  I want to play the video in a loop – and hopefully the Raspberry Pi can handle video better than the animated GIFs.  Installing ffmpeg on the Raspberry Pi was trivial.  I am loving package managers these days.

$ apt-get install ffmpeg

I tried to convert the GIF files directly to an AVI, but the videos were all coming out one frame.  I found some discussions online showing I should convert the Gif into component frames – then convert the frames to video.  I ran:

$ convert 0.gif out%05d.jpg

The command separated the GIF into components, but only the first frame looked right.  The rest of the frames contained severe artifacts like this:

8th Frame with bad artifacts
8th Frame with severe artifacts

I used the -coalesce flag to correct the issue, and the frame came out without any severe artifacts.

$ convert -coalesce 2.gif out%05d.jpg
8th Frame without severe artifacts
8th Frame without severe artifacts

I ran ffmpeg on the resulting JPG frames with an AVI output.

$ ffmpeg -r 25 -i out%05d.jpg -y -an test.avi

The video runs smoothly in VLC on my PC.  You can see the results here.

Still to do:

  • Bake the GIF -> Video conversion into the ruby script
  • Write the display script to loop the video on the Raspberry Pi
  • Dynamically change the frame-rate depending on the properties of the GIF