Coyote's Guide to IDL Programming

Making MPEG Movies In IDL

QUESTION: How do I create an MPEG movie from within IDL?

ANSWER: There are two answers to this question. There is a method that applies to versions of IDL prior to version IDL 5.1, and another method that applies to versions of IDL since that time. Let's take the newest version of IDL first.

[Editor's Note: In IDL 8.1 a new IDLffVideoWrite object was introduced that makes it extremely easy to write MPEG-4 and AVI files on all platforms supported by IDL. You can see an example of an AVI movie created with this object in the Coyote Plot Gallery.]

In IDL 5.1, RSI introduced a new MPEG object and a set of interface routines for that object that allows you to build an MPEG movie directly from within IDL. The object is quite easy to operate. (You can also build an MPEG movie directly from with XInterAnimate, the IDL-supplied animation tool.)

(Please note that I have had folks running some versions of IDL 5.1 complain that the program below doesn't work in IDL 5.1. Apparently the Filename keyword is not a valid keyword for the MPEG_Open command. It does seem to be valid for the MPEG_Save command instead. I don't have IDL 5.1 running any longer, so I haven't been able to test this. Just be aware that you may have to make this change in the code below.)

The only difficult part about using the MPEG object is how to create a color MPEG movie. (There is an undocumented Color keyword to the MPEG_PUT module, but this doesn't work on my IDL 5.2 beta release, when I am running in 24-bit color.) A simple way to create color MPEG movies is to make 24-bit color images from your 2D data sets.

Here is a short example program that can create gray-scale or color movies of a 3D data set. To make color movies, set the Color keyword and pass in your favorite color table via the Table keyword. (The Standard Gamma II color table will be used by default if you don't specify a color table.)

   PRO Make_MPEG_Movie, data, Color=color, Table=table

      ; Check keywords.

   IF N_Elements(table) EQ 0 THEN table = 5 ; Standard Gamma II.
   color = Keyword_Set(color)

      ; Load a 3D data set if none passed into program.

   IF N_Elements(data) EQ 0 THEN BEGIN

         ; Open the MRI head data set.

      file = Filepath(SubDir=['examples', 'data'], 'head.dat')
      data = BytArr(80, 100, 57)
      OpenR, lun, file, /Get_Lun
      ReadU, lun, data
      Free_Lun, lun

      ; Rebin the data to make it a little bigger.

      data = Rebin(data, 80*3, 100*3, 57)

   ENDIF

      ; Is this a 3D data set?

   IF Size(data, /N_Dimensions) NE 3 THEN BEGIN
      ok = Dialog_Message('Data must have three dimensions.')
      RETURN
   ENDIF

      ; Get the size of the data set.

   s = Size(data, /Dimensions)
   xsize = s[0]
   ysize = s[1]
   frames = s[2]

      ; Open the MPEG object.

   filename = Dialog_Pickfile(/Write, Title='MPEG File Name...', File='test.mpg')
   IF filename EQ '' THEN RETURN

   mpegID = MPEG_Open([xsize, ysize], Filename=filename)

      ; Need a color movie?

   IF color THEN BEGIN

         ; Load a color table.

      LoadCT, 0 > table < 41, /Silent

         ; Create a 24-bit image for viewing. Get color table vectors.

      image24 = BytArr(3, xsize, ysize)
      TVLCT, r, g, b, /Get

      ; Load the frames.

      FOR j=0,frames-1 DO BEGIN
         image24[0,*,*] = r(data[*,*,j])
         image24[1,*,*] = g(data[*,*,j])
         image24[2,*,*] = b(data[*,*,j])
         MPEG_Put, mpegID, Image=image24, Frame=j
      ENDFOR

   ENDIF ELSE BEGIN

      FOR j=0,frames-1 DO MPEG_Put, mpegID, Image=data[*,*,j], Frame=j

   ENDELSE

      ; Save the MPEG sequence. Be patient this will take several seconds.

   MPEG_Save, mpegID

      ; Close the MPEG sequence and file.

   MPEG_Close, mpegID

   END

Making MPEG Movies in Earlier Versions of IDL

This answer comes from some code origially submitted to the IDL newsgroup by Scott Denning, formerly of Colorado State University and now at the University of California at Santa Barabara. It has been modified slightly by Christian Soeller of St. Georges Hopital Medical School in London to better run on most UNIX machines.

The following IDL procedure will produce an MPEG file from a series of images stored in a 3D array (width x height x number of frames). It requires the mpeg_encode executable to be in the UNIX search path. This can be obtained from ftp://mm-ftp.CS.Berkeley.EDU/pub/mpeg/encode/.

There are a lot of options that can be handled differently to make tradeoffs between image quality, speed, and disk space. See the documentation for mpeg_encode for more details.

If your animation is stored in the array image_array and you want to write it to a file called movie.mpg, you would do so by typing:

   WRITE_MPEG, 'movie.mpg', image_array

Here is a copy of the WRITE_MPEG documentation header. To download a local copy, click here. For the latest updated version, please contact Christian Soeller directly.

   ;+
   ; NAME: WRITE_MPEG
   ;
   ; PURPOSE: write a sequence of images as an mpeg movie
   ;
   ; CATEGORY: utility
   ;
   ; CALLING SEQUENCE:
   ;         WRITE_MPEG,'movie.mpg',ims
   ;
   ; INPUTS:
   ;         ims: sequence of images as a 3D array with dimensions [sx, sy, nims]
   ;              where sx = xsize of images
   ;                    sy = ysize of images
   ;                    nims = number of images
   ;
   ; OPTIONAL INPUTS: None
   ;
   ; KEYWORD PARAMETERS:
   ;             delaft:   if set delete temporary array after movie was created
   ;                       you should actually always do it otherwise you get
   ;                       problems with permissions on multiuser machines (since
   ;                       /tmp normally has the sticky bit set)
   ;             rep:      if given means repeat every image 'rep' times
   ;                       (as a workaround to modify replay speed)
   ;
   ; OUTPUTS: None
   ;
   ; OPTIONAL OUTPUTS:
   ;
   ; COMMON BLOCKS:
   ;
   ; SIDE EFFECTS:
   ;          creates some files in TMPDIR which are only removed when
   ;          the DELAFT keyword is used
   ;
   ;
   ; RESTRICTIONS:
   ;          depends on the program mpeg_encode from University of
   ;          California, Berkeley, which must be installed in /usr/local/bin
   ;
   ;
   ; PROCEDURE:
   ;         writes a parameter file based on the dimensions of the image
   ;         array + the sequence of images in ppm format into a
   ;         temporary directory; finally spawns mpeg_encode to build the
   ;         movie
   ;
   ;
   ; EXAMPLE:
   ;
   ;
   ;
   ; MODIFICATION HISTORY:
   ;
   ;       Mon Nov 18 13:13:53 1996, Christian Soeller
   ;       
   ;
   ;		grabbed original from the net and made slight modifications
   ;
   ;-

Google
 
Web Coyote's Guide to IDL Programming