Animating PNG files

I have struggled with optimizing the process of animating PNG files. I have code that generates a PNG image based on an array of data, which, in my case is the intensity of an optical beam, but it can be anything. I have found various options ranging from quick-and-dirty to fairly robust. MEncoder plays a role in several of them, and it was no easy feat to find a combination of flags that properly encode the video for playback on linux and Mac platforms. Here, I present my two most promising options so far.

To clarify the usage, the commands quoted are what I used on a linux machine, which where I run C++ code to generate the images. I want to view movies on both linux and mac platforms (especially since I give presentations on my mac). The viewing is done with either mplayer (linux) or quicktime player (mac).


As part of the ImageMagick package, the animate command works fairly well. My images all have a common filename chunk so I use:

animate *_frame.png

This shows an animation of the frames, slowly first as they load into memory, and then at a framerate that can be set on the command line. You can also slow it down with the “>” key, and speed it up with the “<” key. Those seem backwards but YMMV.


My files are generated from the pngwriter libraries and they don’t seem to play nice with MEncoder for some reason, although I’m still trying to track that reason down. In any case, ImageMagick comes to the rescue:

mogrify -format jpg -quality 90 *.png

The resulting JPG files can then be assembled into a DivX movie using:

mencoder mf://*.jpg -mf fps=25:type=jpg -ovc lavc -lavcopts vcodec=mpeg4 -oac copy -o output.avi -ffourcc DX50

I initially tried the same command but with *.png and type=png, as claimed in the MEncoder manual, but to no avail. All of the other codecs available to me (on a departmental machine) also either segfaulted or returned empty movies with the PNG files. Converting en-masse to jpeg is a hack, but it works. The -ffourcc DX50 flag came to me from the gentoo-wiki Mencoder HOWTO. This flag simply sets the video type to something that the player is likely to understand.

Of course your mileage may vary, but this series of commands works for me to animate PNG files and put them into a compressed video. Note, I have installed the DivX codecs on my mac laptop by installing the open-source Perian package. Having a DivX codec is a requirement to play these files. I am searching for a clear howto on creating mac-friendly videos, but right now I’m just trying to understand the codecs that ship with the mac. Historically there isn’t a lot of AVI support built in to QuickTime player so that is a sticking point for most of the possible video formats.


