Skip to main content

skip to main content

developerWorks  >  Java technology  >

Mobile video: Get started with the QuickTime for Java API

Create iPod video content on the Java platform

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Introductory

Bruce Hopkins (bhopkins@gestalt-llc.com), Technical Architect, Gestalt LLC

07 Feb 2006

The newest generation of iPod media players has opened up a world of opportunity in mobile video programming -- but how do you get started? This article introduces you to the QuickTime for Java™ library, which is required to programmatically create video content for the iPod media player.

The Apple iPod is one of several media players on the market today that is capable of playing different types of media, including audio, video, and photos. In the United States and elsewhere the iPod leads the portable media player market, with over 30 million units sold since its inception, and over 78 percent market share for all media players. While the success of the iPod has clearly been driven by the popularity of MP3s, and cell phone users have been snapping and trading digital photos for some time now, the market for mobile video is still emerging. The potential benefits of getting into the mobile video market early are obvious, but for many developers the question is how and where to best apply this new technology.

In this article, I'll first suggest some practical (and potentially very popular) uses for mobile video, and then present two programs to get you started using the QuickTime for Java API to create video content for the iPod. These programs let you easily add captions to existing video files and convert legacy video files into an iPod-compatible format. At the end of the article, I'll leave you with some example code that you can use to learn more about manipulating videos using the QuickTime for Java API.

Why mobile video?


Figure 1. The iPod video player
The iPod Video Player

Portable media players are hugely popular among a certain demographic, namely consumers between the ages of 18 and 38 years old with a certain amount of disposable income. Because mobile video is a relatively new addition to portable media players, the first question for most developers is, Why create mobile video content? Getting past the obvious entertainment appeal of mobile media, there are many practical use cases in which it could both increase sales and introduce a new class of users to portable media players and content. Consider just these few:

  • Real estate agents could create a video of a virtual walk-through of available properties, with features described by a recorded voice-over. Potential home buyers could then view selected properties using a portable media player. This tremendous time-saver would be popular among home seekers without the time to see every house that matched their needs.
  • Automobile manufacturers and dealers could use mobile video to distribute a current video catalog of vehicles for sale. Potential customers could become more familiar with the features and benefits of selected vehicles without having to be physically at a dealership.
  • College and university officials could provide students with lectures in a mobile video format as a study aid.
  • Manufacturers could include assembly instructions in mobile video format for such items as bikes, bookcases, and toys (to name a few).

Obviously, these are just a few examples of the business drivers for creating mobile video content. Inherent in them all is the potential for introducing portable media to a new market. Now, let's take a look at the Java API that lets you create and edit video files programmatically.



Back to top


The QuickTime for Java API

For Java developers the most capable and versatile API to enable the creation, modification, and playback of digital media is the QuickTime for Java API. QuickTime for Java was originally created for the Macintosh platform, but it's been freely available to both Windows and Mac users for several years. Of course, if you've been programming in the Java language for some time, you might wonder why you can't simply use the Java Media Framework (JMF) API.

The JMF was originally created to give Java V1.0.2 developers the ability to work with various types of media. However, it is not widely used and has support for several outdated media formats and codecs like MPEG-2, AU, HotMedia, and Flash 2. QuickTime for Java, on the other hand, supports all the media formats and codecs that are playable by QuickTime itself. Using the QuickTime for Java API gives you access to more current media formats, including MPEG-4, MP3 audio, H.264, and Flash 5. In fact, here's the complete list of video codecs supported by QuickTime:

  • Animation
  • Apple BMP
  • Apple Pixlet
  • Apple Video
  • Cinepak
  • Component video
  • DV and DVC Pro NTSC
  • DV PAL
  • DVC Pro PAL
  • Graphics
  • H.261
  • H.263
  • H.264
  • JPEG 2000
  • Microsoft® OLE
  • Microsoft Video 1
  • Motion JPEG A
  • Motion JPEG B
  • MPEG-4 (Part 2)
  • Photo JPEG
  • Planar RGB
  • PNG
  • Sorenson Video 2
  • Sorenson Video 3
  • TGA
  • TIFF


Back to top


Adding captions to video

Adding captions to a video clip is a great place to start if you're new to the QuickTime for Java API or simply new to working with digital media altogether. Listing 1 shows CaptionAdder.java, a simple program that takes a video file and adds a text string to the video as a caption.


Listing 1. CaptionAdder.java

import quicktime.*;
import quicktime.io.*;
import quicktime.std.image.*;
import quicktime.std.movies.media.*;
import quicktime.std.movies.*;
import quicktime.std.*;
import quicktime.qd.*;
import quicktime.util.QTPointer;


import java.io.*;

public class CaptionAdder {

