home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / plat-mac / videoreader.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  10.8 KB  |  351 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. import sys
  5. from Carbon import Qt
  6. from Carbon import QuickTime
  7. from Carbon import Qd
  8. from Carbon import Qdoffs
  9. from Carbon import QDOffscreen
  10. from Carbon import Res
  11.  
  12. try:
  13.     import MediaDescr
  14. except ImportError:
  15.     
  16.     def _audiodescr(data):
  17.         pass
  18.  
  19.  
  20.  
  21. def _audiodescr(data):
  22.     return MediaDescr.SoundDescription.decode(data)
  23.  
  24.  
  25. try:
  26.     from imgformat import macrgb
  27. except ImportError:
  28.     macrgb = 'Macintosh RGB format'
  29.  
  30. import os
  31.  
  32. class VideoFormat:
  33.     
  34.     def __init__(self, name, descr, width, height, format):
  35.         self._VideoFormat__name = name
  36.         self._VideoFormat__descr = descr
  37.         self._VideoFormat__width = width
  38.         self._VideoFormat__height = height
  39.         self._VideoFormat__format = format
  40.  
  41.     
  42.     def getname(self):
  43.         return self._VideoFormat__name
  44.  
  45.     
  46.     def getdescr(self):
  47.         return self._VideoFormat__descr
  48.  
  49.     
  50.     def getsize(self):
  51.         return (self._VideoFormat__width, self._VideoFormat__height)
  52.  
  53.     
  54.     def getformat(self):
  55.         return self._VideoFormat__format
  56.  
  57.  
  58.  
  59. class _Reader:
  60.     
  61.     def __init__(self, path):
  62.         fd = Qt.OpenMovieFile(path, 0)
  63.         (self.movie, d1, d2) = Qt.NewMovieFromFile(fd, 0, 0)
  64.         self.movietimescale = self.movie.GetMovieTimeScale()
  65.         
  66.         try:
  67.             self.audiotrack = self.movie.GetMovieIndTrackType(1, QuickTime.AudioMediaCharacteristic, QuickTime.movieTrackCharacteristic)
  68.             self.audiomedia = self.audiotrack.GetTrackMedia()
  69.         except Qt.Error:
  70.             self.audiotrack = None
  71.             self.audiomedia = None
  72.             self.audiodescr = { }
  73.  
  74.         handle = Res.Handle('')
  75.         n = self.audiomedia.GetMediaSampleDescriptionCount()
  76.         self.audiomedia.GetMediaSampleDescription(1, handle)
  77.         self.audiodescr = _audiodescr(handle.data)
  78.         self.audiotimescale = self.audiomedia.GetMediaTimeScale()
  79.         del handle
  80.         
  81.         try:
  82.             self.videotrack = self.movie.GetMovieIndTrackType(1, QuickTime.VisualMediaCharacteristic, QuickTime.movieTrackCharacteristic)
  83.             self.videomedia = self.videotrack.GetTrackMedia()
  84.         except Qt.Error:
  85.             self.videotrack = None
  86.             self.videomedia = None
  87.             self.videotimescale = None
  88.  
  89.         if self.videotrack:
  90.             self.videotimescale = self.videomedia.GetMediaTimeScale()
  91.             (x0, y0, x1, y1) = self.movie.GetMovieBox()
  92.             self.videodescr = {
  93.                 'width': x1 - x0,
  94.                 'height': y1 - y0 }
  95.             self._initgworld()
  96.         
  97.         self.videocurtime = None
  98.         self.audiocurtime = None
  99.  
  100.     
  101.     def __del__(self):
  102.         self.audiomedia = None
  103.         self.audiotrack = None
  104.         self.videomedia = None
  105.         self.videotrack = None
  106.         self.movie = None
  107.  
  108.     
  109.     def _initgworld(self):
  110.         (old_port, old_dev) = Qdoffs.GetGWorld()
  111.         
  112.         try:
  113.             movie_w = self.videodescr['width']
  114.             movie_h = self.videodescr['height']
  115.             movie_rect = (0, 0, movie_w, movie_h)
  116.             self.gworld = Qdoffs.NewGWorld(32, movie_rect, None, None, QDOffscreen.keepLocal)
  117.             self.pixmap = self.gworld.GetGWorldPixMap()
  118.             Qdoffs.LockPixels(self.pixmap)
  119.             Qdoffs.SetGWorld(self.gworld.as_GrafPtr(), None)
  120.             Qd.EraseRect(movie_rect)
  121.             self.movie.SetMovieGWorld(self.gworld.as_GrafPtr(), None)
  122.             self.movie.SetMovieBox(movie_rect)
  123.             self.movie.SetMovieActive(1)
  124.             self.movie.MoviesTask(0)
  125.             self.movie.SetMoviePlayHints(QuickTime.hintsHighQuality, QuickTime.hintsHighQuality)
  126.         finally:
  127.             Qdoffs.SetGWorld(old_port, old_dev)
  128.  
  129.  
  130.     
  131.     def _gettrackduration_ms(self, track):
  132.         tracktime = track.GetTrackDuration()
  133.         return self._movietime_to_ms(tracktime)
  134.  
  135.     
  136.     def _movietime_to_ms(self, time):
  137.         (value, d1, d2) = Qt.ConvertTimeScale((time, self.movietimescale, None), 1000)
  138.         return value
  139.  
  140.     
  141.     def _videotime_to_ms(self, time):
  142.         (value, d1, d2) = Qt.ConvertTimeScale((time, self.videotimescale, None), 1000)
  143.         return value
  144.  
  145.     
  146.     def _audiotime_to_ms(self, time):
  147.         (value, d1, d2) = Qt.ConvertTimeScale((time, self.audiotimescale, None), 1000)
  148.         return value
  149.  
  150.     
  151.     def _videotime_to_movietime(self, time):
  152.         (value, d1, d2) = Qt.ConvertTimeScale((time, self.videotimescale, None), self.movietimescale)
  153.         return value
  154.  
  155.     
  156.     def HasAudio(self):
  157.         return self.audiotrack is not None
  158.  
  159.     
  160.     def HasVideo(self):
  161.         return self.videotrack is not None
  162.  
  163.     
  164.     def GetAudioDuration(self):
  165.         if not self.audiotrack:
  166.             return 0
  167.         
  168.         return self._gettrackduration_ms(self.audiotrack)
  169.  
  170.     
  171.     def GetVideoDuration(self):
  172.         if not self.videotrack:
  173.             return 0
  174.         
  175.         return self._gettrackduration_ms(self.videotrack)
  176.  
  177.     
  178.     def GetAudioFormat(self):
  179.         if not self.audiodescr:
  180.             return (None, None, None, None, None)
  181.         
  182.         bps = self.audiodescr['sampleSize']
  183.         nch = self.audiodescr['numChannels']
  184.         if nch == 1:
  185.             channels = [
  186.                 'mono']
  187.         elif nch == 2:
  188.             channels = [
  189.                 'left',
  190.                 'right']
  191.         else:
  192.             channels = map((lambda x: str(x + 1)), range(nch))
  193.         if bps % 8:
  194.             blocksize = 0
  195.             fpb = 0
  196.         else:
  197.             blocksize = (bps / 8) * nch
  198.             fpb = 1
  199.         if self.audiodescr['dataFormat'] == 'raw ':
  200.             encoding = 'linear-excess'
  201.         elif self.audiodescr['dataFormat'] == 'twos':
  202.             encoding = 'linear-signed'
  203.         else:
  204.             encoding = 'quicktime-coding-%s' % self.audiodescr['dataFormat']
  205.         return (channels, encoding, blocksize, fpb, bps)
  206.  
  207.     
  208.     def GetAudioFrameRate(self):
  209.         if not self.audiodescr:
  210.             return None
  211.         
  212.         return int(self.audiodescr['sampleRate'])
  213.  
  214.     
  215.     def GetVideoFormat(self):
  216.         width = self.videodescr['width']
  217.         height = self.videodescr['height']
  218.         return VideoFormat('dummy_format', 'Dummy Video Format', width, height, macrgb)
  219.  
  220.     
  221.     def GetVideoFrameRate(self):
  222.         tv = self.videocurtime
  223.         if tv == None:
  224.             tv = 0
  225.         
  226.         flags = QuickTime.nextTimeStep | QuickTime.nextTimeEdgeOK
  227.         (tv, dur) = self.videomedia.GetMediaNextInterestingTime(flags, tv, 1.0)
  228.         dur = self._videotime_to_ms(dur)
  229.         return int(1000.0 / dur + 0.5)
  230.  
  231.     
  232.     def ReadAudio(self, nframes, time = None):
  233.         if time is not None:
  234.             self.audiocurtime = time
  235.         
  236.         flags = QuickTime.nextTimeStep | QuickTime.nextTimeEdgeOK
  237.         if self.audiocurtime == None:
  238.             self.audiocurtime = 0
  239.         
  240.         tv = self.audiomedia.GetMediaNextInterestingTimeOnly(flags, self.audiocurtime, 1.0)
  241.         if (tv < 0 or self.audiocurtime) and tv < self.audiocurtime:
  242.             return (self._audiotime_to_ms(self.audiocurtime), None)
  243.         
  244.         h = Res.Handle('')
  245.         desc_h = Res.Handle('')
  246.         (size, actualtime, sampleduration, desc_index, actualcount, flags) = self.audiomedia.GetMediaSample(h, 0, tv, desc_h, nframes)
  247.         self.audiocurtime = actualtime + actualcount * sampleduration
  248.         return (self._audiotime_to_ms(actualtime), h.data)
  249.  
  250.     
  251.     def ReadVideo(self, time = None):
  252.         if time is not None:
  253.             self.videocurtime = time
  254.         
  255.         flags = QuickTime.nextTimeStep
  256.         if self.videocurtime == None:
  257.             flags = flags | QuickTime.nextTimeEdgeOK
  258.             self.videocurtime = 0
  259.         
  260.         tv = self.videomedia.GetMediaNextInterestingTimeOnly(flags, self.videocurtime, 1.0)
  261.         if (tv < 0 or self.videocurtime) and tv <= self.videocurtime:
  262.             return (self._videotime_to_ms(self.videocurtime), None)
  263.         
  264.         self.videocurtime = tv
  265.         moviecurtime = self._videotime_to_movietime(self.videocurtime)
  266.         self.movie.SetMovieTimeValue(moviecurtime)
  267.         self.movie.MoviesTask(0)
  268.         return (self._videotime_to_ms(self.videocurtime), self._getpixmapcontent())
  269.  
  270.     
  271.     def _getpixmapcontent(self):
  272.         '''Shuffle the offscreen PixMap data, because it may have funny stride values'''
  273.         rowbytes = Qdoffs.GetPixRowBytes(self.pixmap)
  274.         width = self.videodescr['width']
  275.         height = self.videodescr['height']
  276.         start = 0
  277.         rv = ''
  278.         for i in range(height):
  279.             nextline = Qdoffs.GetPixMapBytes(self.pixmap, start, width * 4)
  280.             start = start + rowbytes
  281.             rv = rv + nextline
  282.         
  283.         return rv
  284.  
  285.  
  286.  
  287. def reader(url):
  288.     
  289.     try:
  290.         rdr = _Reader(url)
  291.     except IOError:
  292.         return None
  293.  
  294.     return rdr
  295.  
  296.  
  297. def _test():
  298.     import EasyDialogs
  299.     
  300.     try:
  301.         import img
  302.     except ImportError:
  303.         img = None
  304.  
  305.     import MacOS
  306.     Qt.EnterMovies()
  307.     path = EasyDialogs.AskFileForOpen(message = 'Video to convert')
  308.     if not path:
  309.         sys.exit(0)
  310.     
  311.     rdr = reader(path)
  312.     if not rdr:
  313.         sys.exit(1)
  314.     
  315.     dstdir = EasyDialogs.AskFileForSave(message = 'Name for output folder')
  316.     if not dstdir:
  317.         sys.exit(0)
  318.     
  319.     num = 0
  320.     os.mkdir(dstdir)
  321.     videofmt = rdr.GetVideoFormat()
  322.     imgfmt = videofmt.getformat()
  323.     (imgw, imgh) = videofmt.getsize()
  324.     (timestamp, data) = rdr.ReadVideo()
  325.     while data:
  326.         fname = 'frame%04.4d.jpg' % num
  327.         num = num + 1
  328.         pname = os.path.join(dstdir, fname)
  329.         if not img:
  330.             print 'Not',
  331.         
  332.         print 'Writing %s, size %dx%d, %d bytes' % (fname, imgw, imgh, len(data))
  333.         if img:
  334.             wrt = img.writer(imgfmt, pname)
  335.             wrt.width = imgw
  336.             wrt.height = imgh
  337.             wrt.write(data)
  338.             (timestamp, data) = rdr.ReadVideo()
  339.             MacOS.SetCreatorAndType(pname, 'ogle', 'JPEG')
  340.             if num > 20:
  341.                 print 'stopping at 20 frames so your disk does not fill up:-)'
  342.                 break
  343.             
  344.         num > 20
  345.     print 'Total frames:', num
  346.  
  347. if __name__ == '__main__':
  348.     _test()
  349.     sys.exit(1)
  350.  
  351.