//
// MovieSelection.java
// VideoGrok
// 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 quicktime.std.clocks.TimeRecord;
import quicktime.std.movies.MovieController;
//import quicktime.app.players.QTPlayer;
import quicktime.std.movies.Movie;
import java.util.Hashtable;
public class MovieSelection implements ToXML, FromXML {
TimeRecord selection=null;
TimeRecord duration = null;
MovieController mc = null;
String movieURL=null;
public static final String Eol = System.getProperty("line.separator");
public String toXML() {
try {
return XFile.Eol+"<MovieSelection movie=\""+movieURL+"\" selectionTime=\""+getSelectionStart()+"\" duration=\""+getSelectionDuration()+"\" scale=\""+selection.getScale()+"\"/>";
} catch (Exception e) {
return "<MovieSelection movie=\""+"Default"+"\" selectionTime=\""+getSelectionStart()+"\" duration=\""+getSelectionDuration()+"\" scale=\""+selection.getScale()+"\"/>";
}
}
public void fromXMLStart(FromXMLHandler parser, String tag, Hashtable attributes) {
if (tag.equals("MovieSelection")) {
try {
movieURL = (String) attributes.get("movie");
int scale = Integer.parseInt((String) attributes.get("scale"));
long selectionTime = Long.parseLong((String) attributes.get("selectionTime"));
long dur = Long.parseLong((String) attributes.get("duration"));
selection = new TimeRecord(scale,selectionTime);
duration = new TimeRecord(scale,dur);
} catch (Exception e) {
System.out.println("Error reading MovieSelection arguments");
e.printStackTrace();
}
}
}
public void fromXMLProcess(String tag, Object content) {
}
public void fromXMLEnd(FromXMLHandler parser, String tag, String content) {
parser.processResult(tag,this);
}
public MovieSelection() {
}
public MovieSelection(MediaPresenter mv, TimeRecord st, TimeRecord dur) {
if (mv != null && mv.getMovieController() != null) {
mc = mv.getMovieController();
try {
movieURL = mv.getCurrentURL();
} catch (Exception e) {
movieURL = mv.getCurrentURL();
}
} else mc = null;
selection = st;
duration = dur;
}
// set up a selection from a movie url .... creates interface to play if does not already exist.
public MovieSelection(String url, TimeRecord st, TimeRecord dur) {
movieURL = url;
/* if (mv != null && mv.getDrawable() instanceof QTPlayer) {
mc = ((QTPlayer) mv.getDrawable()).getMovieController();
movie = mc.getMovie();
} else mc = null;
*/
selection = st;
duration = dur;
}
public void setMovieSelection() {
MovieController mc = null;
if (mc == null) {
// fetch or create a moviecontroller for the selected movie
MediaPresenter presenter = VideoGrok.getPresenter();
try {
presenter.openFromURL(movieURL,getSelectionStart());
mc = presenter.getMovieController();
} catch (Exception e) {
e.printStackTrace();
}
}
if (mc == null) {
System.out.println("Can't create a moviecontroller for "+movieURL+" in MovieSelection.playMovieSelection()");
}
try {
TimeRecord ts = mc.getSelectionBegin();
TimeRecord td = mc.getSelectionDuration();
ts.setValue(selection.getValue());
ts.setScale(selection.getScale());
td.setValue(duration.getValue());
td.setScale(duration.getScale());
mc.setSelectionBegin(ts);
mc.setSelectionDuration(td);
System.out.println("Duration in setMovieSelection: "+td+duration+(td.getValue()-winbase));
} catch (Exception e) {
System.out.println("Could not set selection "+selection.toString()+" "+duration.toString()+" in MovieSelection.setMovieSelection()");
}
}
public void playMovieSelection() {
MovieController mc = null;
if (mc == null) {
// fetch or create a moviecontroller for the selected movie
MediaPresenter presenter = VideoGrok.getPresenter();
try {
presenter.openFromURL(movieURL,getSelectionStart());
mc = presenter.getMovieController();
} catch (Exception e) {
e.printStackTrace();
}
}
if (mc == null) {
System.out.println("Can't create a moviecontroller for "+movieURL+" in MovieSelection.playMovieSelection()");
}
setMovieSelection();
try {
mc.setPlaySelection(true);
mc.goToTime(selection);
mc.play((float) 1.0);
} catch (Exception e) {
System.out.println("Could not play selection "+selection.toString()+" "+duration.toString()+" in MovieSelection.playMovieSelection()");
}
}
public boolean addSelectionToMovie(String mess, MakeMedia mov, boolean includeMeta) {
Movie mc = null;
mc = setSelection(); // sets the selection in the movie
if (mc == null) {
System.out.println("Can't create a movie for "+movieURL+" in MovieSelection.addSelectionToMovie()");
return false;
}
if (mov.copyFromSourceMovie(mc)) {
/*
int selseconds = (int) (selection.getValue()/selection.getScale());
int selframes = (int) (selection.getScale()/25);
int selticks = (int) selection.getValue() - selseconds * (int) selection.getScale();
int selframeremain = selticks / selframes;
int selhours = selseconds / 3600;
int selminutes = (selseconds - selhours * 3600) / 60;
int selsec = selseconds - (selhours*3600) - (selminutes * 60);
String starttime = new String(""+selhours+":"+selminutes+":"+selsec+"."+selframeremain);
int dursec = (int) ((duration.getValue()+selection.getValue())/duration.getScale());
int durframes = (int) (duration.getScale()/25);
int durticks = (int) (duration.getValue()+selection.getValue()) - dursec * (int) duration.getScale();
int durframeremain = durticks / durframes;
int durhours = dursec / 3600;
int durminutes = (dursec - durhours * 3600) / 60;
int durseconds = dursec - (durhours*3600) - (durminutes * 60);
String endtime = new String(" - "+durhours+":"+durminutes+":"+durseconds+"."+durframeremain);
*/
String [] vv = convertTime((int) selection.getValue(), (int) duration.getValue(), (int) selection.getScale());
String starttime = vv[0];
String endtime = vv[1];
if (mov.pasteMovie(mc, mess+Eol+starttime+endtime+"-"+movieURL, includeMeta)) { // paste this clip into the report movie
System.out.println("Copied selection from "+movieURL+" in MovieSelection.addSelectionToMovie()");
}
} else {
System.out.println("Can't copy selection from "+movieURL+" in MovieSelection.addSelectionToMovie()");
return false;
}
return true;
}
public Movie setSelection() {
Movie mc = null;
if (mc == null) {
// fetch or create a moviecontroller for the selected movie
MediaPresenter presenter = VideoGrok.getPresenter();
try {
mc = presenter.getMovieFromURL(movieURL);
} catch (Exception e) {
e.printStackTrace();
mc = null;
}
}
if (mc == null) {
System.out.println("Can't create a movie reference for "+movieURL+" in MovieSelection.setSelection()");
return mc;
}
try {
int sel = (int) selection.getValue();
int dur = (int) duration.getValue();
if (dur == 0) dur = mc.getDuration() - sel;
mc.setSelection(sel, dur);
} catch (Exception e) {
System.out.println("Could not set selection "+selection.toString()+" "+duration.toString()+" in MovieSelection.setSelection()");
}
return mc;
}
public long getSelectionStart() {
long t = selection.getValue();
if (t >= winbase) t -= winbase;
System.out.println(t);
return t;
}
public long getSelectionDuration() {
long t = duration.getValue();
if (t >= winbase) t -= winbase;
System.out.println(t);
return t;
}
public long getSelectionEnd() {
return (getSelectionDuration()+getSelectionStart());
}
public int getSelectionScale() {
return (int) selection.getScale();
}
long winbase=34359738368L;
public void setSelectionStart(long t) {
if (t >= winbase) t -= winbase;
selection.setValue(t);
}
public void setSelectionDuration(long t) {
if (t >= winbase) t -= winbase;
duration.setValue(t);
}
public void setSelectionEnd(long t) {
if (t >= winbase) t -= winbase;
duration.setValue(t-selection.getValue());
}
public void setSelectionStart(String t) {
try {
setSelectionStart(Long.parseLong(t));
} catch (Exception e) {
}
}
public void setSelectionDuration(String t) {
try {
setSelectionDuration(Long.parseLong(t));
} catch (Exception e) {
}
}
public void setSelectionEnd(String t) {
try {
setSelectionEnd(Long.parseLong(t));
} catch (Exception e) {
}
}
public static String[] convertTime(int start, int duration, int scale) {
int selseconds = start/scale;
int selframes = scale/25; // adapt to fps
int selticks = start - selseconds * scale;
int selframeremain = selticks / selframes;
int selhours = selseconds / 3600;
int selminutes = (selseconds - selhours * 3600) / 60;
int selsec = selseconds - (selhours*3600) - (selminutes * 60);
String starttime = new String(""+selhours+":"+selminutes+":"+selsec+"."+selframeremain);
int dursec = (int) ((duration+start)/scale);
int durframes = scale/25; // adapt to fps
int durticks = (duration+start) - dursec * scale;
int durframeremain = durticks / durframes;
int durhours = dursec / 3600;
int durminutes = (dursec - durhours * 3600) / 60;
int durseconds = dursec - (durhours*3600) - (durminutes * 60);
String endtime = new String(" - "+durhours+":"+durminutes+":"+durseconds+"."+durframeremain);
String [] ret = new String[2];
ret[0] = starttime;
ret[1] = endtime;
return ret;
}
}