Skip to main content

If you don't have an IBM ID and password, register here.

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

Mobile video: Get started with the QuickTime for Java API

Create iPod video content on the Java platform

Bruce Hopkins (bhopkins@gestalt-llc.com), Technical Architect, Gestalt LLC
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.

Summary:  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.

View more content in this series

Date:  07 Feb 2006
Level:  Introductory

Comments:  

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.


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

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.


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

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 formatData rateSizeFrame 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();
    }
  }
  

}


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.


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

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.

Report abuse help

Report abuse

Thank you. This entry has been flagged for moderator attention.


Report abuse help

Report abuse

Report abuse submission failed. Please try again later.


developerWorks: Sign in

If you don't have an IBM ID and password, register here.


Forgot your IBM ID?


Forgot your password?
Change your password


By clicking Submit, you agree to the developerWorks terms of use.

 


The first time you sign into developerWorks, a profile is created for you. This profile includes the first name, last name, and display name you identified when you registered with developerWorks. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

Choose your display name

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerWorks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

(Must be between 3 – 31 characters.)


By clicking Submit, you agree to the developerWorks terms of use.

 


Rate this article

Comments

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Java technology
ArticleID=103285
ArticleTitle=Mobile video: Get started with the QuickTime for Java API
publish-date=02072006
author1-email=bhopkins@gestalt-llc.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

For articles in technology zones (such as Java technology, Linux, Open source, XML), Popular tags shows the top tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), Popular tags shows the top tags for just that product zone.

For articles in technology zones (such as Java technology, Linux, Open source, XML), My tags shows your tags for all technology zones. For articles in product zones (such as Info Mgmt, Rational, WebSphere), My tags shows your tags for just that product zone.

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).