We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Movie Player (Read 599 times)
Movie Player
May 5th, 2009, 7:57pm
 
I am trying to create a class that can play the image and audio tracks of movies in various formats. This is my first try. There are several issues. First of all, I rely on awt, which could cause problems. Secondly, I am not sure whether the image and audio tracks will remain in sync. Thirdly the program does not exit gracefully. Fourthly, you name it...
It is all very rough. But I would welcome any thoughts on this approach, or perhaps suggestions for alternative directions.

I will post a sample usage in a separate message, since this one is about to exceed the character limit...

Thanks!

<code follows>

import quicktime.*;
import quicktime.app.time.*;
import quicktime.app.view.*;
import quicktime.io.*;
import quicktime.std.*;
import java.awt.*;
import quicktime.std.movies.*;
import quicktime.std.clocks.*;

class Channel {
    Movie movie;
    MoviePlayer Channel;
    MovieController mc;
    QTImageProducer iP;
    QTFile f;
    OpenMovieFile omf;
    Track audioTrack;
    int trackCount;
    Movie copiedMovie; //virtual clipboard
   
Channel(  String file){
  try {
   QTSessionCheck.check( );
   f = new QTFile( file );
   omf = OpenMovieFile.asRead( f );
   movie = Movie.fromFile( omf );
   trackCount = movie.getTrackCount( );
   Channel = new MoviePlayer( movie );
   mc = new MovieController( movie );  
   
  audioTrack = movie.getIndTrackType( 1,
       StdQTConstants.audioMediaCharacteristic,
       StdQTConstants.movieTrackCharacteristic );
   
  }catch (Exception e) {  System.out.println( e ); }
} //constructor


void start( ){
 try {
    TaskAllMovies.addMovieAndStart(  );
    movie.start( );
 } catch (StdQTException e ) { System.out.println( e ) ; }
 
}

void pause( ) {
 try {
  movie.stop( );
 } catch (Exception e ) { }
}

void stop( ){
 try {
   movie.stop( );
   movie.setTime( new TimeRecord( movie.getTimeScale( ), 0 ) );  
 } catch (Exception e ) { }
}

void prepareToEdit(boolean edit ) {
  try {
    mc.enableEditing( edit );
  } catch (StdQTException e ) { System.out.println( e ) ; }
} //prepareToEdit( )


Image grabImage(int w, int h ) {
 Image image = null;
 try {
  iP = new QTImageProducer(
  Channel, new Dimension( w, h ) );
  image = Toolkit.getDefaultToolkit().createImage( iP );
 }catch (Exception e ) {  return null; }
 return image;
}


void copy( int sourceIn, int sourceDuration ){
  sourceIn = adjustTimeScale( sourceIn );
  sourceDuration = adjustTimeScale( sourceDuration );
  try {
 copiedMovie = new Movie( );
 movie.insertSegment( copiedMovie,
            sourceIn,
            sourceDuration,
            0 );    
  } catch (Exception e ) { System.out.println( e ) ; }
}


boolean cut( int sourceIn, int sourceDuration ) {
  sourceIn = adjustTimeScale( sourceIn );
  sourceDuration = adjustTimeScale( sourceDuration );
 try {
   copiedMovie = new Movie( );
   movie.insertSegment( copiedMovie,
            sourceIn,
            sourceDuration,
            0 );
   movie.deleteSegment( sourceIn, sourceDuration );
   mc.movieChanged( );
    } catch (Exception e ) { System.out.println( e ) ; return false;}
    return true;
}

boolean paste( int targetIn, int duration  ) {
    targetIn = adjustTimeScale( targetIn );
    duration = adjustTimeScale( duration );
  try {  
  if (copiedMovie == null) return false;
   copiedMovie.insertSegment( movie,
                0,
                duration,
                targetIn );
   } catch (Exception e ) { System.out.println( e ) ; return false; }
   return true;
}


void delete( int start, int duration) {
    start = adjustTimeScale( start );
    duration = adjustTimeScale( duration );
  try {
    movie.deleteSegment( start, duration );
  } catch (Exception e ) { System.out.println( e ) ; }
}

void undo( ){
  try {
 mc.undo( );
    } catch (QTException e ) { System.out.println( e ) ; }
}


void scaleSegment( int start, int oldDuration, int newDuration ){
 start = adjustTimeScale( start );
 oldDuration = adjustTimeScale( oldDuration);
 newDuration = adjustTimeScale( newDuration );
 try {
    movie.scaleSegment( start, oldDuration, newDuration );
   } catch (QTException e ) { System.out.println( e ) ; }  
}


void jump( int whereToJump){
 try {
 movie.setTimeValue( adjustTimeScale( whereToJump) );
 movie.update( );
 mc.movieChanged( );
 } catch (Exception e ) { }
 
}


int adjustTimeScale( int seconds ) {
 try {
    return seconds * movie.getTimeScale( );
 }   catch( Exception e ) { return -1; }
}


boolean adjustSound( boolean whatToDo ) {  
 try {
    if (audioTrack == null) return false;
 audioTrack.setEnabled( whatToDo );  
 return true;
 } catch (Exception e ) { return false; }
}


//if the audioTrack is disabled, enable it
//otherwise, disable it.
boolean flipSound( ){
  try {
 return adjustSound( !audioTrack.getEnabled( ) );
  } catch (Exception e ) { return false; }  
}



