X
10713 Rate this article:
4.0

Guest Request - How to create an animated plot video

Anonym

Very recently I had the opportunity to go for a hike here in Boulder with a friend from my college years.  He's now a successful scientist, using IDL since about 1985.  I sense causality.

Of course, since we're both science and software geeks, the topic of conversation rapidly devolved from family and the rejuvenating power of nature, to general health topics for 40-somethings, and coding quandaries.

He had a practical question for me.  "Let's say I want to animate a plot and programmatically generate a video for a presentation.  Is this easy to do in IDL these days?"

He'd not studied the What's New sections of the IDL documentation for a couple releases.  Science takes precedence, and his impressions were based on the now-obsolete MPEG_WRITE functionality.

The answer of course is that IDL now makes this task very simple, indeed. Here is a quick example, animating a simple sine wave plot.

(Recall that as of 8.3, you can copy and paste lines including the IDL prompt from this web page to the Workbench IDL Console, and the "IDL>" string will be stripped automatically.)

First, generate your wave form using the PLOT function.

IDL> x = findgen(361)*!dtor
IDL> y = sin(x)
IDL> p = plot(x, y)

For this exercise, we'll generate an output MPEG-4 file in your temporary directory.

IDL> file = filepath('test.mp4', /tmp)

We'll use the Plot::CopyWindow method to scrape the bitmap from the plot graphic.  This is returned in exactly the shape the WRITE_VIDEO procedure requires as input.

If you are generating output using Direct Graphics, the equivalent action is to call the function TVRD(TRUE=1) .  And if you are using Object Graphics, get the IMAGE_DATA property from your destination object, such as an IDLgrWindow.

IDL> write_video, file, p.copywindow(/antialias), handle = handle, $
IDL>        format = 'mp4', video_fps = 30

We've now written the first frame of our video.

Here, we've specified the format explicitly as MPEG-4 and the playback rate for the video as 30 frames per second.  On the first call to WRITE_VIDEO the HANDLE keyword is an output.  We will use it as input when writing the remainder of our frames.

Let's loop over 360 degrees by a single degree and update the plot at each step.  Our animation will produce about 12 seconds of video in playback.  (361 frames/30 frames per second.)

IDL> for i = 0, 359 do begin & $
IDL>    y = shift(y, 1) & $
IDL>    p.setdata, x, y & $
IDL>    write_video, file, p.copywindow(/antialias), handle = handle & $
IDL> endfor

Once we have written all our frames, explicitly close the video file to commit the changes.

IDL> write_video, /close, handle = handle

If you're running an OS that understands file associations, you should be able to simply SPAWN the file from IDL to verify its playback.

IDL> spawn, file, /hide

That was indeed easy.

If you want to "slow down" your animation at a critical point for highlighting, simply write a single image buffer multiple times.  For example, you might add this code snippet to slow down the animation during playback's central frames:

    if (i gt 90 && i lt 270) then begin
        for j = 0, 3 do begin
            write_video, file, p.copywindow(/antialias), handle = handle
        endfor
    endif

The WRITE_VIDEO procedure is a wrapper for the IDLffVideoWrite class which in turn wraps the popular open-source FFmpeg library.  WRITE_VIDEO provides options for writing an audio stream to your video as well.

In the online help, see the note on "Replacing the FFmpeg Version" in the Creating Video topic if you need to write your animations in a format whose support is outside the defaults in IDL, such as H.264 video format or MP3 audio.