Getting a Pict from a Movie

If you’re working with movies, you’ll probably want to be able to get a pict from some arbitrary time in the movie. You could use this for identifying movies via thumbnail icons, identifying segments on a timeline GUI, etc. This action is so common, and it’s really easy.

How do I do that?

To grab a movie at a certain time, you just need a one-line call to Movie.getPict( ) , as exercised by the dumpToPict( ) method shown here:

Note

Notice I don’t say “grab the current movie frame” because the movie could have other on-screen elements like text, sprites, other movies, etc., not just one frame of one video track.

public void dumpToPict ( ) {
  try {
      float oldRate = movie.getRate( );
      movie.stop( );
      Pict pict = movie.getPict(movie.getTime( ));
      String absPictPath =
          (new File ("movie.pict")).getAbsolutePath( );
      pict.writeToFile (new File (absPictPath));
      movie.setRate (oldRate);
  } catch (Exception e) {
      e.printStackTrace( );
  }
}

This method stops the movie if it’s playing and stores the previous play rate. Then it creates a Pict on the movie’s current time and saves it to a file called movie.pict. Then it restarts the movie.

Note

The downloadable book code exercises this in a demo called PictFromMovie. Run it with ant run-ch05-pictfrommovie.

What about . . .

. . . not stopping the movie? I haven’t had good results with this call unless the movie is stopped. At best, it makes the playback choppy for a few seconds; at worst, it crashes.

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.