aplay.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Demo » sgi » video » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Demo » sgi » video » aplay.py
#! /usr/bin/env python

# Play synchronous video and audio.
# Highly experimental!

import sys
import getopt
import string
import os

import VFile
import aifc

import gl, GL, DEVICE
import al, AL


def usage():
  sys.stderr.write( \
    'usage: aplay [-o offset] [-q qsize] videofile audiofile\n')
  sys.exit(2)

def main():
  offset = 0
  qsize = 0 # This defaults to 1/10 second of sound
  videofile = 'film.video'
  audiofile = 'film.aiff'

  try:
    opts, args = getopt.getopt(sys.argv[1:], 'o:q:')
  except getopt.error, msg:
    sys.stderr.write(msg + '\n')
    usage()

  try:
    for o, a in opts:
      if o == '-o':
        offset = string.atoi(a)
      if o == '-q':
        qsize = string.atoi(a)
  except string.atoi_error:
    sys.stderr.write(o + ' arg must be integer\n')
    usage()

  if len(args) > 2:
    usage()

  if args: videofile = args[0]
  if args[1:]: audiofile = args[1]

  if not os.path.exists(videofile) and \
    os.path.exists(videofile + '.video'):
    if not args[1:] and os.path.exists(videofile + '.aiff'):
      audiofile = videofile + '.aiff'
    videofile = videofile + '.video'

  print 'Opening video input file..'
  vin = VFile.VinFile(videofile)

  print 'Opening audio input file..'
  ain = aifc.open(audiofile, 'r')
  print 'rate    :', ain.getframerate()
  print 'channels:', ain.getnchannels()
  print 'frames  :', ain.getnframes()
  print 'width   :', ain.getsampwidth()
  print 'kbytes  :', \
      ain.getnframes() * ain.getnchannels() * ain.getsampwidth()

  print 'Opening audio output port..'
  c = al.newconfig()
  c.setchannels(ain.getnchannels())
  c.setwidth(ain.getsampwidth())
  nullsample = '\0' * ain.getsampwidth()
  samples_per_second = ain.getnchannels() * ain.getframerate()
  if qsize <= 0: qsize = samples_per_second / 10
  qsize = max(qsize, 512)
  c.setqueuesize(qsize)
  saveparams = [AL.OUTPUT_RATE, 0]
  al.getparams(AL.DEFAULT_DEVICE, saveparams)
  newparams = [AL.OUTPUT_RATE, ain.getframerate()]
  al.setparams(AL.DEFAULT_DEVICE, newparams)
  aport = al.openport(audiofile, 'w', c)

  print 'Opening video output window..'
  gl.foreground()
  gl.prefsize(vin.width, vin.height)
  wid = gl.winopen(videofile + ' + ' + audiofile)
  gl.clear()
  vin.initcolormap()

  print 'Playing..'
  gl.qdevice(DEVICE.ESCKEY)
  gl.qdevice(DEVICE.LEFTARROWKEY)
  gl.qdevice(DEVICE.RIGHTARROWKEY)
##  gl.qdevice(DEVICE.UPARROWKEY)
##  gl.qdevice(DEVICE.DOWNARROWKEY)
  gl.qdevice(DEVICE.SPACEKEY)

  while 1:
    samples_written = 0
    samples_read = 0
    lastt = 0
    pause = 0
    while 1:
      if gl.qtest():
        dev, val = gl.qread()
        if val == 1:
          if dev == DEVICE.ESCKEY:
            sys.exit(0)
          elif dev == DEVICE.LEFTARROWKEY:
            offset = offset - 100
            print 'offset =', offset
          elif dev == DEVICE.RIGHTARROWKEY:
            offset = offset + 100
            print 'offset =', offset
          elif dev == DEVICE.SPACEKEY:
            pause = (not pause)

      if pause:
        continue

      try:
        t, data, cdata = vin.getnextframe()
      except EOFError:
        break
      t = int(t)
      dt = t - lastt
      lastt = t
      target = samples_per_second * t / 1000
      n = target - samples_written + qsize - offset
      if n > 0:
        # This call will block until the time is right:
        try:
          samples = ain.readframes(n)
        except EOFError:
          samples = ''
        k = len(samples) / len(nullsample)
        samples_read = samples_read + k
        if k < n:
          samples = samples + (n-k) * nullsample
        aport.writesamps(samples)
        samples_written = samples_written + n
      vin.showframe(data, cdata)

    while 1:
      try:
        samples = ain.readframes(qsize)
      except EOFError:
        break
      if not samples:
        break
      aport.writesamps(samples)
      k = len(samples) / len(nullsample)
      samples_read = samples_read + k
      samples_written = samples_written + k

    print samples_read, 'samples ==',
    print samples_read * 1.0 / samples_per_second, 'sec.'
    print lastt, 'milliseconds'

    print 'Restarting..'
    ain.close()
    ain = aifc.open(audiofile, 'r')
    vin.rewind()


main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.