FileDocCategorySizeDatePackage
TimeCodeTrackBuilder.javaAPI DocExample6216Sat Dec 03 06:26:16 GMT 2005com.oreilly.qtjnotebook.ch09

TimeCodeTrackBuilder

public class TimeCodeTrackBuilder extends Object

Fields Summary
public static final int
TIMECODE_TRACK_HEIGHT
public static final int
TIMECODE_TRACK_WIDTH
public static final String
SOURCE_NAME
Constructors Summary
Methods Summary
public static TrackaddTimeCodeTrack(Movie movie)

        int timescale = movie.getTimeScale();

        TimeCodeDef tcDef = new TimeCodeDef();
        tcDef.setTimeScale (2997); // ntsc drop-frame
        tcDef.setFrameDuration (100); // 1 frame in 30 fps dropframe
        tcDef.setFramesPerSecond (30);
        tcDef.setFlags (StdQTConstants.tcDropFrame); 
        // other good options: tcNegTimesOK, tc24HoursMax

        // first record at 0 hrs, 0 min, 0 sec, 0 frames
        TimeCodeTime tcTime = new TimeCodeTime (0, 0, 0, 0);

        // create timecode track and media
        Track tcTrack = movie.addTrack (TIMECODE_TRACK_WIDTH,
                                        TIMECODE_TRACK_HEIGHT,
                                        0);
        TimeCodeMedia tcMedia = new TimeCodeMedia (tcTrack, timescale);
        TimeCoder timeCoder = tcMedia.getTimeCodeHandler();

        // turn on timecode display, set colors
        timeCoder.setFlags (timeCoder.getFlags() |
                            StdQTConstants.tcdfShowTimeCode,
                            StdQTConstants.tcdfShowTimeCode);
        TCTextOptions tcTextOptions = timeCoder.getDisplayOptions();
        tcTextOptions.setTXSize (14);
        tcTextOptions.setTXFace (QDConstants.bold);
        tcTextOptions.setForeColor (QDColor.yellow);
        tcTextOptions.setBackColor (QDColor.black);
        timeCoder.setDisplayOptions (tcTextOptions);

        // set up a sample as a 4-byte array in a QTHandle
        int frameNumber = timeCoder.toFrameNumber (tcTime, tcDef);
        int frameNums[] = new int[1];
        // BOOK ERRATA: this is buggy on Windows for timecodes other
        // than 00:00:00;00.  You need to adjust for endianness, as
        // seen in the revised (uncommented) line.
        // frameNums[0] = frameNumber;
        frameNums[0] = EndianOrder.flipNativeToBigEndian32 (frameNumber);
        QTHandle frameNumHandle = new QTHandle (4, false);
        frameNumHandle.copyFromArray (0, frameNums, 0, 1);

        // create a timecode description (the sample to be added)
        TimeCodeDescription tcDesc = new TimeCodeDescription();
        tcDesc.setTimeCodeDef (tcDef);

        // add the sample to the TimeCodeMedia
        tcMedia.beginEdits();
        tcMedia.addSample (frameNumHandle,
                           0,
                           frameNumHandle.getSize(),
                           movie.getDuration(),
                           tcDesc,
                           1,
                           0);
        tcMedia.endEdits();

        // now insert this media into track
        tcTrack.insertMedia (0, // trackStart
                               0, // mediaTime
                               tcMedia.getDuration(), // mediaDuration
                               1); // mediaRate

        // move the time code to the bottom of the movie and
        // set a transparent-background GrahpicsMode
        int x = (movie.getBox().getWidth()/2) - (TIMECODE_TRACK_WIDTH / 2);
        int y = movie.getBox().getHeight() - TIMECODE_TRACK_HEIGHT;
        QDRect moveFrom = new QDRect (0, 0,
                                      TIMECODE_TRACK_WIDTH,
                                      TIMECODE_TRACK_HEIGHT);
        QDRect moveTo = new QDRect (x, y,
                                    TIMECODE_TRACK_WIDTH,
                                    TIMECODE_TRACK_HEIGHT);
        Matrix matrix = new Matrix();
        matrix.rect (moveFrom, moveTo);
        tcTrack.setMatrix (matrix);
        timeCoder.setGraphicsMode (new GraphicsMode (QDConstants.transparent,
                                                     QDColor.black));

        return tcTrack;
    
public static voidmain(java.lang.String[] args)


          
        try {
            QTSessionCheck.check();
            // open a movie
            QTFile file = QTFile.standardGetFilePreview (
                                    QTFile.kStandardQTFileTypes);
            OpenMovieFile omf = OpenMovieFile.asRead(file);
            Movie movie = Movie.fromFile(omf);
            // add a timecode track
            addTimeCodeTrack (movie);

            // create GUI
            Frame f = new Frame ("Movie with TimeCode track");
            MovieController controller = new MovieController(movie);
            Component c = QTFactory.makeQTComponent(controller).asComponent();
            f.add(c);
            f.pack();
            f.setVisible(true);

        } catch (QTException qte) {
            qte.printStackTrace();
        }