home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / sgi / video / aplay.py < prev    next >
Text File  |  1996-11-27  |  4KB  |  168 lines

  1. #! /usr/bin/env python
  2.  
  3. # Play synchronous video and audio.
  4. # Highly experimental!
  5.  
  6. import sys
  7. import getopt
  8. import string
  9. import os
  10.  
  11. import VFile
  12. import aifc
  13.  
  14. import gl, GL, DEVICE
  15. import al, AL
  16.  
  17.  
  18. def usage():
  19.     sys.stderr.write( \
  20.         'usage: aplay [-o offset] [-q qsize] videofile audiofile\n')
  21.     sys.exit(2)
  22.  
  23. def main():
  24.     offset = 0
  25.     qsize = 0 # This defaults to 1/10 second of sound
  26.     videofile = 'film.video'
  27.     audiofile = 'film.aiff'
  28.  
  29.     try:
  30.         opts, args = getopt.getopt(sys.argv[1:], 'o:q:')
  31.     except getopt.error, msg:
  32.         sys.stderr.write(msg + '\n')
  33.         usage()
  34.  
  35.     try:
  36.         for o, a in opts:
  37.             if o == '-o':
  38.                 offset = string.atoi(a)
  39.             if o == '-q':
  40.                 qsize = string.atoi(a)
  41.     except string.atoi_error:
  42.         sys.stderr.write(o + ' arg must be integer\n')
  43.         usage()
  44.  
  45.     if len(args) > 2:
  46.         usage()
  47.  
  48.     if args: videofile = args[0]
  49.     if args[1:]: audiofile = args[1]
  50.  
  51.     if not os.path.exists(videofile) and \
  52.         os.path.exists(videofile + '.video'):
  53.         if not args[1:] and os.path.exists(videofile + '.aiff'):
  54.             audiofile = videofile + '.aiff'
  55.         videofile = videofile + '.video'
  56.  
  57.     print 'Opening video input file..'
  58.     vin = VFile.VinFile(videofile)
  59.  
  60.     print 'Opening audio input file..'
  61.     ain = aifc.open(audiofile, 'r')
  62.     print 'rate    :', ain.getframerate()
  63.     print 'channels:', ain.getnchannels()
  64.     print 'frames  :', ain.getnframes()
  65.     print 'width   :', ain.getsampwidth()
  66.     print 'kbytes  :', \
  67.           ain.getnframes() * ain.getnchannels() * ain.getsampwidth()
  68.  
  69.     print 'Opening audio output port..'
  70.     c = al.newconfig()
  71.     c.setchannels(ain.getnchannels())
  72.     c.setwidth(ain.getsampwidth())
  73.     nullsample = '\0' * ain.getsampwidth()
  74.     samples_per_second = ain.getnchannels() * ain.getframerate()
  75.     if qsize <= 0: qsize = samples_per_second / 10
  76.     qsize = max(qsize, 512)
  77.     c.setqueuesize(qsize)
  78.     saveparams = [AL.OUTPUT_RATE, 0]
  79.     al.getparams(AL.DEFAULT_DEVICE, saveparams)
  80.     newparams = [AL.OUTPUT_RATE, ain.getframerate()]
  81.     al.setparams(AL.DEFAULT_DEVICE, newparams)
  82.     aport = al.openport(audiofile, 'w', c)
  83.  
  84.     print 'Opening video output window..'
  85.     gl.foreground()
  86.     gl.prefsize(vin.width, vin.height)
  87.     wid = gl.winopen(videofile + ' + ' + audiofile)
  88.     gl.clear()
  89.     vin.initcolormap()
  90.  
  91.     print 'Playing..'
  92.     gl.qdevice(DEVICE.ESCKEY)
  93.     gl.qdevice(DEVICE.LEFTARROWKEY)
  94.     gl.qdevice(DEVICE.RIGHTARROWKEY)
  95. ##    gl.qdevice(DEVICE.UPARROWKEY)
  96. ##    gl.qdevice(DEVICE.DOWNARROWKEY)
  97.     gl.qdevice(DEVICE.SPACEKEY)
  98.  
  99.     while 1:
  100.         samples_written = 0
  101.         samples_read = 0
  102.         lastt = 0
  103.         pause = 0
  104.         while 1:
  105.             if gl.qtest():
  106.                 dev, val = gl.qread()
  107.                 if val == 1:
  108.                     if dev == DEVICE.ESCKEY:
  109.                         sys.exit(0)
  110.                     elif dev == DEVICE.LEFTARROWKEY:
  111.                         offset = offset - 100
  112.                         print 'offset =', offset
  113.                     elif dev == DEVICE.RIGHTARROWKEY:
  114.                         offset = offset + 100
  115.                         print 'offset =', offset
  116.                     elif dev == DEVICE.SPACEKEY:
  117.                         pause = (not pause)
  118.  
  119.             if pause:
  120.                 continue
  121.  
  122.             try:
  123.                 t, data, cdata = vin.getnextframe()
  124.             except EOFError:
  125.                 break
  126.             t = int(t)
  127.             dt = t - lastt
  128.             lastt = t
  129.             target = samples_per_second * t / 1000
  130.             n = target - samples_written + qsize - offset
  131.             if n > 0:
  132.                 # This call will block until the time is right:
  133.                 try:
  134.                     samples = ain.readframes(n)
  135.                 except EOFError:
  136.                     samples = ''
  137.                 k = len(samples) / len(nullsample)
  138.                 samples_read = samples_read + k
  139.                 if k < n:
  140.                     samples = samples + (n-k) * nullsample
  141.                 aport.writesamps(samples)
  142.                 samples_written = samples_written + n
  143.             vin.showframe(data, cdata)
  144.  
  145.         while 1:
  146.             try:
  147.                 samples = ain.readframes(qsize)
  148.             except EOFError:
  149.                 break
  150.             if not samples:
  151.                 break
  152.             aport.writesamps(samples)
  153.             k = len(samples) / len(nullsample)
  154.             samples_read = samples_read + k
  155.             samples_written = samples_written + k
  156.  
  157.         print samples_read, 'samples ==',
  158.         print samples_read * 1.0 / samples_per_second, 'sec.'
  159.         print lastt, 'milliseconds'
  160.  
  161.         print 'Restarting..'
  162.         ain.close()
  163.         ain = aifc.open(audiofile, 'r')
  164.         vin.rewind()
  165.  
  166.  
  167. main()
  168.