Ubuntu – Convert thousands of .pngs to animated .gif, `convert` uses too much memory

command lineconvertimage processingsoftware-recommendation

Many of the questions asking how to create an animated gif from a set of png images suggest to use a variant of ImageMagick's convert command:

convert -delay 2 -loop 0 *.png animated.gif

However, I have a few thousand images and thus convert uses up all my memory, swap, and then crashes. What alternative software exists, which is more memory-conscious? I could use another open format if .gif is not supported, and I do prefer a CLI tool.

Best Answer

It sounds like you're trying to make a video. If that's the case, then I'd use a proper video format.

In this case, I'd use ffmpeg to convert the individual PNG files to a H.264 video. Since ffmpeg is made to work with videos that can be hours long, it should have no problem with your thousands of images. Using H.264 instead of animated gif will result in a vast improvement in image quality.

Something like this should work for you:

 ffmpeg -framerate 1/2 -i img%04d.png -c:v libx264 -r 30 out.mp4
  • -framerate 1/2: This sets the framerate to one-half FPS, or 2 seconds per frame.
  • -i img%04d.png: This tells ffmpeg to read the files img0000.png though img9999.png.
  • -c:v libx264: Use video codec libx264.
    • You can specify video compression parameters here, if you like:
    • -crf <number>: Quality setting. 0 to 51. 23 is the default. 0 is true lossless encoding, which will be quite high bandwidth. 18 is nearly visually lossless.
  • -r 30: Set the output framerate to 30 FPS. Each of the input images will be duplicated to make the output what you specify here. You can leave this parameter off, and the output file will be at the input framerate, but the resulting movie didn't display properly when I tried it just now.
  • out.mp4: Output filename.