Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 1 | # Capture a movie using the Indigo video library and board |
| 2 | |
| 3 | import sv, SV |
| 4 | import VFile |
| 5 | import gl, GL, DEVICE |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 6 | import al, AL |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 7 | |
| 8 | import time |
| 9 | import sys |
| 10 | import posix |
| 11 | import getopt |
| 12 | import string |
| 13 | |
| 14 | def main(): |
| 15 | QSIZE = 16 |
| 16 | TIME = 5 |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 17 | audio = 0 |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 19 | opts, args = getopt.getopt(sys.argv[1:], 'aq:t:') |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 20 | for opt, arg in opts: |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 21 | if opt == '-a': |
| 22 | audio = 1 |
| 23 | elif opt == '-q': |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 24 | QSIZE = string.atoi(arg) |
| 25 | elif opt == '-t': |
| 26 | TIME = string.atoi(arg) |
| 27 | |
| 28 | if args: |
| 29 | filename = args[0] |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 30 | else: |
| 31 | filename = 'film.video' |
| 32 | |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 33 | if audio: |
| 34 | if args[1:]: |
| 35 | audiofilename = args[1] |
| 36 | else: |
| 37 | audiofilename = 'film.aiff' |
| 38 | |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 39 | 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 Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 72 | afile = None |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 73 | # 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 Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 93 | if audio: |
| 94 | afile = initaudio(audiofilename) |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 95 | continue |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 96 | # Mouse up -- start actual recording |
| 97 | global recording, stop_recording |
| 98 | if audio: |
| 99 | stop_recording = 0 |
| 100 | recording.release() |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 101 | 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 Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 108 | doframe(v, ofile, x, y, t) |
| 109 | v.StopCapture() |
Guido van Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 110 | stop_recording = 1 |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 111 | while v.GetCaptured() > 0: |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 112 | 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 Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 125 | if afile: |
| 126 | afile.destroy() |
Guido van Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 127 | posix._exit(0) |
| 128 | # EndCapture dumps core... |
| 129 | v.EndCapture() |
| 130 | v.CloseVideo() |
| 131 | gl.winclose(win) |
| 132 | |
| 133 | def 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 Rossum | 66beddb | 1992-08-18 16:01:07 +0000 | [diff] [blame] | 139 | AQSIZE = 16000 |
| 140 | |
| 141 | def 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 | |
| 162 | def 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 Rossum | 74a3f8b | 1992-08-18 14:47:41 +0000 | [diff] [blame] | 171 | main() |