FileDocCategorySizeDatePackage
TransitionTrackBuilder.javaAPI DocExample11823Wed Nov 10 12:59:32 GMT 2004com.oreilly.qtjnotebook.ch09

TransitionTrackBuilder.java

/*

Copyright (c) 2004, Chris Adamson

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

*/
package com.oreilly.qtjnotebook.ch09;

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

import com.oreilly.qtjnotebook.ch01.QTSessionCheck;

public class TransitionTrackBuilder {

    public static final int EFFECT_TRACK_WIDTH = 320;
    public static final int EFFECT_TRACK_HEIGHT = 240;
    public static final int TIMESCALE = 600;
    public static final int TRANSITION_DURATION_SECONDS = 3;
    public static final int TRANSITION_DURATION =
        TRANSITION_DURATION_SECONDS * TIMESCALE;
 

    public static void main (String[] args) {
        try {
            new TransitionTrackBuilder();
        } catch (QTException qte) {
            qte.printStackTrace();
        }
        System.exit(0);
    }

    public TransitionTrackBuilder() throws QTException {
        QTSessionCheck.check();

        QTFile movFile = new QTFile (new java.io.File("transition.mov"));
        // query user for source movies
        Movie sourceAMovie = queryUserForMovie();
        Movie sourceBMovie = queryUserForMovie();

        /* create *5* video tracks:
           1. all of sourceAMovie up to effect
           2. portion of sourceAMovie used for effect
           3. portion of sourceBMovie used for effect
           4. all of sourceBMovie after effect
           5. effects track
        */
        Movie movie =
            Movie.createMovieFile(movFile,
                                  StdQTConstants.kMoviePlayer,
                                  StdQTConstants.createMovieFileDeleteCurFile |
                                  StdQTConstants.createMovieFileDontCreateResFile);

        Track preEffectTrack = addVideoTrack (sourceAMovie,
                                              movie,
                                              0, 
                                              sourceAMovie.getDuration() - TRANSITION_DURATION,
                                              0);
        Track sourceATrack = addVideoTrack (sourceAMovie,
                                            movie,
                                            sourceAMovie.getDuration() - TRANSITION_DURATION,
                                            TRANSITION_DURATION,
                                            sourceAMovie.getDuration() - TRANSITION_DURATION);

        Track sourceBTrack = addVideoTrack (sourceBMovie,
                                            movie,
                                            0,
                                            TRANSITION_DURATION,
                                            movie.getDuration() - TRANSITION_DURATION);
        Track postEffectTrack = addVideoTrack (sourceBMovie,
                                               movie,
                                               TRANSITION_DURATION,
                                               sourceBMovie.getDuration() - TRANSITION_DURATION,
                                               movie.getDuration());

        Track effectsTrack = movie.addTrack (EFFECT_TRACK_WIDTH,
                                             EFFECT_TRACK_HEIGHT,
                                             0);
        int TIMESCALE = 600;
		VideoMedia effectsMedia = new VideoMedia(effectsTrack,
                                                 TIMESCALE);

        // get list of effects
        // StdQTConstants.elOptionsIncludeNoneInList)
        EffectsList effectsList = new EffectsList (2, 2, 0);

        // probably need to set up input map here
        AtomContainer inputMap = new AtomContainer();

        int trackARef =
            effectsTrack.addReference (sourceATrack,
                                       StdQTConstants.kTrackModifierReference);
        int trackBRef =
            effectsTrack.addReference (sourceBTrack,
                                       StdQTConstants.kTrackModifierReference);

        // add input reference atoms
        Atom aInputAtom = 
            inputMap.insertChild (null,
                                  StdQTConstants.kTrackModifierInput, 
                                  trackARef,
                                  0);
        inputMap.insertChild (aInputAtom,
                              StdQTConstants.kTrackModifierType,
                              1,
                              0,
                              EndianOrder.flipNativeToBigEndian32(StdQTConstants.videoMediaType));
        inputMap.insertChild (aInputAtom,
                              StdQTConstants.kEffectDataSourceType,
                              1,
                              0,
                              EndianOrder.flipNativeToBigEndian32(QTUtils.toOSType ("srcA")));

        Atom bInputAtom = 
            inputMap.insertChild (null,
                                  StdQTConstants.kTrackModifierInput, 
                                  trackBRef,
                                  0);
        inputMap.insertChild (bInputAtom,
                              StdQTConstants.kTrackModifierType,
                              1,
                              0,
                              EndianOrder.flipNativeToBigEndian32(StdQTConstants.videoMediaType));

        inputMap.insertChild (bInputAtom,
                              StdQTConstants.kEffectDataSourceType,
                              1,
                              0,
                              EndianOrder.flipNativeToBigEndian32(QTUtils.toOSType ("srcB")));

        System.out.println ("set up input map atoms");

        // show list of effects
        // flags are in StdQTConstants.pdOptions...
        // (http://developer.apple.com/documentation/QuickTime/REF/refEffects.61.htm)
        Pict[] previewPicts = new Pict[2];
        previewPicts[0] = sourceAMovie.getPosterPict();
        previewPicts[1] = sourceBMovie.getPosterPict();
        AtomContainer effect =
            ParameterDialog.showParameterDialog (effectsList,
                                                 0, // dialogOptions
                                                 null, // parameters
                                                 "Pick an effect", // title
                                                 previewPicts //pictArray
                                                 );
        // find out the effect type by getting the "what" atom,
        // whose data is a FOUR_CHAR_CODE
        Atom what = effect.findChildByIndex_Atom (null, 
                                                   StdQTConstants.kParameterWhatName,
                                                   1);
        int effectType = effect.getAtomData(what).getInt(0);
	effectType = EndianOrder.flipNativeToBigEndian32 (effectType);
        System.out.println ("User chose " + 
                            QTUtils.fromOSType(effectType) +
                            " effect type");

        // make a sample description for the effect description
        ImageDescription imgDesc = ImageDescription.forEffect (effectType);
        imgDesc.setWidth (EFFECT_TRACK_WIDTH);
        imgDesc.setHeight (EFFECT_TRACK_HEIGHT);

        // give the effect description refs to the sources
        effect.insertChild (null,
                            StdQTConstants.kEffectSourceName,
                            1,
                            0,
                            EndianOrder.flipNativeToBigEndian32(QTUtils.toOSType ("srcA")));
        effect.insertChild (null,
                            StdQTConstants.kEffectSourceName,
                            2,
                            0,
                            EndianOrder.flipNativeToBigEndian32(QTUtils.toOSType ("srcB")));


        // add effect to the video media
        effectsMedia.beginEdits();

        effectsMedia.addSample (effect, // QTHandleRef data,
                                0, // int dataOffset,
                                effect.getSize(), // int dataSize,
                                TRANSITION_DURATION, //int durationPerSample,
                                imgDesc, // SampleDescription sampleDesc,
                                1, // int numberOfSamples,
                                0 // int sampleFlags
                                );
        effectsMedia.setInputMap (inputMap);

        effectsMedia.endEdits();

        // now insert this media into track
        effectsTrack.insertMedia (sourceATrack.getDuration() -
                                    TRANSITION_DURATION, // trackStart
                                  0, // mediaTime
                                  TRANSITION_DURATION, // mediaDuration
                                  1); // mediaRate
        System.out.println ("inserted media into effects track");

        effectsTrack.setLayer (-1);

        // save up 
        System.out.println ("Saving...");
        OpenMovieFile omf = OpenMovieFile.asWrite (movFile);
        movie.addResource (omf,
                           StdQTConstants.movieInDataForkResID,
                           movFile.getName());
        System.out.println ("Done");

    }

