// Created by Michael D. Fischer on 27/07/2006.
// Copyright (c) 2006, Centre for Social Anthropology and Computing,
// University of Kent. All rights reserved.
//
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// Neither the name of the Centre for Social Anthropology and Computing,
// University of Kent nor the names of its contributors may be used
// to endorse or promote products derived from this software without
// specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE
// COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
// BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
// OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import quicktime.qd.*;
import quicktime.*;
import quicktime.io.*;
import quicktime.std.*;
import quicktime.std.movies.*;
import quicktime.std.image.*;
import quicktime.app.view.QTFactory;
import quicktime.app.view.QTComponent;
import quicktime.util.QTPointer;
import quicktime.util.QTHandle;
import quicktime.std.movies.media.*;
import quicktime.std.movies.media.DataRef;
public class MakeMedia {
Movie targetMovie=null;
public Movie getMovie() {
return targetMovie;
}
protected void initMovie( ) throws QTException {
targetMovie = new Movie ();
//targetMovieController =
// new MovieController (targetMovie);
// targetMovieController.enableEditing (true);
//targetPlayer = new QTPlayer (targetMovieController);
//targetMovieCanvas.setClient (targetPlayer, false);
//targetMovieCanvas.clientChanged (320, 256);
//targetMovieCanvas.invalidate();
// validate();
}
public boolean copyFromSourceMovie(Movie sourceMovie) {
try {
if (sourceMovie != null) {
Movie clipMovie = sourceMovie.copySelection();
clipMovie.putOnScrap (0);
}
} catch (QTException qte) {
// showOKDialog ("Error in MakeMedia.copyFromSourceMovie", qte.toString());
return false;
}
return true;
}
public boolean pasteMovie(Movie sourceMovie, String mess, boolean includeMeta) {
try {
if (targetMovie == null)
initMovie();
// Movie clipMovie = Movie.fromScrap(0);
Movie clipMovie = sourceMovie.copySelection();
QTHandle theHandle = new QTHandle();
clipMovie.putIntoHandle(theHandle);
//clipMovie = clipMovie.fromHandle(theHandle);
if (clipMovie == null) {
// showOKDialog ("MakeMedia.pasteMovie: Whoa.", "No movie on scrap");
return false;
} else {
DataRef dr = new DataRef(theHandle);
clipMovie.setDefaultDataRef(dr); // need this to edit media
if (includeMeta) addTextToMovie(clipMovie, mess);
// check selection, paste to end if none
TimeInfo selection =
targetMovie.getSelection();
if ((selection.time == 0) && (selection.duration == 0)) {
targetMovie.setSelection (targetMovie.getDuration(), 0);
}
targetMovie.pasteSelection (clipMovie);
targetMovie.setSelection (
targetMovie.getDuration(), 0);
// targetMovieCanvas.removeClient();
// targetMovieCanvas.setClient (targetPlayer, false);
// move to end of target movie, to show edit
// had an effect
targetMovie.setTimeValue (targetMovie.getDuration());
}
} catch (QTException qte) {
// showOKDialog ("Error in MakeMedia.pasteMovie", qte.toString());
return false;
}
return true;
}
public void saveMovie() {
saveMovie("NoName");
}
public void saveMovie(String name, boolean flat) {
XFile movieFile = new XFile();
if (!movieFile.Choose(XFile.WRITE,name+".mov","Make transcript movie...")) {
return;
}
try {
// don't flatten this movie to specified file
File f = movieFile.aFile;
QTFile qtf = new QTFile (f);
targetMovie.convertToFile (
qtf,
StdQTConstants.kMoviePlayer,
-1,
IOConstants.smSystemScript);
} catch (QTException qte) {
System.out.println("Error"+ qte.toString());
// showOKDialog ("Error", qte.toString());
}
}
public void saveMovie(String name) {
XFile movieFile = new XFile();
if (!movieFile.Choose(XFile.WRITE,name+".mov","Make transcript movie...")) {
return;
}
try {
// flatten this movie to specified file
File f = movieFile.aFile;
QTFile qtf = new QTFile (f);
targetMovie.flatten (
0,
qtf,
StdQTConstants.kMoviePlayer,
IOConstants.smSystemScript,
StdQTConstants.createMovieFileDeleteCurFile,
StdQTConstants.movieInDataForkResID,
qtf.getName());
} catch (QTException qte) {
// showOKDialog ("Error", qte.toString());
}
}
public void addTextToMovie(Movie mv, String message) {
try {
Movie m = mv;
float textTrackHeight = 128;
QDRect movieBounds = m.getNaturalBoundsRect();
float movieWidth = movieBounds.getWidthF();
float movieHeight = movieBounds.getHeightF() + textTrackHeight;
System.out.println("MakeMedia.addTextToMovie "+movieWidth+" "+movieHeight);
if (movieWidth < 480) movieWidth = 480;
if (movieHeight < textTrackHeight + 20) movieHeight = textTrackHeight+20;
Track textTrack = m.addTrack (movieWidth, textTrackHeight, 0 /* no volume */);
Matrix textTrackMatrix = textTrack.getMatrix();
textTrackMatrix.translate (0, movieHeight - textTrackHeight);
textTrack.setMatrix (textTrackMatrix);
textTrack.setEnabled (true);
int movieTimeScale = m.getTimeScale();
TextMedia textMedia = new TextMedia (textTrack, movieTimeScale);
QDRect textBounds = new QDRect (3,5,movieWidth-3, movieHeight-3);
textMedia.beginEdits();
TimeInfo sampleTime = new TimeInfo (0, m.getDuration());
String text = new String (message);
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" ),
14,
1,
QDColor.black,
QDColor.white,
QDConstants.teJustLeft,
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 (qtf);
// m.updateResource (outStream, StdQTConstants.movieInDataForkResID, qtf.getName());
} catch (Exception e) {
e.printStackTrace();
//QTSession.close();
// System.exit(0);
}
}
}