1 #! /ufs/guido/bin/sgi/python
2 #! /ufs/guido/src/video/py
4 # Capture a CMIF movie using the Indigo video library and board
9 # makemovie [-q queuesize] [-t recordtime] [-a] [moviefile [audiofile]]
14 # -q queuesize : set the capture queue size (default and max 16)
15 # -t recordtime : set the record time in seconds (default 5 seconds)
16 # -a : record audio as well
17 # moviefile : here goes the movie data (default film.video);
18 # the format is documented in cmif-film.ms
19 # audiofile : with -a, here goes the audio data (default film.aiff);
20 # audio data is recorded in AIFF format, using the
21 # input sampling rate, source and volume set by the
22 # audio panel, in mono, 8 bits/sample
27 # Start the application. Resize the window to the desired movie size.
28 # Click the left mouse button to start recording (recording starts
29 # when you release the mouse button). Recording time is specified by
30 # the -t option (XXX this should change).
32 # Press ESC or select the window manager Quit or Close window option
33 # to quit. (You can do this without recording -- then the output
34 # files are untouched.)
36 # (It is possible to record more than once; but this doesn't set the
37 # time stamps correctly yet, and doesn't work at all with audio. So
43 # fix timestamps for second and further recordings
45 # flush audio buffer when recording starts
46 # make code more readable
50 sys
.path
.append('/ufs/guido/src/video')
66 opts
, args
= getopt
.getopt(sys
.argv
[1:], 'aq:t:')
71 QSIZE
= string
.atoi(arg
)
73 TIME
= string
.atoi(arg
)
78 filename
= 'film.video'
82 audiofilename
= args
[1]
84 audiofilename
= 'film.aiff'
88 x
, y
= SV
.PAL_XMAX
/ 4, SV
.PAL_YMAX
/ 4
93 gl
.maxsize(SV
.PAL_XMAX
, SV
.PAL_YMAX
)
94 gl
.keepaspect(SV
.PAL_XMAX
, SV
.PAL_YMAX
)
95 win
= gl
.winopen(filename
)
100 v
.BindGLWindow(win
, SV
.IN_REPLACE
)
102 v
.BindGLWindow(win
, SV
.IN_REPLACE
)
104 v
.SetCaptureFormat(SV
.RGB_FRAMES
)
105 v
.SetCaptureMode(SV
.BLOCKING_CAPTURE
)
106 v
.SetQueueSize(QSIZE
)
108 if v
.GetQueueSize() != QSIZE
:
109 QSIZE
= v
.GetQueueSize()
110 print 'Warning: QSIZE reduced to', QSIZE
112 gl
.qdevice(DEVICE
.LEFTMOUSE
)
113 gl
.qdevice(DEVICE
.WINQUIT
)
114 gl
.qdevice(DEVICE
.WINSHUT
)
115 gl
.qdevice(DEVICE
.ESCKEY
)
117 print 'Click left mouse to start recording', TIME
, 'seconds'
120 # Mouse down opens the file & freezes window
121 # Mouse up starts recording frames
124 dev
, val
= gl
.qread()
125 if dev
== DEVICE
.LEFTMOUSE
:
128 # Mouse down -- preparations
130 ofile
= VFile
.VoutFile().init(filename
)
131 ofile
.format
= 'rgb8'
135 # XXX other format bits?
136 # The window can't be resized from now
139 gl
.wintitle('* ' + filename
)
141 afile
= initaudio(audiofilename
)
143 # Mouse up -- start actual recording
144 global recording
, stop_recording
148 t0
= time
.millitimer()
151 t
= time
.millitimer() - t0
154 if v
.GetCaptured() > 2:
155 doframe(v
, ofile
, x
, y
, t
)
158 while v
.GetCaptured() > 0:
159 doframe(v
, ofile
, x
, y
, t
)
160 t
= time
.millitimer() - t0
161 gl
.wintitle(filename
)
162 elif dev
== DEVICE
.REDRAW
:
163 # Window resize (or move)
167 v
.BindGLWindow(win
, SV
.IN_REPLACE
)
168 elif dev
in (DEVICE
.ESCKEY
, DEVICE
.WINQUIT
, DEVICE
.WINSHUT
):
175 # EndCapture dumps core...
180 def doframe(v
, ofile
, x
, y
, t
):
181 cd
, start
= v
.GetCaptureData()
182 data
= cd
.interleave(x
, y
)
183 cd
.UnlockCaptureData()
184 ofile
.writeframe(t
, data
, None)
188 def initaudio(filename
):
190 global recording
, stop_recording
191 afile
= aiff
.Aiff().init(filename
, 'w')
192 afile
.nchannels
= AL
.MONO
193 afile
.sampwidth
= AL
.SAMPLE_8
194 params
= [AL
.INPUT_RATE
, 0]
195 al
.getparams(AL
.DEFAULT_DEVICE
, params
)
196 print 'rate =', params
[1]
197 afile
.samprate
= params
[1]
199 c
.setchannels(AL
.MONO
)
200 c
.setqueuesize(AQSIZE
)
201 c
.setwidth(AL
.SAMPLE_8
)
202 aport
= al
.openport(filename
, 'r', c
)
203 recording
= thread
.allocate_lock()
206 thread
.start_new_thread(recorder
, (afile
, aport
))
209 def recorder(afile
, aport
):
210 # XXX recording more than one fragment doesn't work
211 # XXX (the thread never dies)
213 while not stop_recording
:
214 data
= aport
.readsamps(AQSIZE
/2)
215 afile
.writesampsraw(data
)