Play animated GIF to end of file

How do I play a GIF animation once to end of file instead of using a timer. I have some long animations. I see someone asked this and he found the answer, but didn’t reveal what it was. :slight_smile:

1 Like

The quickest way is to just set the timeout to be longer than your GIFs

There’s not yet a good solution for having a default timeout to be short, and also allow for playing long GIFs.

It’s possible that you can use the error code that decodeFrame() returns to find when the decoder has reached the end of the file. I think ERROR_DONE_PARSING is returned at the end of the file.

1 Like

Thanks for the response. I’ve tried changing the timer for a long animation. But I want to play a series of GIFs that range from about 15 minutes to 10 hours so the timer won’t do.
I thought about the error code for end of file. How do I access that from my sketch?

int errorCode;

errorCode = decoder.decodeFrame();

A better solution is possibly not too far away, but no promises yet.

Thank you, Louis! I appreciate it. I’ll try that in the next day or so.


I’m turning the AnimatedGIFs sketch into a GifDecoder Arduino Library with example sketch:

It’s not yet available with Arduino Library Manager. You’ll need to download and install the library in your Arduino/libraries directory. Take a look at the example sketch for how to play a GIF until it’s complete. Note the code that checks for a complete gif is only available in the new Library version, not your old version. Also note you’ll want to delete the GifDecoder*.h files and lzw*.* files from your sketch directory, and use the new FilenameFunctions.* files that are in the new example.

Please let me know how it goes for you.

It seems to work great! It’s just what I needed to run long GIF animations. Thanks for your efforts!

Check the latest change in that example: I updated the timeout logic to handle millis() overflowing (which happens every 49.7 days). As you’re running really long GIFs, I thought maybe you might be running long GIFs, for a long time and you might want this

Thanks again, Louis. It seems to work great, however I have not run it for 49.7 days. :slight_smile:

Not yet… :wink:

I’m curious what animations you’re running if you want to share. Is it original content, or converted from something online?

No, they aren’t original, but various space, scenic or abstract videos from Youtube. Here are some examples:

I use AnyVideoConverter (free) to download and process the videos. It will create AVIs, MP4s, GIFs and other formats and can resize/

Of course the resolution is limited on a 32x64 display and with GIFs it’s limited to 256 colors (8 bit). But the result is surprisingly good.

These are fun. I’m going to try using a series of BMP picture files for 24 bit color. :slight_smile:


Nice, I like the Electric Sheep animations, and I’ve tried running them on a 32x32 display before.

You might try some tools that are good at maximizing palette usage with the GIF format, before switching to BMPs which seems like more work. I did some searching and found this tool:

Referenced from this post:

I think this might use some rarely used features of the GIF format, and the GifDecoder library might not be able to handle it. If you do run into issues, try changing this define inside GifDecoder.h:

#define NO_IMAGEDATA 2

Try setting to 1, and if that still doesn’t work, 0. I think NO_IMAGEDATA == 2 is the most efficient method, but doesn’t support some features.

If you try the tool, or another tool, or switch over to BMPs, please post back!

Thanks for the ideas, Louis. I found the BMP viewer will do about 15 pics/sec, good enough for animation. I coverted part of Electric Sheep to BMPs using every other frame. Of course I ended up with over 66,000 files. I think Fat32 will only do 32768. It seemed to animate quite well at first, but as it progressed, it got slower and slower. After it was 2000 - 3000 frames in, it was only about 3 pics/sec. The filenames are numbered and it reads them out of the directory. Maybe it will speed up if I generate the filenames in the sketch.
I’ll try the GIF processing ideas you suggested in the next few days. Thanks!

SdFat might be a better/faster library than SD, might want to give it a try! I think they have a similar but not exactly the same API. May want to look at the AnimatedGifs_SD project linked to in the GifDecoder README for a reference, as I believe they converted the project to use SdFat

Also consider grouping images in folders if you’re still trying to get bmps to work

Louis, thanks for all your help. I tried ffmpeg. It seems to work but is a bit of trouble to use. I can covert to jpegs and then to pngs. The gifski to make the gif animation. The results are much better than I was getting with AnyVideoConverter which while it creates gifs, it has no options for how the color and dithering is done. Gifski works much better. In fact my results are such that I may not need the bmp effort. The only cumbersome aspect is the number of png files. One video created 450,000 files. When I’m done, it take quite a bit of time just to delete them! :slight_smile:
Thanks again for your help.

This is very cool stuff! Thanks to both of you.

I would like to be able to use the ESP32’s internal memory to store and play back a 64x64 animated GIF, instead of reading from an SD card. Is this possible with your new library? How would I go about doing that?

I have successfully displayed images by including .c files generated from GIMP, using the teensylc library.

Hi, it’s not implemented in the GifDecoder library yet, but could be. I’m nearly finished adapting the GifDecoder library to use the external AnimatedGIF library to do the decoding, and the AnimatedGIF library supports playing back a GIF that’s stored in memory.

The changes are made in this branch, but I’ll merge it in to the main branch and do a release soon:

This example from AnimatedGIF doesn’t use SmartMatrix Library, but you could probably adapt it to SmartMatrix easily enough:

There’s also the option of using the SPIFFS to store a GIF file. I’m not that familiar with that option. Someone adapted the AnimatedGIFs sketch (which is the basis of the GifDecoder Library) to use SPIFFS, so you might want to refer to their code and try to get that working with GifDecoder.

Feel free to post back and get some help on either path. I’m interested in supporting both options but don’t have a lot of time right now to work on it.

Thank you, Louis. Being that I am a hardware guy (mostly), I feel like I would do best with the merged release you say you are working on.

I just finished up the release moments ago:

Fantastic! Thank you, Louis! I will install the library and see if I can adapt the example to play back a GIF from memory.