Class MovieEncoder

java.lang.Object
sim.util.media.MovieEncoder
All Implemented Interfaces:
Serializable, javax.media.ControllerListener, javax.media.datasink.DataSinkListener

public class MovieEncoder extends Object implements javax.media.datasink.DataSinkListener, javax.media.ControllerListener, Serializable

Usage of this class depends on the existence of the Java Media Framework (JMF) which can be acquired from javasoft.com. The class was tested on JMF 2.1.1.

This class encodes BufferedImages into quicktime movies. It has three main functions:

First, you call getEncodingFormats(), which returns an array of available formats. You specify a frame rate and a prototypical image.

Then, you call the constructor -- specify the frame rate, file, an encoding format chosen from the array in getEncodingFormats, and again provide a prototypical image. You'll want to ensure that future images encoded into the MovieEncoder will have the same size and format as the prototype image. If not, they'll be converted and cropped accordingly, which will take more time. You should expect the constructor to take a few seconds to start up -- JMF is not speedy.

Then you drop images into the MovieEncoder to add to the Quicktime movie as frames, using the add(BufferedImage) method. If there was an error and the movie could not be written, this method (and all subsequent add(...) calls) will return false.

When you're all finished, you call the stop() method to clean up and complete writing the file.

Note: Sun's JMF spawns threads in the background which it never cleans up. Thus if you use this class, you'll need to call System.exit(0) to quit your program rather than just dropping out of main().

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    MovieEncoder(float frameRate, File file, BufferedImage typicalImage, javax.media.Format encodeFormat)
    Creates an object which will write out a move of the specified format, and written to the provided file.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Adds an image to the movie.
    void
    controllerUpdate(javax.media.ControllerEvent evt)
    Controller Listener.
    void
    dataSinkUpdate(javax.media.datasink.DataSinkEvent evt)
    Event handler for the file writer.
    static javax.media.Format[]
    getEncodingFormats(float fps, BufferedImage typicalImage)
    Returns null and prints an error out to stderr if an error occurred while trying to get the formats
    boolean
    Stops the writer and finishes uprocessor.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MovieEncoder

      public MovieEncoder(float frameRate, File file, BufferedImage typicalImage, javax.media.Format encodeFormat)
      Creates an object which will write out a move of the specified format, and written to the provided file.
  • Method Details

    • getEncodingFormats

      public static javax.media.Format[] getEncodingFormats(float fps, BufferedImage typicalImage)
      Returns null and prints an error out to stderr if an error occurred while trying to get the formats
    • controllerUpdate

      public void controllerUpdate(javax.media.ControllerEvent evt)
      Controller Listener.
      Specified by:
      controllerUpdate in interface javax.media.ControllerListener
    • dataSinkUpdate

      public void dataSinkUpdate(javax.media.datasink.DataSinkEvent evt)
      Event handler for the file writer.
      Specified by:
      dataSinkUpdate in interface javax.media.datasink.DataSinkListener
    • add

      public boolean add(BufferedImage i)
      Adds an image to the movie. The very first image added will specify the size of the movie's frames and the image type for later images. You should strive to keep the images the same size, else MovieEncoder must will conver and crop them to the original image's format and size. Returns true if the image was successfully added. Returns false if we've been stopped (either intentionally or due to an error).
    • stop

      public boolean stop()
      Stops the writer and finishes uprocessor. After this, this object cannot be used. It is possible that the system may take a second to write out and finish up