Coyote's Guide to IDL Programming

Scientific Animations in IDL

QUESTION: What is the best way to make a scientific animation in IDL? I would like to run the animation in a Powerpoint presentation or on my web page.

ANSWER: Oh, oh. You've opened up a can of worms with that question. I'll just point out that if you have IDL 8.1 or higher this is much easier than it used to be with the IDLffVideoWrite object. In fact, you can see an example of an AVI movie created with this object in the Coyote Plot Gallery.

Here is an edited sample of the kinds of remarks people on the IDL newsgroup made to a similar question.

   Subject: Re: movie into powerpoint
   From: JD Smith 
   Date: Thu, 03 May 2001 10:57:02 -0400

   I chose the FliC format, which is smaller and much nicer than MPEG, etc,
   for scientific "cartoon" type movies (animated graphs, etc.) -- none of
   the flicker and blotchiness I associated with MPEGs of the same type. 
   Powerpoint cannot play FliC's internally without buying some plug-in for
   WMP, I guess.  I ended up using the free PowerFlic player and just
   linking to it.  To generate the flics, I write ppm's, and then spawn
   ppm2fli from the unix command line.  Make sure to use the -g option to
   set the size of the movie, unless you've written 640x480 frames (the
   default size).

   Subject: Re: movie into powerpoint
   From: Kelly Dean 
   Date: Thu, 03 May 2001 10:41:16 -0600

   The method I use and have always recommend to students and my fellow
   researchers is turn the sequence of images into an AVI file using JASC
   Animation Workshop ( shareware ) and load the Media Player as an object into
   PowerPoint looking at the AVI file. And remember the AVI files most go along
   with the PowerPoint PPT file if you move to another machine.

   Subject: Re: movie into powerpoint
   From: "Rick Towler" 
   Date: Thu, 3 May 2001 10:46:22 -0700

   Is this a can of worms.....

   Select your format depending on your audience, delivery method, and content.
   If you are putting this up for general consumption then requiring the
   download and installation of an external player will turn away many visitors
   (you can decide it that is good or bad).  Will the file be available via the
   internet?  If so, then you really can't throw out a 150MB animation and
   think that people will download it (but this goes back to the audience too
   since most researchers have a decent internet connection).  Lastly, the
   actual images you are seeking to animate will have properties of that lend
   themselves to one compression scheme or another.

   I agree that .flc is a decent format for scientific animations but it has
   its drawbacks.  It is 8 bit, requires an external player, and depending on
   the images it compiles it can yield excellent to poor compression.  A simple
   2d animation of satellite tracked drifter movements intended for viewing by
   collogues is a great candidate for .flc.  Not so great is an object graphics
   fly thru of a school of pollock moving thru an echosounder beam intended for
   distribution to a wide audience.

   MPEG has it's problems as well.  First, there are codecs for MPEG1 thru
   MPEG4.  Windoze Media Player only plays MPEG1 by default.  Plus, the codec
   bundled with it only handles certain aspect ratios.  You can't just pick an
   image size and assume that it will work.  On top of this, the compression
   scheme fares quite poorly with "scientific animations" specifically ones
   with large collections of pixels with the same color.  You can choose a high
   bit rate to alleviate the image quality issue but then you are left with
   huge files.  MPEG1 is handled by most every bundled player, but at lower
   compression rates your animations may look like crap making it impossible to
   deliver small files that look good.  But, for presentations it *MAY* work
   for you.  You have to experiment.

   .avi is yet another option.  We have been working with this format lately
   and have been fairly happy with it.  File sizes are moderate, quality is
   high (with our material) and every standard bundled player plays .avi files
   of reasonable dimensions. There are a number of codecs for .avi which yield
   different results so you may need to experiment to find the right one.  So
   far, .avi has been the most flexible format of the three.

   For actually compiling the frames into an animation I am using the shareware
   program VideoMach (http://www.gromada.com/).  It uses whatever codecs are
   installed on your machine (so they will vary) but on a win2k box I have
   quite a few options (it will do all of the formats mentioned above and
   more).  I will usually load up 75 or 100 frames (.png images) and get my
   frame rate, compression scheme and size worked out then load up the rest and
   process the whole animation.  It takes some time, but you will start to
   figure out what codecs are good at what and other methods to manipulate file
   size vs quality so you can target your audience and method of delivery.

   Whatever format you choose, make sure the animation plays on your machine.
   .avi files created on my win2k box required a codec to play back on an Win
   NT4 machine.  I don't have an NT machine so I don't know if NT lacks any
   .avi codecs or the bundled codec just didn't like the file I was giving it.

   As for powerpoint........

   To keep the flow of a presentation I prefer to embed the animation in the
   presentation instead of going to an external viewer. Since we don't have a
   .flc plugin for WMP we steer clear of .flc.  This leaves MPEG1 and AVI.
   Both work well, but I must warn you to TEST THE ANIMATION ON THE PRESETATION
   MACHINE WHILE IT IS CONNECTED TO THE PROJECTOR! I have seen many
   presentations where the animation plays on the laptop screen but the
   projector shows only a black box where the animation should be playing.  I
   haven't had enough time to figure out why this is, but it hasn't been
   limited to a single laptop or projector.
   

Other people try to use XInterAnimate and its MPEG capability to build animations. There is general agreement that this doesn't work very well. Here is a typical response from the IDL newsgroup:

      Subject: Re: mpeg quality
      From: Haje Korth
      Newsgroups: comp.lang.idl-pvwave

      I quit using the mpeg routines because of the crummy quality. I am pretty
      happy with the idl2avi.dlm from Ronn Kling and the vp3 codec, which can be
      downloaded free of charge. I have been using this solution for a several months 
      and it works great.

   Haje
   

And here is what Rick Towler has to say about making MPEG movies in IDL.

I don't want to complicate things but forget about MPEG. MPEG is most often a lousy format for scientific animations. I would suggest writing all of your frames to disk as individual .png files then using another program to create the animation.

For 8-bit animations, it's tough to beat the FLI/FLC format. Rich Signell has a page with all you need to know to get started with FLI/FLC format at: http://woodshole.er.usgs.gov/operations/modeling/flc.html.

For 24-bit animations it is a bit more complicated as there are a number of encoder/decoders out there. I would be happy to advise further if you want to go this route.

If you just want to stick with MPEG, understand that most MPEG players only support certain frame sizes and bit rates so to ensure compatibility you need to stick to them. For frames sizes, I would stick with D1 (704x480 or 720x480), half D1 (352x480), and quarter D1 (352x240). MPEG-2 will give you better overall quality vs file size but not all media players play MPEG-2 content (for example, Windows Media Player requires a special plug-in). MPEG-1 is widely compatible but you are limited in maximum bit-rate (1856 kbps) and quality vs file size isn't as good as MPEG-2.

What About Movies on Macs?

With more and more users switching to Macs, which are suppose to be terrific at movie making, is there a Mac solution? Not really. Ken Bowman offers this advice.

I'll try to get around to adding this to the MacTips web page. But, here is the short version.

I don't currently have a good programmatic solution from inside IDL, but there are at least two easy ways to do it using external apps.

First, in IDL, write all the frames into a directory in some obvious sequence like this:

   WRITE_PNG, 'frame_' + STRING(i, FORMAT = "(I4.4)") + '.png', TVRD(True=1)

I much prefer PNGs to TIFFs (8- and 24-bit, good lossless compression, no MPEG artifacts, public domain, ...)

Once the frames are created, use GraphicConverter or QuickTime Pro to make the frames into a movie.

GraphicConverter is an outstanding shareware program that will convert between virtually any two graphic formats. Choose Convert & Modify from the File menu. On the left, navigate to the directory containing the frames. Select all the frames that you want in the movie. On the right, navigate to the directory where you want the movie to go. Set the Dest. Format to QuickTime (.mov). Click the Options button and select the PNG compressor. Set the frame rate. Turn off Key frames. Click OK to get back to the Convert & Modify dialog. Click Go. You may need to wait after if finishes reading the files for the QuickTime movie to be created (depends on number and size of frames).

Lastly, pay the shareware fee for GraphicConverter.

You can also do this with Apple's QuickTime, but you have to pay Apple the $20 or whatever to "upgrade" QuickTime to QuickTime Pro.

Further Notes

Here is a note from the old curmudgeon, Mark Hadfield, who knows a thing or two about making scientific animations.

Use AVI with the MS Video 1 codec, at 85% quality. Play it with Gromada's Imagen. It's all you need (on Windows, anyway). (Editor's Note: The MS Video 1 codec comes with all versions of Windows, through Windows XP, but is now considered obsolute. MS Video 9 codec is generally thought to be the equivalent.)

Another user suggests this:

Try writing your frames to disk and then calling ffmpeg. It is very fast, high quality, multi-platform, free, and makes all major movie formats. Finding a Windows excutable is a bit of a pain, but they are out there. This has worked well for me in the past.

A new video creating object, IDLffVideoWrite, introduced in IDL 8.0, can be used to create annimations in either mpg4 or AVI format. You can specify which Codecs you want to use, and so on. There are quite a few features. According to the documentation, the open-source library FFmpeg is used to handle the video and audio compression and file formatting.

IDL uses the open-source library FFmpeg to handle video and audio compression and file formats. Because of patent issues, the version of FFmpeg that comes with IDL includes only a select subset of the codecs and formats that FFmpeg is able to support. However, IDLffVideoWrite can accommodate other versions of FFmpeg if you replace the version installed with IDL.

Here is a short example that demonstrates how IDLffVideoWrite can be used to create an AVI formatted movie. This assumes you have a directory of PNG files to open and save as frames.

   PRO Movie_Example

     Compile_Opt idl2
     
     ; Open the video recorder.
     video_file = 'movie_example.avi'
     video = IDLffVideoWrite(video_file, Format='avi')
     
     ; Configure the video output for the PNG files you
     ; plan to add.
     framerate = 5
     xsize = 750
     ysize = 600
     stream = video.AddVideoStream(xsize, ysize, framerate)
     
     ; Get the files.
     files = File_Search('*.png', COUNT=fileCnt)
     FOR j=0,fileCnt-1 DO BEGIN
        
       ; Read the PNG file.
       image = Read_PNG(files[j])
       
       ; Save the image in the video stream
       void = video.Put(stream, image)
     
     ENDFOR
     
     ; Clean things up.
     video.Cleanup
     
   END

Another suggestion is to use ImageMagick to build an animated GIF or MPEG movie. ImageMagick is normally installed on your machine, especially if you use Coyote Graphics. If not, it is extremely easy to install.

To create a GIF movie, type something like this.

   convert -adjoin -delay 10 *.png animated.gif

To create a MPEG animation, type something like this. ImageMagick also uses the open-source FFmpeg to create the MPEG file.

   convert -adjoin -delay 10  *.png movie.mpg

[Return to IDL Programming Tips]