1 #! /ufs/guido/bin/sgi/python
3 # Play synchronous video and audio.
20 'usage: aplay [-o offset] [-q qsize] videofile audiofile\n')
25 qsize
= 0 # This defaults to 1/10 second of sound
26 videofile
= 'film.video'
27 audiofile
= 'film.aiff'
30 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'o:q:')
31 except getopt
.error
, msg
:
32 sys
.stderr
.write(msg
+ '\n')
38 offset
= string
.atoi(a
)
40 qsize
= string
.atoi(a
)
41 except string
.atoi_error
:
42 sys
.stderr
.write(o
+ ' arg must be integer\n')
48 if args
: videofile
= args
[0]
49 if args
[1:]: audiofile
= args
[1]
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'
57 print 'Opening video input file..'
58 vin
= VFile
.VinFile(videofile
)
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()
67 ain
.getnframes() * ain
.getnchannels() * ain
.getsampwidth()
69 print 'Opening audio output port..'
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)
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
)
84 print 'Opening video output window..'
86 gl
.prefsize(vin
.width
, vin
.height
)
87 wid
= gl
.winopen(videofile
+ ' + ' + audiofile
)
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
)
106 dev
, val
= gl
.qread()
108 if dev
== DEVICE
.ESCKEY
:
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
:
123 t
, data
, cdata
= vin
.getnextframe()
129 target
= samples_per_second
* t
/ 1000
130 n
= target
- samples_written
+ qsize
- offset
132 # This call will block until the time is right:
134 samples
= ain
.readframes(n
)
137 k
= len(samples
) / len(nullsample
)
138 samples_read
= samples_read
+ k
140 samples
= samples
+ (n
-k
) * nullsample
141 aport
.writesamps(samples
)
142 samples_written
= samples_written
+ n
143 vin
.showframe(data
, cdata
)
147 samples
= ain
.readframes(qsize
)
152 aport
.writesamps(samples
)
153 k
= len(samples
) / len(nullsample
)
154 samples_read
= samples_read
+ k
155 samples_written
= samples_written
+ k
157 print samples_read
, 'samples ==',
158 print samples_read
* 1.0 / samples_per_second
, 'sec.'
159 print lastt
, 'milliseconds'
163 ain
= aifc
.open(audiofile
, 'r')