 public static void main (String args[]) {

  if (args.length != 1){

    System.out.println("Usage: java CaptionAdder [input_filename]");

  } else {
      
    try {
     QTSession.open();     

     System.out.println ("QuickTime version: " + QTSession.getMajorVersion() + "." + 
        QTSession.getMinorVersion());

     QTFile qtfile = new QTFile(new File(args[0]));
     DataRef urlMovie = new DataRef ("file://" + qtfile.getPath());
     Movie movie = Movie.fromDataRef (urlMovie,StdQTConstants.newMovieActive);
    
     float  textTrackHeight = 32;

     QDRect movieBounds = movie.getNaturalBoundsRect();
     float  movieWidth  = movieBounds.getWidthF();
     float  movieHeight = movieBounds.getHeightF();

     Track  textTrack = movie.addTrack(movieWidth, textTrackHeight, 0);

     Matrix textTrackMatrix = textTrack.getMatrix();
     textTrackMatrix.translate (0, movieHeight - textTrackHeight);
     textTrack.setMatrix (textTrackMatrix);

     textTrack.setEnabled (true);

     int movieTimeScale = movie.getTimeScale();
     TextMedia textMedia = new TextMedia (textTrack, movieTimeScale);

       QDRect textBounds = new QDRect (movieWidth, movieHeight);

     textMedia.beginEdits();

      TimeInfo sampleTime = new TimeInfo (0, movie.getDuration()/2);
    
      String text = new String ("1234 Main St. - Listing price: $164,000");
      TextMediaHandler textMediaHandler = textMedia.getTextHandler();
      QTPointer textPointer = new QTPointer ( text.length() + 1, true );
      textPointer.copyFromArray ( 0, text.getBytes(), 0, text.length() );
      textMediaHandler.addTextSample (
        textPointer,
        QDFont.getFNum("Times"), 
        16, 
        0,
        QDColor.white,
        QDColor.black,
        QDConstants.teCenter,
        textBounds,
        StdQTConstants.dfClipToTextBox | StdQTConstants.dfKeyedText,
        0,0,0,
        null,
        sampleTime.duration );
      
     textMedia.endEdits();

     textTrack.insertMedia (sampleTime.time, 0, sampleTime.duration, 1 );
     OpenMovieFile outStream = OpenMovieFile.asWrite (qtfile); 

     movie.updateResource (outStream, StdQTConstants.movieInDataForkResID, 
        qtfile.getName());
    } catch (Exception e) {
         e.printStackTrace();
             QTSession.close();
             System.exit(0);
    }
        
               QTSession.close();

  // end else
  }

  System.out.println ("complete.");

 //end method
 }
  

}

About the code

As with almost every QuickTime for Java application, all the real action occurs between the QTSession.open() and the QTSession.close() statements. When you make a call to QTSession.open() the QuickTime engine is able to initialize itself. If you attempt to make any calls to any of the QuickTime for Java classes without previously calling QTSession.open() you'll get a very ugly stacktrace thrown to the command line.

One of the core objects used in QuickTime for Java API is the quicktime.std.movies.Movie object. If you're new to using QuickTime for Java, you need to realize that there's a subtle difference between the quicktime.std.movies.Movie object and the file_name.mov that may exist on your hard drive. Importantly, quicktime.std.movies.Movie objects can be created and can exist without needing to create a file_name.mov file on the hard drive.

A quicktime.std.movies.Movie object can be made from several tracks, which are independent sources of media (like audio, video, still photos, or text). In CaptionAdder.java(), after creating a quicktime.std.movies.Track object, I added the text caption in between the textMedia.beginEdits() statement and the textMedia.endEdits() statement. In this example, I've used the quicktime.std.movies.TimeInfo object to set how long I want the caption to appear in the video and when to start displaying the caption. I wanted the text caption to appear in the first half of the video, so I set the TimeInfo to start at 0 and end halfway through the movie's duration.



Back to top


Using CaptionAdder.java

So how do you use CaptionAdder.java? Well, first of all, invoking CaptionAdder.java is pretty simple and straightforward. After you've added the QuickTime for Java libraries to your classpath all you need to do is provide the name of the file that you want to edit. For instance, say you wanted to add a caption to the example real-estate listing video shown in Figure 2.


Figure 2. Snapshot of the real-estate video example without captions
Snapshot of the real-estate video example without captions

To add a caption to this file, you would simply run the CaptionAdder.java program as shown in Listing 2.


Listing 2. Using CaptionAdder.java

java CaptionAdder listing22345.mov 

The results are shown in Figure 3.


Figure 3. The real-estate video example with captions added
Snapshot of a fictional real-estate listing video after running CaptionAdder.java


Back to top


Converting to iPod formats

After you have the basics of creating and manipulating video content using the QuickTime for Java API down, your next major hurdle is converting the content to an iPod-compatible format. Whether you use a video camera, record from a live video source, or create an animation sequence, chances are your video content won't be immediately compatible with the iPod video player. The iPod is somewhat finicky about the types of media it plays. Table 1 shows the video formats and settings supported by the iPod environment.


Table 1. Video settings for iPod-compatible content


Video format

Data rate

Size

Frame rate
H.264 videoUp to 768 Kbps320 x 240 pixels30 fps
MPEG-4 videoUp to 2.5 Mbps480 x 480 pixels30 fps

