blob: 0a620c0a75086512a03fada89a5be66f9390f191 [file] [log] [blame]
Guido van Rossum74a3f8b1992-08-18 14:47:41 +00001# Capture a movie using the Indigo video library and board
2
3import sv, SV
4import VFile
5import gl, GL, DEVICE
Guido van Rossum66beddb1992-08-18 16:01:07 +00006import al, AL
Guido van Rossum74a3f8b1992-08-18 14:47:41 +00007
8import time
9import sys
10import posix
11import getopt
12import string
13
14def main():
15 QSIZE = 16
16 TIME = 5
Guido van Rossum66beddb1992-08-18 16:01:07 +000017 audio = 0
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000018
Guido van Rossum66beddb1992-08-18 16:01:07 +000019 opts, args = getopt.getopt(sys.argv[1:], 'aq:t:')
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000020 for opt, arg in opts:
Guido van Rossum66beddb1992-08-18 16:01:07 +000021 if opt == '-a':
22 audio = 1
23 elif opt == '-q':
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000024 QSIZE = string.atoi(arg)
25 elif opt == '-t':
26 TIME = string.atoi(arg)
27
28 if args:
29 filename = args[0]
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000030 else:
31 filename = 'film.video'
32
Guido van Rossum66beddb1992-08-18 16:01:07 +000033 if audio:
34 if args[1:]:
35 audiofilename = args[1]
36 else:
37 audiofilename = 'film.aiff'
38
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000039 gl.foreground()
40
41 x, y = SV.PAL_XMAX / 4, SV.PAL_YMAX / 4
42 print x, 'x', y
43
44 gl.minsize(40, 30)
45 gl.stepunit(8, 6)
46 gl.maxsize(SV.PAL_XMAX, SV.PAL_YMAX)
47 gl.keepaspect(SV.PAL_XMAX, SV.PAL_YMAX)
48 win = gl.winopen(filename)
49 x, y = gl.getsize()
50 print x, 'x', y
51
52 v = sv.OpenVideo()
53 v.BindGLWindow(win, SV.IN_REPLACE)
54 v.SetSize(x, y)
55 v.BindGLWindow(win, SV.IN_REPLACE)
56
57 v.SetCaptureFormat(SV.RGB_FRAMES)
58 v.SetCaptureMode(SV.BLOCKING_CAPTURE)
59 v.SetQueueSize(QSIZE)
60 v.InitCapture()
61 if v.GetQueueSize() != QSIZE:
62 QSIZE = v.GetQueueSize()
63 print 'Warning: QSIZE reduced to', QSIZE
64
65 gl.qdevice(DEVICE.LEFTMOUSE)
66 gl.qdevice(DEVICE.WINQUIT)
67 gl.qdevice(DEVICE.WINSHUT)
68 gl.qdevice(DEVICE.ESCKEY)
69
70 print 'Click left mouse to start recording', TIME, 'seconds'
71 ofile = None
Guido van Rossum66beddb1992-08-18 16:01:07 +000072 afile = None
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000073 # Mouse down opens the file & freezes window
74 # Mouse up starts recording frames
75
76 while 1:
77 dev, val = gl.qread()
78 if dev == DEVICE.LEFTMOUSE:
79 # Start recording
80 if val == 1:
81 # Mouse down -- preparations
82 if ofile == None:
83 ofile = VFile.VoutFile().init(filename)
84 ofile.format = 'rgb8'
85 ofile.width = x
86 ofile.height = y
87 ofile.writeheader()
88 # XXX other format bits?
89 # The window can't be resized from now
90 gl.prefsize(x, y)
91 gl.winconstraints()
92 gl.wintitle('* ' + filename)
Guido van Rossum66beddb1992-08-18 16:01:07 +000093 if audio:
94 afile = initaudio(audiofilename)
Guido van Rossum74a3f8b1992-08-18 14:47:41 +000095 continue
Guido van Rossum66beddb1992-08-18 16:01:07 +000096 # Mouse up -- start actual recording
97 global recording, stop_recording
98 if audio:
99 stop_recording = 0
100 recording.release()
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000101 t0 = time.millitimer()
102 v.StartCapture()
103 while 1:
104 t = time.millitimer() - t0
105 if t >= TIME*1000:
106 break
107 if v.GetCaptured() > 2:
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000108 doframe(v, ofile, x, y, t)
109 v.StopCapture()
Guido van Rossum66beddb1992-08-18 16:01:07 +0000110 stop_recording = 1
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000111 while v.GetCaptured() > 0:
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000112 doframe(v, ofile, x, y, t)
113 t = time.millitimer() - t0
114 gl.wintitle(filename)
115 elif dev == DEVICE.REDRAW:
116 # Window resize (or move)
117 x, y = gl.getsize()
118 print x, 'x', y
119 v.SetSize(x, y)
120 v.BindGLWindow(win, SV.IN_REPLACE)
121 elif dev in (DEVICE.ESCKEY, DEVICE.WINQUIT, DEVICE.WINSHUT):
122 # Quit
123 if ofile:
124 ofile.close()
Guido van Rossum66beddb1992-08-18 16:01:07 +0000125 if afile:
126 afile.destroy()
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000127 posix._exit(0)
128 # EndCapture dumps core...
129 v.EndCapture()
130 v.CloseVideo()
131 gl.winclose(win)
132
133def doframe(v, ofile, x, y, t):
134 cd, start = v.GetCaptureData()
135 data = cd.interleave(x, y)
136 cd.UnlockCaptureData()
137 ofile.writeframe(t, data, None)
138
Guido van Rossum66beddb1992-08-18 16:01:07 +0000139AQSIZE = 16000
140
141def initaudio(filename):
142 import thread, aiff
143 global recording, stop_recording
144 afile = aiff.Aiff().init(filename, 'w')
145 afile.nchannels = AL.MONO
146 afile.sampwidth = AL.SAMPLE_8
147 params = [AL.INPUT_RATE, 0]
148 al.getparams(AL.DEFAULT_DEVICE, params)
149 print 'rate =', params[1]
150 afile.samprate = params[1]
151 c = al.newconfig()
152 c.setchannels(AL.MONO)
153 c.setqueuesize(AQSIZE)
154 c.setwidth(AL.SAMPLE_8)
155 aport = al.openport(filename, 'r', c)
156 recording = thread.allocate_lock()
157 recording.acquire()
158 stop_recording = 0
159 thread.start_new_thread(recorder, (afile, aport))
160 return afile
161
162def recorder(afile, aport):
163 # XXX recording more than one fragment doesn't work
164 # XXX (the thread never dies)
165 recording.acquire()
166 while not stop_recording:
167 data = aport.readsamps(AQSIZE/2)
168 afile.writesampsraw(data)
169 del data
170
Guido van Rossum74a3f8b1992-08-18 14:47:41 +0000171main()