    public static Movie queryUserForMovie()
        throws QTException {
        QTFile file =
            QTFile.standardGetFilePreview (QTFile.kStandardQTFileTypes);
        OpenMovieFile omf = OpenMovieFile.asRead (file);
        return Movie.fromFile (omf);
    }

    public static Track addVideoTrack (Movie sourceMovie,
                                       Movie targetMovie,
                                       int srcIn,
                                       int srcDuration,
                                       int targetTime)
        throws QTException { 
        // find first video track
        Track videoTrack = 
            sourceMovie.getIndTrackType (1,
                                         StdQTConstants.videoMediaType,
                                         StdQTConstants.movieTrackMediaType);

        if (videoTrack == null)
            throw new QTException ("can't find a video track");
        // add videoTrack to targetMovie
        Track newTrack =
            targetMovie.newTrack (videoTrack.getSize().getWidthF(),
                                  videoTrack.getSize().getHeightF(),
                                  1.0f);
        VideoMedia newMedia = 
            new VideoMedia (newTrack,
                            videoTrack.getMedia().getTimeScale(),
                            new DataRef(new QTHandle()));
        videoTrack.insertSegment (newTrack,
                                  srcIn, // 0
                                  srcDuration, // videoTrack.getDuration()
                                  targetTime);
        return newTrack;
    }

}