16 thoughts on “Animating PNG files

  1. “You can also slow it down with the “>” key, and speed it up with the “<” key. Those seem backwards”

    I also would have chosen it the other way around. My guess is that whoever made that choice views those symbols like the crescendo and decrescendo symbols in music.

    By the way, I always used ffmpeg to generate movies from a stack of pngs. (ffmpeg is free and still installed in the /xtmp space on rossler if you wanted to give it a whirl.) I don’t know that it will do anything for you that MEncoder doesn’t, I always found it had enough range of options that suited making a good looking movie.

  2. Update: drop mencoder and go with ffmpeg!

    ffmpeg works like a charm, and I’d have to say that the command-line options for ffmpeg leave mencoder in the dust. For example, I can now use

    ffmpeg -i %03d_frame.png frames.mp4

    to take the sequence of files like 000_frame.png and make the mpeg frames.mp4. There are the advanced flags for users that need them, but it the spirit of making easy things easy and hard things possible, ffmpeg is a winner.

    • I’d love to do what you’re doing and can sometimes get it to work – though maybe I was hallucinating because mostly it doesn’t.

      I make png plots with gnuplot and then I run ffmpeg on them but I get an error about truncated files.

      Here’s the format from gnuplot. I’ve tried variations here too with explicit sizes etc. None work. This is the simplest.
      set terminal png

      Here’s the command line and response. I’ve tried several other variations (-r and -b flags).
      ffmpeg -i Plots/ALL_DATA_plot%06d.png ALL_DATAmovie.mp4
      FFmpeg version 0.5, Copyright (c) 2000-2009 Fabrice Bellard, et al.
      configuration: –prefix=/opt/local –disable-vhook –enable-gpl –enable-postproc –enable-swscale –enable-avfilter –enable-avfilter-lavf –enable-libmp3lame –enable-libvorbis –enable-libtheora –enable-libdirac –enable-libschroedinger –enable-libfaac –enable-libfaad –enable-libxvid –enable-libx264 –mandir=/opt/local/share/man –enable-shared –enable-pthreads –cc=gcc-4.0
      libavutil 49.15. 0 / 49.15. 0
      libavcodec 52.20. 0 / 52.20. 0
      libavformat 52.31. 0 / 52.31. 0
      libavdevice 52. 1. 0 / 52. 1. 0
      libavfilter 1. 4. 0 / 1. 4. 0
      libswscale 1. 7. 1 / 1. 7. 1
      libpostproc 51. 2. 0 / 51. 2. 0
      built on May 12 2009 13:15:32, gcc: 4.0.1 (Apple Inc. build 5490)
      Plots/ALL_DATA_plot%06d.png: I/O error occurred
      Usually that means that input file is truncated and/or corrupted.

      Is there some size limit or size ratio limit on ffmpeg. I have googled for this error and found nothing helpful. I’m hoping since you can get it to work, that you might see what Im doing wrong.


      • Ricardo, I have been in contact with Lenore and the source of the error she mentions here was a file sequence that did not begin with a file numbered 1. You are correct, the %03d placeholder is a 3-digit zero-padded number, and it isn’t obvious, but this number has to start with 001. In the case of %06d, it will look for a file with 000001 in this place. If your first image is in a file called ALL_DATA_plot000010.png, it will give an error.

  3. I found your blog while trying to make mencoder work for me, and I’ve finally found a solution which doesn’t involve converting to JPG (and therefore losing some quality): mencoder requires images with 8-bit colour (8 bits per channel), while some program (like mogrify) output in 16-bit colour.

    The reason converting to JPEG works is that mogrify doesn’t support 16-bit-per-channel JPEGs (maybe the JPEG standard doesn’t), and so converts to 8-bit.

    I only found this out because in desperation I tried converting the images to SGI format, which didn’t work, but did make mencoder give a meaningful message about colour depth.

    Try “mogrify -depth 8 *.png”, or set the program which made the PNGs in the first place to 8 bits per colour channel.

    I know this is an old post, but if I found it on Google, other people with the same problem probably will.

    • Ben, this is very useful information, thanks for posting. My original use for mencoder persists today and I have continued to use a png -> jpeg step in order to get movies out of my c++ data. I think the same issue may exit for ffmpeg because I have not been successful getting it to convert png files to video either.

      The png files are being generated by PNGwriter (a c++ library). Perhaps there is a way to force 8-bit images in that library. That way I could avoid the intermediate “mogrify” as well.

  4. My use was for animating gnuplot output, and since the PNG output of gnuplot won’t antialias on my system, I was using EPS output and ‘convert’ from Imagemagick, so for me, it was just a matter of adding -depth 8 to the convert command line.

    Although an extra ‘mogrify’ does use some processor time, at least you aren’t loosing any information (apart from colour depth, which has to be lost to make the movie).

  5. Pingback: Animating PNG Files part II « The Daily Photon

  6. adawes: wow. ffmpeg is really nice. Thic C-like %d syntax saves so much time. I’ve always used bash for perpending 0’s, as mencoder use lexicographical order. I still have some troubles with compiling it on Snow Leopard, but on Lion it works perfect with MacPorts.

  7. adawes, Thanks a lot for the detailed description about ffmpeg and leading me to the sites about Xcode and MacPorts. I was able to run ffmpeg on a large number of .png files without getting an error. However, when I run the generated .mp4 file in quicktime player I get a black screen. Have you ever encountered this problem? The .png files were produced in matlab and look fine as individuals. Thanks!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s