Exporting Movies

One of the most obviously useful components is the MovieExporter, which you can use to convert a QuickTime movie into a non-QuickTime format, such as AVI or MPEG-4.

How do I do that?

The quicktime.std.qtcomponents.MovieExporter class provides a convenient Java wrapper around movie exporter components. It requires that you pass it a subtype indicating which exporter you want—i.e., which format you want to export to. Example 4-2 shows how a MovieExporter can be created and used from a canned list of subtypes.

Note

Compile and run this example with ant run-ch04-simplemovieexport.

Example 4-2. Simple MovieExporter creation and use

package com.oreilly.qtjnotebook.ch04;
 
import quicktime.*;
import quicktime.std.*;
import quicktime.std.movies.*;
import quicktime.io.*;
import quicktime.std.qtcomponents.*;
import quicktime.utils.QTUtils;
 
import java.awt.*;
import javax.swing.*;
 
import com.oreilly.qtjnotebook.ch01.QTSessionCheck;
 
public class SimpleMovieExport extends Object {
 
  public static final void main (String[  ] args) {
      new SimpleMovieExport( );
  }
 
  public SimpleMovieExport( ) {
      // build choices
      ExportChoice[  ] choices = new ExportChoice[3];
      choices[0] =
          new ExportChoice ("QuickTime Movie",
                            StdQTConstants.kQTFileTypeMovie);
      choices[1] =
          new ExportChoice ("AVI file",
                            StdQTConstants.kQTFileTypeAVI);
      choices[2] =
          new ExportChoice ("MPEG-4 file", 
                            QTUtils.toOSType("mpg4"));
 
      try {
          // query user for a movie to open
          QTSessionCheck.check( );
          QTFile file =
              QTFile.standardGetFilePreview (QTFile.kStandardQTFileTypes);
          OpenMovieFile omFile = OpenMovieFile.asRead (file);
          Movie movie = Movie.fromFile (omFile);
 
          // offer a choice of movie exporters
          JComboBox exportCombo = new JComboBox (choices);
          JOptionPane.showMessageDialog (null,
                                         exportCombo,
                                         "Choose exporter",
                                         JOptionPane.PLAIN_MESSAGE);
          ExportChoice choice = 
              (ExportChoice) exportCombo.getSelectedItem( );
 
          // create an exporter
          MovieExporter exporter =
              new MovieExporter (choice.subtype);
 
          QTFile saveFile = 
              new QTFile (new java.io.File("Untitled"));
 
          // do the export
          movie.setProgressProc( );
          movie.convertToFile (null,
                               saveFile,
                               StdQTConstants.kQTFileTypeMovie,
                               StdQTConstants.kMoviePlayer,
                               IOConstants.smSystemScript,
                               StdQTConstants.showUserSettingsDialog |
                               StdQTConstants.movieToFileOnlyExport |
                               StdQTConstants.movieFileSpecValid,
                               exporter);
 
          // need to explicitly quit (since awt is running)
          System.exit(0);
      } catch (QTException qte) {
          qte.printStackTrace( );
      }
      
  }
 
  public class ExportChoice {
      String name;
      int subtype;
      public ExportChoice (String n, int st) {
          name = n;
          subtype = st;
      }
      public String toString( ) {
          return name;
      }
  }
}

When run, this program prompts the user to open a movie file. Once the movie loads, the program offers a dialog with a choice of formats to export to, as shown in Figure 4-2.

Choice dialog with canned MovieExporter types

Figure 4-2. Choice dialog with canned MovieExporter types

Next, it shows the user a save dialog detailing the proposed export (e.g., “Movie to MPEG-4”) and an Options button. The button brings up a dialog specific to the export format. For example, the AVI export dialog is fairly simple, offering only a few settings to choose from. On the other hand, the MPEG-4 export dialog, seen in Figure 4-3, is extraordinarily busy, packed with descriptions of the many options to help end users understand their choices and potentially keep their exported file compliant with MPEG-4 standards.

MPEG-4 export dialog

Figure 4-3. MPEG-4 export dialog

After the user makes his choices and clicks OK, the long export process begins. Because movie export is very computationally intensive—potentially every frame of video and every audio sample must be re-encoded—a progress dialog appears, so the user can see how much of the export has completed and how much longer it will take.

What just happened?

This program uses an inner class called ExportType to wrap a subtype, int, and a String, largely for the purpose of simplifying the JComboBox used in the format-choice dialog. These subtypes come from constants defined in the StdQTConstants class.

Once a choice is made, the program instantiates a MovieExporter by passing the subtype to its constructor. Next, it requests a progress dialog by calling setProgressProc() on the movie.

Finally, the export is performed by calling convertToFile() and passing in the exporter. This method takes several parameters:

  • A Track to indicate that only this track should be exported, or null for all tracks.

  • A QTFile to export to.

  • A file type, such as StdQTConstants.kQTFileTypeMovie.

  • A creator, such as StdQTConstants.kMoviePlayer.

  • A script tag, typically IOConstants.smSystemScript.

  • Behavior flags. This example uses all three of the valid values: showUserSettingsDialog makes the export bring up the Save As dialog that includes the filename and the options button; movieToFileOnlyExport limits the export choices to formats supported by the exporter component; and movieFileSpecValid asserts that the QTFile is valid and should be used as the default name in the dialog.

Note

Including showUserSettingsDialog allows you to pick up the settings and the save-as GUIs in one call, instead of having to show separate dialogs for each. Too bad flags like this aren’t described in Javadoc.

  • The MovieExporter to use for the export.

What about...

...using the MovieExporter itself to do the export? That’s an alternative. The exporter’s toFile() exports the movie to a file, and its toHandle( ) exports to memory. This also has the advantage of being able to export just part of a movie, as specified by the startTime and duration arguments. Note that doing this requires a different program flow, because first you’d need to get a valid QTFile (perhaps with an AWT file dialog) and then you’d need to call the exporter’s doUserDialog( ) to configure the export. Also the Movie class’s convertToFile( ) method can be more convenient, because, as seen here, it allows use of the default progress dialog. When using the MovieExporter methods, there’s no access to the default dialog. In that case, the only alternative is to provide a custom progress dialog and handle progress callbacks with setProgressProc( ).

Also, a complaint: I tried exporting to MPEG-4 on Windows and didn’t get any audio options. When I click the Audio Track menu in the Exporter dialog, I get the useless panel as shown in Figure 4-4.

Audio non-options for MPEG-4 export on Windows

Figure 4-4. Audio non-options for MPEG-4 export on Windows

This is not a technical issue but a legal one. Apple has licensed MPEG-4 audio encoding for its Mac-based QuickTime users, but not for Windows users. The codecs exist, but apparently you have to contact Dolby about license terms to enable them for Windows.

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.