<s> import sys <EOL> from Carbon import Qt <EOL> from Carbon import QuickTime <EOL> from Carbon import Qd <EOL> from Carbon import Qdoffs <EOL> from Carbon import QDOffscreen <EOL> from Carbon import Res <EOL> try : <EOL> import MediaDescr <EOL> except ImportError : <EOL> def _audiodescr ( data ) : <EOL> return None <EOL> else : <EOL> def _audiodescr ( data ) : <EOL> return MediaDescr . SoundDescription . decode ( data ) <EOL> try : <EOL> from imgformat import macrgb <EOL> except ImportError : <EOL> macrgb = "<STR_LIT>" <EOL> import os <EOL> class VideoFormat : <EOL> def __init__ ( self , name , descr , width , height , format ) : <EOL> self . __name = name <EOL> self . __descr = descr <EOL> self . __width = width <EOL> self . __height = height <EOL> self . __format = format <EOL> def getname ( self ) : <EOL> return self . __name <EOL> def getdescr ( self ) : <EOL> return self . __descr <EOL> def getsize ( self ) : <EOL> return self . __width , self . __height <EOL> def getformat ( self ) : <EOL> return self . __format <EOL> class _Reader : <EOL> def __init__ ( self , path ) : <EOL> fd = Qt . OpenMovieFile ( path , <NUM_LIT:0> ) <EOL> self . movie , d1 , d2 = Qt . NewMovieFromFile ( fd , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> self . movietimescale = self . movie . GetMovieTimeScale ( ) <EOL> try : <EOL> self . audiotrack = self . movie . GetMovieIndTrackType ( <NUM_LIT:1> , <EOL> QuickTime . AudioMediaCharacteristic , QuickTime . movieTrackCharacteristic ) <EOL> self . audiomedia = self . audiotrack . GetTrackMedia ( ) <EOL> except Qt . Error : <EOL> self . audiotrack = self . audiomedia = None <EOL> self . audiodescr = { } <EOL> else : <EOL> handle = Res . Handle ( '<STR_LIT>' ) <EOL> n = self . audiomedia . GetMediaSampleDescriptionCount ( ) <EOL> self . audiomedia . GetMediaSampleDescription ( <NUM_LIT:1> , handle ) <EOL> self . audiodescr = _audiodescr ( handle . data ) <EOL> self . audiotimescale = self . audiomedia . GetMediaTimeScale ( ) <EOL> del handle <EOL> try : <EOL> self . videotrack = self . movie . GetMovieIndTrackType ( <NUM_LIT:1> , <EOL> QuickTime . VisualMediaCharacteristic , QuickTime . movieTrackCharacteristic ) <EOL> self . videomedia = self . videotrack . GetTrackMedia ( ) <EOL> except Qt . Error : <EOL> self . videotrack = self . videomedia = self . videotimescale = None <EOL> if self . videotrack : <EOL> self . videotimescale = self . videomedia . GetMediaTimeScale ( ) <EOL> x0 , y0 , x1 , y1 = self . movie . GetMovieBox ( ) <EOL> self . videodescr = { '<STR_LIT:width>' : ( x1 - x0 ) , '<STR_LIT>' : ( y1 - y0 ) } <EOL> self . _initgworld ( ) <EOL> self . videocurtime = None <EOL> self . audiocurtime = None <EOL> def __del__ ( self ) : <EOL> self . audiomedia = None <EOL> self . audiotrack = None <EOL> self . videomedia = None <EOL> self . videotrack = None <EOL> self . movie = None <EOL> def _initgworld ( self ) : <EOL> old_port , old_dev = Qdoffs . GetGWorld ( ) <EOL> try : <EOL> movie_w = self . videodescr [ '<STR_LIT:width>' ] <EOL> movie_h = self . videodescr [ '<STR_LIT>' ] <EOL> movie_rect = ( <NUM_LIT:0> , <NUM_LIT:0> , movie_w , movie_h ) <EOL> self . gworld = Qdoffs . NewGWorld ( <NUM_LIT:32> , movie_rect , None , None , QDOffscreen . keepLocal ) <EOL> self . pixmap = self . gworld . GetGWorldPixMap ( ) <EOL> Qdoffs . LockPixels ( self . pixmap ) <EOL> Qdoffs . SetGWorld ( self . gworld . as_GrafPtr ( ) , None ) <EOL> Qd . EraseRect ( movie_rect ) <EOL> self . movie . SetMovieGWorld ( self . gworld . as_GrafPtr ( ) , None ) <EOL> self . movie . SetMovieBox ( movie_rect ) <EOL> self . movie . SetMovieActive ( <NUM_LIT:1> ) <EOL> self . movie . MoviesTask ( <NUM_LIT:0> ) <EOL> self . movie . SetMoviePlayHints ( QuickTime . hintsHighQuality , QuickTime . hintsHighQuality ) <EOL> finally : <EOL> Qdoffs . SetGWorld ( old_port , old_dev ) <EOL> def _gettrackduration_ms ( self , track ) : <EOL> tracktime = track . GetTrackDuration ( ) <EOL> return self . _movietime_to_ms ( tracktime ) <EOL> def _movietime_to_ms ( self , time ) : <EOL> value , d1 , d2 = Qt . ConvertTimeScale ( ( time , self . movietimescale , None ) , <NUM_LIT:1000> ) <EOL> return value <EOL> def _videotime_to_ms ( self , time ) : <EOL> value , d1 , d2 = Qt . ConvertTimeScale ( ( time , self . videotimescale , None ) , <NUM_LIT:1000> ) <EOL> return value <EOL> def _audiotime_to_ms ( self , time ) : <EOL> value , d1 , d2 = Qt . ConvertTimeScale ( ( time , self . audiotimescale , None ) , <NUM_LIT:1000> ) <EOL> return value <EOL> def _videotime_to_movietime ( self , time ) : <EOL> value , d1 , d2 = Qt . ConvertTimeScale ( ( time , self . videotimescale , None ) , <EOL> self . movietimescale ) <EOL> return value <EOL> def HasAudio ( self ) : <EOL> return not self . audiotrack is None <EOL> def HasVideo ( self ) : <EOL> return not self . videotrack is None <EOL> def GetAudioDuration ( self ) : <EOL> if not self . audiotrack : <EOL> return <NUM_LIT:0> <EOL> return self . _gettrackduration_ms ( self . audiotrack ) <EOL> def GetVideoDuration ( self ) : <EOL> if not self . videotrack : <EOL> return <NUM_LIT:0> <EOL> return self . _gettrackduration_ms ( self . videotrack ) <EOL> def GetAudioFormat ( self ) : <EOL> if not self . audiodescr : <EOL> return None , None , None , None , None <EOL> bps = self . audiodescr [ '<STR_LIT>' ] <EOL> nch = self . audiodescr [ '<STR_LIT>' ] <EOL> if nch == <NUM_LIT:1> : <EOL> channels = [ '<STR_LIT>' ] <EOL> elif nch == <NUM_LIT:2> : <EOL> channels = [ '<STR_LIT:left>' , '<STR_LIT:right>' ] <EOL> else : <EOL> channels = map ( lambda x : str ( x + <NUM_LIT:1> ) , range ( nch ) ) <EOL> if bps % <NUM_LIT:8> : <EOL> blocksize = <NUM_LIT:0> <EOL> fpb = <NUM_LIT:0> <EOL> else : <EOL> blocksize = ( bps / <NUM_LIT:8> ) * nch <EOL> fpb = <NUM_LIT:1> <EOL> if self . audiodescr [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> encoding = '<STR_LIT>' <EOL> elif self . audiodescr [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> encoding = '<STR_LIT>' <EOL> else : <EOL> encoding = '<STR_LIT>' % self . audiodescr [ '<STR_LIT>' ] <EOL> return channels , encoding , blocksize , fpb , bps <EOL> def GetAudioFrameRate ( self ) : <EOL> if not self . audiodescr : <EOL> return None <EOL> return int ( self . audiodescr [ '<STR_LIT>' ] ) <EOL> def GetVideoFormat ( self ) : <EOL> width = self . videodescr [ '<STR_LIT:width>' ] <EOL> height = self . videodescr [ '<STR_LIT>' ] <EOL> return VideoFormat ( '<STR_LIT>' , '<STR_LIT>' , width , height , macrgb ) <EOL> def GetVideoFrameRate ( self ) : <EOL> tv = self . videocurtime <EOL> if tv == None : <EOL> tv = <NUM_LIT:0> <EOL> flags = QuickTime . nextTimeStep | QuickTime . nextTimeEdgeOK <EOL> tv , dur = self . videomedia . GetMediaNextInterestingTime ( flags , tv , <NUM_LIT:1.0> ) <EOL> dur = self . _videotime_to_ms ( dur ) <EOL> return int ( ( <NUM_LIT> / dur ) + <NUM_LIT:0.5> ) <EOL> def ReadAudio ( self , nframes , time = None ) : <EOL> if not time is None : <EOL> self . audiocurtime = time <EOL> flags = QuickTime . nextTimeStep | QuickTime . nextTimeEdgeOK <EOL> if self . audiocurtime == None : <EOL> self . audiocurtime = <NUM_LIT:0> <EOL> tv = self . audiomedia . GetMediaNextInterestingTimeOnly ( flags , self . audiocurtime , <NUM_LIT:1.0> ) <EOL> if tv < <NUM_LIT:0> or ( self . audiocurtime and tv < self . audiocurtime ) : <EOL> return self . _audiotime_to_ms ( self . audiocurtime ) , None <EOL> h = Res . Handle ( '<STR_LIT>' ) <EOL> desc_h = Res . Handle ( '<STR_LIT>' ) <EOL> size , actualtime , sampleduration , desc_index , actualcount , flags = self . audiomedia . GetMediaSample ( h , <NUM_LIT:0> , tv , desc_h , nframes ) <EOL> self . audiocurtime = actualtime + actualcount * sampleduration <EOL> return self . _audiotime_to_ms ( actualtime ) , h . data <EOL> def ReadVideo ( self , time = None ) : <EOL> if not time is None : <EOL> self . videocurtime = time <EOL> flags = QuickTime . nextTimeStep <EOL> if self . videocurtime == None : <EOL> flags = flags | QuickTime . nextTimeEdgeOK <EOL> self . videocurtime = <NUM_LIT:0> <EOL> tv = self . videomedia . GetMediaNextInterestingTimeOnly ( flags , self . videocurtime , <NUM_LIT:1.0> ) <EOL> if tv < <NUM_LIT:0> or ( self . videocurtime and tv <= self . videocurtime ) : <EOL> return self . _videotime_to_ms ( self . videocurtime ) , None <EOL> self . videocurtime = tv <EOL> moviecurtime = self . _videotime_to_movietime ( self . videocurtime ) <EOL> self . movie . SetMovieTimeValue ( moviecurtime ) <EOL> self . movie . MoviesTask ( <NUM_LIT:0> ) <EOL> return self . _videotime_to_ms ( self . videocurtime ) , self . _getpixmapcontent ( ) <EOL> def _getpixmapcontent ( self ) : <EOL> """<STR_LIT>""" <EOL> rowbytes = Qdoffs . GetPixRowBytes ( self . pixmap ) <EOL> width = self . videodescr [ '<STR_LIT:width>' ] <EOL> height = self . videodescr [ '<STR_LIT>' ] <EOL> start = <NUM_LIT:0> <EOL> rv = '<STR_LIT>' <EOL> for i in range ( height ) : <EOL> nextline = Qdoffs . GetPixMapBytes ( self . pixmap , start , width * <NUM_LIT:4> ) <EOL> start = start + rowbytes <EOL> rv = rv + nextline <EOL> return rv <EOL> def reader ( url ) : <EOL> try : <EOL> rdr = _Reader ( url ) <EOL> except IOError : <EOL> return None <EOL> return rdr <EOL> def _test ( ) : <EOL> import EasyDialogs <EOL> try : <EOL> import img <EOL> except ImportError : <EOL> img = None <EOL> import MacOS <EOL> Qt . EnterMovies ( ) <EOL> path = EasyDialogs . AskFileForOpen ( message = '<STR_LIT>' ) <EOL> if not path : sys . exit ( <NUM_LIT:0> ) <EOL> rdr = reader ( path ) <EOL> if not rdr : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> dstdir = EasyDialogs . AskFileForSave ( message = '<STR_LIT>' ) <EOL> if not dstdir : sys . exit ( <NUM_LIT:0> ) <EOL> num = <NUM_LIT:0> <EOL> os . mkdir ( dstdir ) <EOL> videofmt = rdr . GetVideoFormat ( ) <EOL> imgfmt = videofmt . getformat ( ) <EOL> imgw , imgh = videofmt . getsize ( ) <EOL> timestamp , data = rdr . ReadVideo ( ) <EOL> while data : <EOL> fname = '<STR_LIT>' % num <EOL> num = num + <NUM_LIT:1> <EOL> pname = os . path . join ( dstdir , fname ) <EOL> if not img : print '<STR_LIT>' , <EOL> print '<STR_LIT>' % ( fname , imgw , imgh , len ( data ) ) <EOL> if img : <EOL> wrt = img . writer ( imgfmt , pname ) <EOL> wrt . width = imgw <EOL> wrt . height = imgh <EOL> wrt . write ( data ) <EOL> timestamp , data = rdr . ReadVideo ( ) <EOL> MacOS . SetCreatorAndType ( pname , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if num > <NUM_LIT:20> : <EOL> print '<STR_LIT>' <EOL> break <EOL> print '<STR_LIT>' , num <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) <EOL> sys . exit ( <NUM_LIT:1> ) </s>