 public static class QTSessionCheck {

 private Thread shutdownHook;
   static QTSessionCheck instance;
 private QTSessionCheck( ) throws QTException {
     super( );
     // init
     QTSession.open( );
     // create shutdown handler
     shutdownHook = new Thread( ) {
             public void run( ) {
                 QTSession.close( );
             }
         };
     Runtime.getRuntime( ).addShutdownHook(shutdownHook);
 }
static QTSessionCheck getInstance( ) throws QTException {
     if (instance == null)
         instance = new QTSessionCheck( );
     return instance;
 }
 
 static void check( ) throws QTException {
     getInstance( );
 }
 
}
 
Re: Movie Player
Reply #1 - May 5th, 2009, 8:06pm
 
This message continues my previous post. Here is an example of how the class previously posted might be used. Hope this is clear....


Channel ap = new Channel( "somePathToAMovie.mov" );
Channel ap2 = new Channel( "SomeAudioFile.mp3");
Channel ap3 = new Channel( "anotherFile.avi");

void setup( ) {
size( 900, 900);

//I want to edit the clip
ap.prepareToEdit( true);

//cut the first 15 seconds of the clip (but the original
//file will not be modified),
//so that every time I restart the movie, it begins 15 secs into the clip
ap.cut( 0, 15 ); //this method returns false if it fails...  

//start playing.
//now we start to hear the sound
//but the image will not be displayed.
//The image display is handled in draw( ).
ap.start( );
}


void draw( ) {
PImage img;
 try {
  //println( ap2.m.getTime( ) );
  img = new PImage( ap.grabImage( width, height) );
  loadPixels( );
  img.loadPixels( );
  arrayCopy( img.pixels, pixels);
  updatePixels( );

  //Two alternative ways of displaying the image...
  //background( img );
  //image( img, 0, 0);

 //Will the image and sound stay roughly in sync?

 } catch (Exception e ) { }

}


//just to demonstrate some other functionality.
void keyPressed( ) {
//if the sound is on, turn it off, or vice versa...
 if (key == 'x') ap.flipSound( );  
//jump to a random point in the movie
if (key == 'k')  ap.jump( (int) random( 550) );  

//You can also call ap.pause(), which will stop the  movie
//so that any calls to ap.start() will begin from the same point
//If you call ap.pause( ), the movie will commence from the beginning
}
Re: Movie Player
Reply #2 - May 5th, 2009, 8:18pm
 
I forgot to add some calls to mc.movieChanged( ) in some of the functions in the first posting, and in the second posting i wrote (wrongly) that calling Channel.pause( ) will cause any call to Channel.start() to begin from the start. I meant to write that calling Channel.stop( ) will do that...
Page Index Toggle Pages: 1