To make matters more difficult, it only supports Advanced Audio Coding (AAC) for audio tracks within a video, so more than likely you will need to convert legacy video content if you want to play it on the iPod. Fortunately, you can turn to MovieConverter.java, which is a handy tool for converting legacy video content to an iPod-compatible format. MovieConverter.java is shown in Listing 3.


Listing 3. MovieConverter.java


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import java.io.IOException;

import quicktime.*;
import quicktime.io.*;
import quicktime.qd.*;
import quicktime.std.*;
import quicktime.std.clocks.*;
import quicktime.std.StdQTConstants;
import quicktime.std.image.*;
import quicktime.std.movies.*;
import quicktime.std.movies.media.*;
import quicktime.std.qtcomponents.*;
import quicktime.util.*;

import quicktime.app.view.*;

public class MovieConverter extends JFrame implements Errors, ActionListener{
	
  QTComponent component = null;  
  JPanel jpanel = null;
  Button selectButton = null;

  public static void main (String args[]) {
    try{
      new MovieConverter();
    } catch(Exception e){
      System.out.println(e);
    }
  }

  public MovieConverter(){

    super("Movie Converter");

    try { 
      QTSession.open();
    } catch (Exception e) {
      e.printStackTrace();
      QTSession.close();
    }

    jpanel = new JPanel();

    this.setContentPane(jpanel);
    selectButton = new Button ("Select a File to Convert");
    selectButton.addActionListener(this);

    jpanel.add (selectButton);

    addWindowListener(new WindowAdapter () {
      public void windowClosing (WindowEvent e) {
        QTSession.close();
        dispose();
      }

      public void windowClosed (WindowEvent e) { 
        System.exit(0);
      }
    });     

    this.pack();
    this.setVisible(true);

  }

  public void actionPerformed (ActionEvent event) {
    exportMovie();    
  }


  void displayMovie (Movie m) throws QTException {
    component = QTFactory.makeQTComponent (new MovieController (m));
    jpanel.add ((Component)component);
    jpanel.remove(selectButton);
    pack();
  }
    
  void exportMovie () {
    try{
      FileDialog fileDialog = new FileDialog (this, 
          "Choose Movie to Export...", FileDialog.LOAD);
      fileDialog.show();
      if (fileDialog.getFile() == null)
        return;
      QTFile movieFile = new QTFile (fileDialog.getDirectory() 
         + fileDialog.getFile());
      
      Movie movie = Movie.fromFile (OpenMovieFile.asRead(movieFile));
    
      if (component != null) {
        component.setMovieController(new MovieController(movie));
      } else {
        displayMovie (movie);
      }
    
      new Thread (new Exporter(movie)).start();
    
    } catch (QTException err) {
      err.printStackTrace();
    }
  }
  

}



Back to top


Using MovieConverter.java

Using MovieConverter.java is pretty simple. In Figure 4 you see a screenshot of a fictional academic training video on how to use the Internet (starting with the Google homepage).The actual movie file is in AVI format and also contains an audio track of the voice of the lecturer.


Figure 4. Snapshot of the example academic training video
Snapshot of the example academic training video

Converting this video file to an iPod-compatible format is easy with the MovieConverter.java program. After starting MovieConverter.java, you're first prompted to select the file that you want to convert. Then, you're prompted to name the new file to be created, as shown in Figure 5.


Figure 5. MovieConverter.java asks for a new file name
MovieConverter.java asks for a new file name

After you specify a name for the new video file the application displays a movie settings summary screen, as shown in Figure 6. From that screen, you have the options to modify the codecs, compression algorithms, and even apply video filters to your media.


Figure 6. Movie settings summary
Movie settings summary

In Figure 7 you see MovieConverter's Video Settings window, which allows you to choose your video codecs and also provides a preview of the converted video.


Figure 7. Video settings window
MovieConverter.java's video settings window

After you've picked your setting, MovieConverter produces a new video file that can be played on the iPod video player. Also note that MovieConverter.java is a very versatile and powerful application. Not only can it can be used to produce video content that can be played on iPod media players, but it can also be used to convert your legacy video into other video formats supported by QuickTime.



Back to top


In conclusion

It's likely that the iPod will lead the portable media player market for years to come. The addition of video content to the current generation of iPod media players has opened up many exciting marketing and business opportunities. In this article, I offered a quick look at some of the practical and business uses for mobile video content, introduced to you the QuickTime for Java API, and showed you how to programmatically manipulate a mobile video file and convert it to a format compatible with the Video iPod.



Resources

Learn

Get products and technologies

Discuss


About the author

Bruce Hopkins is the author of the book, Bluetooth for Java (Apress) and is the creator of the JB-22 developer kit. He graduated from Wayne State University in Detroit with a B.S. degree in Electrical and Computer Engineering. He currently works as a Technical Architect for Gestalt LLC and focuses on distributed computing, Web services, and wireless technologies. He can be contacted at bhopkins@gestalt-llc.com.




Rate this page


Please take a moment to complete this form to help us better serve you.



YesNoDon't know
 


 


12345
Not
useful
Extremely
useful
 


Back to top