# # Media Wrappers for "Introduction to Media Computation" # Started: Mark Guzdial, 2 July 2002 # Revisions: # 18 June 2003: (ellie) # Added (blocking)play(AtRate)InRange methods to Sound class # and global sound functions # Changed getSampleValue, setSampleValue to give the appearance # of 1-based indexing # Added getLeftSampleValue and getRightSampleValue to Sound class # 14 May 2003: Fixed discrepancy between getMediaPath and setMediaFolder (AdamW) # 8 Nov: Fixed getSamplingRate (MarkG) # 1 Nov: Fixed printing pixel # 31 Oct: Fixed pickAColor (MarkG) # 30 Oct: Added raises, fixed error messages. Added repaint (MarkG) # 10 Oct: Coerced value to integer in Sound.setSampleValue (MarkR) # 2 Oct: Corrected calls in setSampleValueAt (MarkG): # 30 Aug: Made commands more consistent and add type-checking (MarkG) # 2 Aug: Changed to getSampleValueAt and setSampleValueAt (MarkG) # # TODO: ## Fix HSV/RGB conversions -- getting a divide by zero error when max=min # Get the Java Pieces import JavaSound import JavaPicture import JavaPixel import JavaMusic import java.awt.Color as jcolor import java.awt.Font as jfont import java.awt as awt import javax.swing as swing import java.util import sys import os import math import traceback # Support a media shortcut mediaFolder = os.getcwd() + os.sep # Store last pickAFile() opening _lastFilePath = "" #Set a path to the media for those who want to have a shortcut (5/14/03 AW) def setMediaPath(): global mediaFolder file = pickAFolder(); mediaFolder = file+os.sep print "New media folder: "+mediaFolder def getMediaPath(filename): global mediaFolder file = mediaFolder+filename if not os.path.isfile(file): print "Note: There is no file at "+file return file #And for those who think of things as folders (5/14/03 AW) def setMediaFolder(): global mediaFolder file = pickAFolder(); mediaFolder = file+os.sep print "New media folder: "+mediaFolder def getMediaFolder(filename): global mediaFolder file = mediaFolder+filename if not os.path.isfile(file): print "Note: There is no file at "+file return file def getShortPath(filename): dirs = filename.split(os.sep) if len(dirs) < 1: return "." elif len(dirs) == 1: return dirs[0] else: return (dirs[len(dirs) - 2] + os.sep + dirs[len(dirs) - 1]) # # Sound # class Sound: def __init__(self,filename): global mediaFolder self.s = JavaSound() try: if not os.path.isabs(filename): filename = mediaFolder + filename self.filename = filename self.s.loadFromFile(filename) except: print "Filename "+filename+" could not be read." print "Are you sure it's a valid sound file?" def __str__(self): return "Sound of length "+str(self.getLength()) def __rep__(self): return "Sound of length "+str(self.getLength()) def play(self): try: self.s.play() except: print "Trouble accessing the sound device." def playAtRate(self,rate): try: self.s.playAtRateDur(rate,self.getLength()) except: exc_type, exc_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." def playAtRateDur(self,rate,dur): try: self.s.playAtRateDur(rate,dur) except: exc_type, exc_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." #added 18June03 - new functionality in JavaSound.java def playInRange(self,start,stop): try: self.s.playAtRateInRange(1,start-1,stop-1) #JavaSound is 0-indexed except: exc_type, ex_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." #added 18June03 - new functionality in JavaSound.java def playAtRateInRange(self,rate,start,stop): try: self.s.playAtRateInRange(rate,start-1,stop-1) #JavaSound is 0-indexed except: exc_type, ex_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." def blockingPlay(self): try: self.s.blockingPlay() except: print "Trouble accessing the sound device." def blockingPlayAtRate(self,rate): try: self.s.blockingPlayAtRateDur(rate,self.getLength()) except: exc_type, exc_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." def blockingPlayAtRateDur(self,rate,dur): try: self.s.blockingPlayAtRateDur(rate,dur) except: exc_type, exc_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." #added 18June03 - new functionality in JavaSound.java def blockingPlayInRange(self,start,stop): try: self.s.blockingPlayAtRateInRange(1,start-1,stop-1) #JavaSound is 0-indexed except: exc_type, ex_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." #added 18June03 - new functionality in JavaSound.java def blockingPlayAtRateInRange(self,rate,start,stop): try: self.s.blockingPlayAtRateInRange(rate,start-1,stop-1) #JavaSound is 0-indexed except: exc_type, ex_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Trouble accessing the sound device." def getSamples(self): return Samples(self) def getLength(self): return self.s.getLengthInFrames() def getSampleValue(self,index): return self.s.getSample(index-1) #JavaSound is 0-indexed #added 18June03 - new functionality in JavaSound.java def getLeftSampleValue(self,index): return self.s.getLeftSample(index-1) #JavaSound is 0-indexed #added 18June03 - new functionality in JavaSound.java def getRightSampleValue(self,index): return self.s.getRightSample(index-1) #JavaSound is 0-indexed def setSampleValue(self,index,value): return self.s.setSample(index-1,int(value)) #JavaSound is 0-indexed #changed 20June03 - JavaSound uses an AudioFileFormat now def getSamplingRate(self): return self.s.getAudioFileFormat().getFormat().getSampleRate() def getSampleObjectAt(self,index): return Sample(self,index) def writeTo(self,filename): global mediaFolder if not os.path.isabs(filename): filename = mediaFolder + filename self.s.writeToFile(filename) class Samples: def __init__(self,aSound): self.myList = [] self.sound = aSound for s in range(1,aSound.getLength()): self.myList.append(Sample(aSound,s)) def __str__(self): return "Samples, length "+str(self.sound.getLength()) def __rep__(self): return "Samples, length "+str(self.sound.getLength()) def __getitem__(self,item): return self.myList[item] def __setitem__(self,item,value): self.sound.setSampleValue(item,value) def getSound(self): return self.sound class Sample: def __init__(self,aSound,index): self.sound=aSound self.index=index def __str__(self): return "Sample at "+str(self.index)+" value at "+str(self.getValue()) def __rep__(self): return "Sample at "+str(self.index)+" value at "+str(self.getValue()) def setValue(self,value): self.sound.setSampleValue(self.index,int(round(value))) def getValue(self): return self.sound.getSampleValue(self.index) def getSound(self): return self.sound ## ## Global sound functions ## def makeSound(filename): global mediaFolder if not os.path.isabs(filename): filename = mediaFolder + filename if not os.path.isfile(filename): print "There is no file at "+filename raise ValueError return Sound(filename) def getSamples(sound): if not sound.__class__ == Sound: print "getSamples(sound): Input is not a sound." raise ValueError return Samples(sound) def play(sound): if not sound.__class__ == Sound: print "play(sound): Input is not a sound." raise ValueError sound.play() def blockingPlay(sound): if not sound.__class__ == Sound: print "blockingPlay(sound): Input is not a sound." raise ValueError sound.blockingPlay() def playAtRate(sound,rate): if not sound.__class__ == Sound: print "playAtRate(sound,rate): Input is not a sound." raise ValueError sound.playAtRate(rate) def playAtRateDur(sound,rate,dur): if not sound.__class__ == Sound: print "playAtRateDur(sound,rate,dur): Input is not a sound." raise ValueError sound.playAtRateDur(rate,dur) #20June03 new functionality in JavaSound (ellie) def playInRange(sound,start,stop): if not sound.__class__ == Sound: print "playInRange(sound,start,stop): Input is not a sound." raise ValueError sound.playInRange(start,stop) #20June03 new functionality in JavaSound (ellie) def blockingPlayInRange(sound,start,stop): if not sound.__class__ == Sound: print "blockingPlayInRange(sound,start,stop): Input is not a sound." raise ValueError sound.blockingPlayInRange(start,stop) #20June03 new functionality in JavaSound (ellie) def playAtRateInRange(sound,rate,start,stop): if not sound.__class__ == Sound: print "playAtRateInRAnge(sound,rate,start,stop): Input is not a sound." raise ValueError sound.playAtRateInRange(rate,start,stop) #20June03 new functionality in JavaSound (ellie) def blockingPlayAtRateInRange(sound,rate,start,stop): if not sound.__class__ == Sound: print "blockingPlayAtRateInRange(sound,rate,start,stop): Input is not a sound." raise ValueError sound.blockingPlayAtRateInRange(start,stop) def getSamplingRate(sound): if not sound.__class__ == Sound: print "getSamplingRate(sound): Input is not a sound." raise ValueError return sound.getSamplingRate() def setSampleValueAt(sound,index,value): if not sound.__class__ == Sound: print "setSampleValueAt(sound,index,value): Input is not a sound." raise ValueError sound.setSampleValue(index,value) def getSampleValueAt(sound,index): if not sound.__class__ == Sound: print "getSampleValueAt(sound,index): Input is not a sound." raise ValueError return sound.getSampleValue(index) def getSampleObjectAt(sound,index): if not sound.__class__ == Sound: print "getSampleObjectAt(sound,index): Input is not a sound." raise ValueError return sound.getSampleObjectAt(index) def setSample(sample,value): if not sample.__class__ == Sample: print "setSample(sample,value): Input is not a sample." raise ValueError # Need to coerce value to integer return sample.setValue(value) def getSample(sample): if not sample.__class__ == Sample: print "getSample(sample): Input is not a sample." raise ValueError return sample.getValue() def getSound(sample): if not sample.__class__ == Sample: print "getSound(sample): Input is not a sample." raise ValueError return sample.getSound() def getLength(sound): if not sound.__class__ == Sound: print "getLength(sound): Input is not a sound." raise ValueError return sound.getLength() def writeSoundTo(sound,filename): global mediaFolder if not os.path.isabs(filename): filename = mediaFolder + filename if not sound.__class__ == Sound: print "writeSoundTo(sound,filename): Input is not a sound." raise ValueError sound.writeTo(filename) ## ## IMAGE CLASSES ## class Picture: def __init__(self): self.pic=JavaPicture() def createImage(self, height, width): self.pic.createNewImage(height, width) def loadImage(self,filename): global mediaFolder try: if not os.path.isabs(filename): filename = mediaFolder + filename if self.pic.loadImage(filename) == 0: print "load image failed." except: exc_type, exc_value, exc_tb = sys.exc_info() print exc_type print exc_value print traceback.extract_tb(exc_tb) print "Was unable to load the image in "+filename def __str__(self): return "Picture, filename "+self.filename()+" height "+str(self.getHeight())+" width "+str(self.getWidth()) def filename(self): return self.pic.filename def repaint(self): self.pic.repaint() def show(self): self.pic.show() def setTitle(self, title): self.pic.setTitle(title) def getWidth(self): return self.pic.getWidth() def getHeight(self): return self.pic.getHeight() def getPixel(self,x,y): return Pixel(self,x,y) def getPixels(self): collect = [] for x in range(1,self.getWidth()): for y in range(1,self.getHeight()): collect.append(Pixel(self,x,y)) return collect def writeTo(self,filename): if not os.path.isabs(filename): filename = mediaFolder + filename self.pic.saveImage(filename) # Graphics stuff on pictures def addRectFilled(self, acolor,x,y,w,h): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.fillRect(x,y,w,h) def addRect(self, acolor,x,y,w,h): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.drawRect(x,y,w,h) def addOvalFilled(self, acolor,x,y,w,h): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.fillOval(x,y,w,h) def addOval(self, acolor,x,y,w,h): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.drawOval(x,y,w,h) def addArcFilled(self, acolor,x,y,w,h,start,angle): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.fillArc(x,y,w,h,start,angle) def addArc(self, acolor,x,y,w,h,start,angle): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.drawArc(x,y,w,h,start,angle) def addLine(self, acolor, x1, y1, x2, y2): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.drawLine(x1,y1,x2,y2) def addText(self, acolor, x, y, string): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.drawString(string, x, y) def addTextWithStyle(self, acolor, x, y, string, style): g = self.pic.bimg.getGraphics() g.setColor(acolor.getJcolor()) g.setFont(style) g.drawString(string, x, y) ## # Globals for styled text ## def makeStyle(fontName,emph,size): return jfont(fontName,emph,size) sansSerif = "SansSerif" serif = "Serif" mono = "Monospaced" italic = jfont.ITALIC bold = jfont.BOLD plain = jfont.PLAIN # # CLASS PIXEL # class Pixel: def __init__(self,picture,x,y): self.pixel = picture.pic.getPixel(x,y) def __str__(self): return "Pixel, color="+str(self.getColor()) def setRed(self,r): self.pixel.setRed(int(r)) def setGreen(self,g): self.pixel.setGreen(int(g)) def setBlue(self,b): self.pixel.setBlue(int(b)) def getRed(self): return self.pixel.getRed() def getGreen(self): return self.pixel.getGreen() def getBlue(self): return self.pixel.getBlue() def getColor(self): return Color(self.getRed(),self.getGreen(), self.getBlue()) def setColor(self,color): self.setRed(color.getRed()) self.setGreen(color.getGreen()) self.setBlue(color.getBlue()) def getX(self): return self.pixel.x def getY(self): return self.pixel.y class Color: def __init__(self,r,g,b): #Check and make sure that the values are correct, i.e. Integers from 0-255 (5/14/03 AdamW) r = int(r) if r < 0: r = 0 if r > 255: r = 255 g = int(g) if g < 0: g = 0 if g > 255: g = 255 b = int(b) if b < 0: b = 0 if b > 255: b = 255 self.r=r self.g=g self.b=b self.setJcolor() def setJcolor(self): self.jc=jcolor(int(self.r),int(self.g),int(self.b)) def setFromJcolor(self,njc): self.r=njc.getRed() self.g=njc.getGreen() self.b=njc.getBlue() self.jc=njc def __str__(self): return "color r="+str(self.getRed())+" g="+str(self.getGreen())+" b="+str(self.getBlue()) def __eq__(self,newcolor): return ((self.r == newcolor.r) and (self.g == newcolor.g) and (self.b == newcolor.b)) def __ne__(self,newcolor): return ((self.r != newcolor.r) or (self.g != newcolor.g) or (self.b != newcolor.b)) def distance(self,color): r = pow((self.r - color.r),2) g = pow((self.g - color.g),2) b = pow((self.b - color.b),2) return math.sqrt(r+g+b) def __sub__(self,color): return Color((self.r-color.r),(self.g-color.g),(self.b-color.b)) def difference(self,color): return self-color def getRGB(self): return [self.r, self.g, self.b] def setRGB(self,atuple): self.r = atuple[1] self.g = atuple[2] self.b = atuple[3] self.setJcolor() def getRed(self): return self.r def getGreen(self): return self.g def getBlue(self): return self.b def setRed(self,value): self.r=value self.setJcolor() def setGreen(self,value): self.g=value self.setJcolor() def setBlue(self,value): self.b=value self.setJcolor() def makeLighter(self): newjc = self.jc.brighter() self.setFromJcolor(newjc) def makeDarker(self): newjc = self.jc.darker() self.setFromJcolor(newjc) def getJcolor(self): return self.jc ## ## Global color functions ## def pickAColor(): import javax.swing choose = javax.swing.JColorChooser() jf = javax.swing.JFrame() retValue = Color(0,0,0).getJcolor() retValue = choose.showDialog(jf,"Choose a color",retValue) print retValue if retValue != None: ncolor=Color(retValue.getRed(),retValue.getGreen(),retValue.getBlue()) #ncolor.setFromJcolor(retValue) return ncolor else: return Color(0,0,0) #Constants black = Color(0,0,0) white = Color(255,255,255) blue = Color(0,0,255) red = Color(255,0,0) green = Color(0,255,0) gray = Color(128,128,128) darkGray = Color(64,64,64) lightGray = Color(192,192,192) yellow = Color(255,255,0) orange = Color(255,200,0) pink = Color(255,175,175) magenta = Color(255,0,255) cyan = Color(0,255,255) ## ## Global picture functions ## def makePicture(filename): global mediaFolder if not os.path.isabs(filename): filename = mediaFolder + filename if not os.path.isfile(filename): print "makePicture(filename): There is no file at "+filename raise ValueError picture = Picture() picture.loadImage(filename) return picture def makeEmptyPicture(height, width): picture = Picture() picture.createImage(height, width) return picture def getPixels(picture): if not picture.__class__ == Picture: print "getPixels(picture): Input is not a picture" raise ValueError return picture.getPixels() def getWidth(picture): if not picture.__class__ == Picture: print "getWidth(picture): Input is not a picture" raise ValueError return picture.getWidth() def getHeight(picture): if not picture.__class__ == Picture: print "getHeight(picture): Input is not a picture" raise ValueError return picture.getHeight() def show(picture, title=None): picture.setTitle(getShortPath(picture.filename())) if title <> None: picture.setTitle(title) if not picture.__class__ == Picture: print "show(picture): Input is not a picture" raise ValueError picture.show() def repaint(picture): if not picture.__class__ == Picture: print "repaint(picture): Input is not a picture" raise ValueError picture.repaint() def addLine(picture,x1,y1,x2,y2): if not picture.__class__ == Picture: print "addLine(picture,x1,y1,x2,y2): Input is not a picture" raise ValueError picture.addLine(black,x1,y1,x2,y2) def addText(picture,x1,y1,string): if not picture.__class__ == Picture: print "addText(picture,x1,y1,string): Input is not a picture" raise ValueError picture.addText(black,x1,y1,string) def addRect(picture,x,y,w,h): if not picture.__class__ == Picture: print "addRect(picture,x,y,w,h): Input is not a picture" raise ValueError picture.addRect(black,x,y,w,h) def addRectFilled(picture,x,y,w,h,acolor): if not picture.__class__ == Picture: print "addRectFilled(picture,x,y,w,h,acolor): Input is not a picture" raise ValueError picture.addRectFilled(acolor,x,y,w,h) def getPixel(picture,x,y): if not picture.__class__ == Picture: print "getPixel(picture,x,y): Input is not a picture" raise ValueError return picture.getPixel(x,y) def setRed(pixel,value): if not pixel.__class__ == Pixel: print "setRed(pixel,value): Input is not a pixel" raise ValueError pixel.setRed(value) def getRed(pixel): if not pixel.__class__ == Pixel: print "getRed(pixel): Input is not a pixel" raise ValueError return pixel.getRed() def setBlue(pixel,value): if not pixel.__class__ == Pixel: print "setBlue(pixel,value): Input is not a pixel" raise ValueError pixel.setBlue(value) def getBlue(pixel): if not pixel.__class__ == Pixel: print "getBlue(pixel): Input is not a pixel" raise ValueError return pixel.getBlue() def setGreen(pixel,value): if not pixel.__class__ == Pixel: print "setGreen(pixel,value): Input is not a pixel" raise ValueError pixel.setGreen(value) def getGreen(pixel): if not pixel.__class__ == Pixel: print "getGreen(pixel): Input is not a pixel" raise ValueError return pixel.getGreen() def getColor(pixel): if not pixel.__class__ == Pixel: print "getColor(pixel): Inputis not a pixel" raise ValueError return pixel.getColor() def setColor(pixel,color): if not pixel.__class__ == Pixel: print "setColor(pixel,color): Input is not a pixel." raise ValueError if not color.__class__ == Color: print "setColor(pixel,color): Input is not a color." raise ValueError pixel.setColor(color) def getX(pixel): if not pixel.__class__ == Pixel: print "getX(pixel): Input is not a pixel" raise ValueError return pixel.getX() def getY(pixel): if not pixel.__class__ == Pixel: print "getY(pixel): Input is not a pixel" raise ValueError return pixel.getY() def distance(c1,c2): if not c1.__class__ == Color: print "distance(c1,c2): First input is not a color." raise ValueError if not c2.__class__ == Color: print "distance(c1,c2): Second input is not a color." raise ValueError return c1.distance(c2) def writePictureTo(pict,filename): global mediaFolder if not os.path.isabs(filename): filename = mediaFolder + filename if not pict.__class__ == Picture: print "writePictureTo(pict,filename): Input is not a picture" raise ValueError pict.writeTo(filename) def makeDarker(color): if not color.__class__ == Color: print "makeDarker(color): Input is not a color." raise ValueError color.makeDarker() return color def makeLighter(color): if not color.__class__ == Color: print "makeLighter(color): Input is not a color." raise ValueError color.makeLighter() return color def makeColor(red,green,blue): return Color(red,green,blue) ## # Java Music Interface ## def playNote(note, duration, intensity=64): JavaMusic.playNote(note, duration, intensity) ## # General user tools # def pickAFile(): global _lastFilePath import javax.swing if _lastFilePath != None and os.path.exists(_lastFilePath): choose = javax.swing.JFileChooser(_lastFilePath) else: choose = javax.swing.JFileChooser() choose.setDialogTitle("Pick A File") jf = javax.swing.JFrame() jf.getContentPane().add(choose) retValue = choose.showOpenDialog(jf) if retValue == 0: path = choose.getSelectedFile().getAbsolutePath() _lastFilePath = os.path.dirname(path) return path else: return "" def pickAFolder(): global _lastFilePath import javax.swing if _lastFilePath != None and os.path.exists(_lastFilePath): choose = javax.swing.JFileChooser(_lastFilePath) else: choose = javax.swing.JFileChooser() choose.setDialogTitle("Pick A Folder") choose.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY) choose.setFileHidingEnabled(1) jf = javax.swing.JFrame() jf.getContentPane().add(choose) retValue = choose.showOpenDialog(jf) if retValue == 0: path = choose.getSelectedFile().getAbsolutePath() _lastFilePath = path return path else: return "" def quit(): sys.exit(0) ## # MediaTools interface # def openPictureTool(picture): import PictureViewer picture.writeTo(".tmp.jpg") p = Picture() p.loadImage(".tmp.jpg") viewer = PictureViewer(p.pic) viewer.setTitle(getShortPath(picture.filename())) def openSoundTool(sound): import SoundView viewer = SoundView(sound.s, 0) viewer.setTitle(getShortPath(sound.filename)) ## # Movie Functions # def makeMovieFromPictures(frames, outputpath, framerate=30): global mediaFolder filename = outputpath try: import JpegImagesToMovie if not os.path.isabs(outputpath): outputpath = mediaFolder + outputpath vec = java.util.Vector() width = frames[0].getWidth() height = frames[0].getHeight() for f in frames: vec.addElement(f.pic) if os.path.exists(filename): os.remove(filename) jitm = JpegImagesToMovie() suc = jitm.doItPath(width, height, framerate, vec, outputpath) if not suc: print "Could not write movie to " + outputpath print "Make sure that the mov path is valid and the pictures are in a list" else: print "Finished writing movie: " + outputpath jitm = None vec = None import java.lang.System as System System.gc() except Exception, e: print "Could not write movie to " + outputpath print "Make sure that the mov path is valid and the pictures are in a list" def openMovie(filename): global mediaFolder import java.lang.System as System if not os.path.isabs(filename): filename = mediaFolder + filename if System.getProperty('os.name').find('Mac') <> -1: print "Movie playback unsupported on Macintosh systems" elif not os.path.exists(filename): print "There is no movie at " + filename else: try: import quicktime import quicktime.io import quicktime.std.movies import quicktime.app.display.QTCanvas import quicktime.app.players.QTPlayer quicktime.QTSession.open() frame = swing.JFrame(os.path.basename(filename)) qtf = quicktime.io.QTFile(filename) movieFile = quicktime.io.OpenMovieFile.asRead(qtf) m = quicktime.std.movies.Movie.fromFile(movieFile) mc = quicktime.std.movies.MovieController(m) mc.setKeysEnabled(1) myQTCanvas = quicktime.app.display.QTCanvas() frame.getContentPane().add(myQTCanvas) myQTPlayer = quicktime.app.players.QTPlayer(mc) myQTCanvas.setClient(myQTPlayer, 1) frame.pack() frame.show() frame.toFront() except Exception, e: print e print "Could not open a player for " + filename print "Make sure it is a valid movie file."