The latest iPod Media Player has opened up a new opportunity in the mobile video programming field-but how to get started? This article describes the QuickTime for Java library, which is required when you create video content for the iPod Media Player programmatically.
Apple iPod is one of several media players on the market today. It can play multiple types of media, including audio, video, and images. In the United States and other places, iPod leads the Portable Media Player market. Since its birth, it has sold more than 78% iPod sets, accounting for more than of the entire media player market. Although the success of iPod is obviously driven by the popularity of MP3, and mobile users have been shooting and trading digital images for some time now, the mobile video market is still in progress. The potential benefits of early entry into the mobile video market are obvious, but for many developers, the problem they want to solve is the best way and place to apply this new technology. In this article, I will first recommend some practical (and possibly very popular) mobile video applications, and then demonstrate two programs, you can use the QuickTime for Java API to create video content for the iPod. These two programs allow you to easily add titles to existing video files and convert traditional video files to iPod-compatible formats. At the end of the article, you will be left with some sample code to learn more about video operations using the QuickTime for Java API.
Why use mobile video??
Figure 1. iPod Video Player |
Portable Media players are particularly popular in certain groups of people (I .e. consumers with a certain disposable income between 18 and 38 years old. Because mobile videos are relatively new to portable media players, the first question most developers encounter is: Why do they need to create mobile video content? In addition to the most obvious entertainment requirements of mobile media, there are many practical scenarios: mobile media can promote sales and bring a new category of users to portable media players and content. Consider the following situations:
Real estate agents can create a video to make the video a virtual walk of the available properties of the real estate and some functions described by the recorded dubbing. Then, potential housing buyers may use a portable media player to view desired properties. This time-saving approach becomes very popular among residential search customers who do not have time to find all their desired properties.
Vehicle manufacturers and dealers can use mobile video to publish a video directory of the latest models sold. Potential customers will be more familiar with the features and benefits of their favorite models, rather than having to go to the dealers in person.
College and University officials can provide lectures to students in mobile video format to help students learn.
Manufacturers can provide installation instructions on mobile video formats for automobiles, bookshelves, toys (and so on.
Obviously, this is just a few examples of the commercial driving force for creating mobile video content. The common feature of these examples is to introduce the potential of portable media to the new market. Now let's take a look at the Java API that allows you to create and edit video files programmatically.
QuickTime for Java API
For Java developers, quick time for Java APIs are the most powerful API that supports the creation, modification, and playback of digital media. QuickTime for Java was initially created for the Macintosh platform, but over the past few years, Windows and Mac users can also use it freely. Of course, if you have done some programming in Java, you may ask: Why can't you just use the java media framework (JMF) API.
JMF was initially created to provide Java v1.0.2 developers with the ability to process various media. However, it is not widely used and supports some outdated media formats and encoding methods, such as MPEG-2, AU, hotmedia and flash 2. On the other hand, QuickTime for JAVA supports all media formats and encoding modes that can be played by QuickTime. Use QuickTime for Java APIs to access updated media formats, including MPEG-4, MP3 audio, H.264, and Flash 5. In fact, the following is a complete list of video encoding methods 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
Motion JPEG B
MPEG-4 (Part 2)
Photo JPEG
Planar RGB
PNG
Sorenson video 2
Sorenson video 3
TGA
Tiff
Add a title to a video
If you are using QuickTime for Java APIs for the first time or processing digital media for the first time, adding titles to video clips is a good starting point. Captionadder. Java shown in Listing 1 is a simple program that accepts video files and adds a text string to the video as the title.
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 = 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 = 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 = 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
}
} |
Code
Almost every QuickTime for Java application is the same. All actual actions occur between qtsession. open () and qtsession. Close () statements. When qtsession. open () is called, the QuickTime engine can initialize itself. If you want to call other QuickTime for Java classes without calling qtsession. open () first, you will get a very ugly stack trace thrown to the command line.
One of the core objects used in QuickTime for Java APIs is the QuickTime. STD. Movies. Movie object. If you use quicktime for Java for the first time, you need to realize that there is a slight difference between the QuickTime. STD. Movies. Movie object and the file_name.mov that may exist on the hard disk. Importantly, you can create a Quicktime. STD. Movies. Movie object without the need to create the file_name.mov file on the hard disk.
Quicktime. STD. Movies. Movie objects can be composed of multiple tracks, which are independent media sources (such as audio, video, static images, or text ). In captionadder. in Java (), QuickTime is created. STD. movies. after the track object, I am in textmedia. beginedits () Statement and textmedia. text titles are added between endedits () statements. In this example, I use the QuickTime. STD. Movies. timeinfo object to set the length of time for the title to be displayed in the video and when to start displaying the title. I want the text title to be displayed in the first half of the video, so I set timeinfo to start from 0 and set it to end at half of running through the duration of movie.
Use captionadder. Java
So how to use captionadder. Java? First, it is very simple and straightforward to call captionadder. java. After the QuickTime for Java library is added to the class path, you must provide the name of the file to be edited. For example, you want to add a title to the real estate list video shown in figure 2.
Figure 2. Example of a real estate video without a title |
To add a title to this file, run the captionadder. Java program as shown in Listing 2.
Listing 2. Using captionadder. Java
Java captionadder listing22345.mov
The result is displayed in figure 3.
Figure 3. Real estate video example with titles added |
Convert to iPod format
After using the QuickTime for Java API to create and manipulate video content, the next major obstacle is to convert the content to an iPod-compatible format. The video content is not immediately compatible with the iPod video player, whether you use a camera, recording from an active video source, or creating an animation sequence. IPod is a little picky about the type of media it plays. Table 1 shows the formats and settings of videos supported by the iPod environment.
Table 1. Video settings for IPOD-compatible content
| Video Format |
Data Rate |
Dimensions |
Frame Rate |
| H.264 video |
Up to 768 Kbps |
320x240 pixels |
30 FPS |
| MPEG-4 video |
Maximum 2.5 Mbps |
480x480 pixels |
30 FPS |
What makes the problem more complicated is that for audio tracks in a video, it only supports advanced audio encoding (AAC), so if you want to play a video on the iPod, you should not only convert the traditional video content. Fortunately, movieconverter. Java is a convenient tool for converting traditional video content to iPod-compatible formats. 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 = 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 actionreceivmed (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 = 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 ();
}
}
} |
Use movieconverter. Java
Using movieconverter. Java is very simple. In Figure 4, you can see a screenshot of a virtual academic training video on how to use the internet (starting from Google's homepage ). The actual video file is in AVI format and includes audio tracks of the Teaching recording.
Figure 4. screenshot of an example academic training video |
Using the movieconverter. Java program to convert this video file to an iPod-compatible format is very easy. After starting movieconverter. Java, select the file to be converted. Specify the name of the newly created file, as shown in Figure 5.
Figure 5. movieconverter. Java request the name of the new file |
After the name of the new video file is specified, the application displays a video setting summary screen, as shown in figure 6. On this screen, you can modify the encoding method and compression algorithm, and even apply a video filter to the media.
Figure 6. Summary of video settings |
In Figure 7, you can see the video setting window of movieconverter. In this window, you can select the video encoding mode and provide a preview of the converted video.
Figure 7. Video setting window |
After setting, movieconverter generates a new video file that can be played on the iPod video player. Note that movieconverter. Java is a rich and powerful application. You can use it not only to generate video content that can be played on the iPod media player, but also to convert traditional videos into other video formats supported by QuickTime.
Conclusion
After several years, iPod is likely to lead the Portable Media Player market. Video content has been added to this iPod Media Player, opening up many exciting marketing and business opportunities. In this article, I provided some practical and commercial applications for mobile video content, introduced QuickTime for Java APIs, and demonstrated how to manipulate mobile video files programmatically, and convert it to a format compatible with the video iPod.