blob: 1ad6713f49b91f7bb7f30559c0cdc2eb16f82103 [file] [log] [blame]
Guido van Rossum82534fd1992-08-18 17:01:02 +00001#! /usr/local/python
2
3# Play CMIF movie files
4
5
6# Usage:
7#
Guido van Rossum78aab861992-08-20 11:52:42 +00008# Vplay [-L] [-M maginfy] [-m msec] [-q] [-r msec] [-s factor] [file] ...
Guido van Rossum82534fd1992-08-18 17:01:02 +00009
10
11# Options:
12#
Guido van Rossum78aab861992-08-20 11:52:42 +000013# -L : loop, playing the movie over and over again
14# -M magnify : magnify the image by the given factor
15# -m n : drop frames closer than n msec (default 0)
16# -q : quiet, no informative messages
17# -r n : regenerate input time base n msec apart
18# -s speed : speed change factor after other processing (default 1.0)
Guido van Rossum82534fd1992-08-18 17:01:02 +000019# file ... : file(s) to play; default film.video
20
21
22# User interface:
23#
24# Place the windo where you want it. The size is determined by the
25# movie file and the -m option.
26#
27# Press ESC or select the window manager Quit or Close window option
28# to close a window; if more files are given the window for the next
Guido van Rossum78aab861992-08-20 11:52:42 +000029# file now pops up. Unless looping, the window closes automatically
30# at the end of the movie.
Guido van Rossum82534fd1992-08-18 17:01:02 +000031
32
Guido van Rossum843d1531992-08-18 14:16:12 +000033import sys
Guido van Rossum82534fd1992-08-18 17:01:02 +000034sys.path.append('/ufs/guido/src/video')
Guido van Rossum843d1531992-08-18 14:16:12 +000035import VFile
36import time
37import gl, GL
38from DEVICE import *
Guido van Rossum82534fd1992-08-18 17:01:02 +000039import getopt
40import string
41
42
43# Global options
44
45magnify = 1
46looping = 0
Guido van Rossum78aab861992-08-20 11:52:42 +000047speed = 1.0
48mindelta = 0
49quiet = 0
50regen = None
Guido van Rossum82534fd1992-08-18 17:01:02 +000051
52
53# Main program -- mostly command line parsing
Guido van Rossum843d1531992-08-18 14:16:12 +000054
55def main():
Guido van Rossum78aab861992-08-20 11:52:42 +000056 global magnify, looping, speed, mindelta, quiet, regen
57 opts, args = getopt.getopt(sys.argv[1:], 'LM:m:qr:s:')
Guido van Rossum82534fd1992-08-18 17:01:02 +000058 for opt, arg in opts:
Guido van Rossum78aab861992-08-20 11:52:42 +000059 if opt == '-L':
Guido van Rossum82534fd1992-08-18 17:01:02 +000060 looping = 1
Guido van Rossum78aab861992-08-20 11:52:42 +000061 elif opt == '-M':
Guido van Rossum82534fd1992-08-18 17:01:02 +000062 magnify = string.atoi(arg)
Guido van Rossum78aab861992-08-20 11:52:42 +000063 elif opt == '-m':
64 mindelta = string.atoi(arg)
65 elif opt == '-q':
66 quiet = 1
67 elif opt == '-r':
68 regen = string.atoi(arg)
69 elif opt == '-s':
70 speed = float(eval(arg))
Guido van Rossum82534fd1992-08-18 17:01:02 +000071 if not args:
72 args = ['film.video']
73 for filename in args:
74 process(filename)
75
76
Guido van Rossum78aab861992-08-20 11:52:42 +000077# Process one movie file
Guido van Rossum843d1531992-08-18 14:16:12 +000078
79def process(filename):
Guido van Rossum78aab861992-08-20 11:52:42 +000080 try:
81 vin = VFile.VinFile().init(filename)
82 except IOError, msg:
83 sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n')
84 return
85 except VFile.Error, msg:
86 sys.stderr.write(msg + '\n')
87 return
88 except EOFError:
89 sys.stderr.write(filename + ': EOF in video header\n')
90 return
91
92 if not quiet:
93 print 'File: ', filename
94 print 'Version: ', vin.version
95 print 'Size: ', vin.width, 'x', vin.height
96 print 'Pack: ', vin.packfactor, '; chrom:', vin.chrompack
97 print 'Bits: ', vin.c0bits, vin.c1bits, vin.c2bits
98 print 'Format: ', vin.format
99 print 'Offset: ', vin.offset
Guido van Rossum843d1531992-08-18 14:16:12 +0000100
101 gl.foreground()
Guido van Rossum82534fd1992-08-18 17:01:02 +0000102 gl.prefsize(vin.width * magnify, vin.height * magnify)
Guido van Rossum78aab861992-08-20 11:52:42 +0000103 win = gl.winopen(filename)
104 if quiet:
105 vin.quiet = 1
Guido van Rossum843d1531992-08-18 14:16:12 +0000106 vin.initcolormap()
107
108 gl.qdevice(ESCKEY)
109 gl.qdevice(WINSHUT)
110 gl.qdevice(WINQUIT)
111
Guido van Rossum843d1531992-08-18 14:16:12 +0000112 while 1:
Guido van Rossum78aab861992-08-20 11:52:42 +0000113 stop = playonce(vin)
114 if stop or not looping:
115 break
116
117 gl.wintitle('(done) ' + filename)
118
119 while not stop:
120 dev, val = gl.qread()
121 if dev in (ESCKEY, WINSHUT, WINQUIT):
122 stop = 1
123
124 gl.winclose(win)
125
126
127# Play a movie once; return 1 if user wants to stop, 0 if not
128
129def playonce(vin):
130 if vin.hascache:
131 vin.rewind()
132 vin.colormapinited = 1
133 vin.magnify = magnify
134
135 tin = 0
136 told = 0
137 nin = 0
138 nout = 0
139 nmissed = 0
140 nskipped = 0
141
142 t0 = time.millitimer()
143
144 while 1:
145 try:
146 tin, size, csize = vin.getnextframeheader()
147 except EOFError:
148 break
149 nin = nin+1
150 if regen:
151 tout = nin * regen
152 else:
153 tout = tin
154 tout = int(tout / speed)
155 if tout - told < mindelta:
Guido van Rossum843d1531992-08-18 14:16:12 +0000156 try:
Guido van Rossum78aab861992-08-20 11:52:42 +0000157 vin.skipnextframedata(size, csize)
Guido van Rossum843d1531992-08-18 14:16:12 +0000158 except EOFError:
Guido van Rossum78aab861992-08-20 11:52:42 +0000159 print '[short data at EOF]'
160 break
161 nskipped = nskipped + 1
162 else:
163 told = tout
164 dt = tout + t0 - time.millitimer()
165 if dt < 0:
166 try:
167 vin.skipnextframedata(size, csize)
168 except EOFError:
169 print '[short data at EOF]'
170 break
171 nmissed = nmissed + 1
172 else:
173 try:
174 data, cdata = vin.getnextframedata \
175 (size, csize)
176 except EOFError:
177 print '[short data at EOF]'
178 break
179 dt = tout + t0 - time.millitimer()
180 if dt > 0:
181 time.millisleep(dt)
182 vin.showframe(data, cdata)
183 nout = nout + 1
184 if gl.qtest():
Guido van Rossum843d1531992-08-18 14:16:12 +0000185 dev, val = gl.qread()
186 if dev in (ESCKEY, WINSHUT, WINQUIT):
Guido van Rossum78aab861992-08-20 11:52:42 +0000187 return 1
Guido van Rossum843d1531992-08-18 14:16:12 +0000188 if dev == REDRAW:
189 gl.reshapeviewport()
190 if data:
Guido van Rossum78aab861992-08-20 11:52:42 +0000191 vin.showframe(data, cdata)
192
193 t1 = time.millitimer()
194
195 if quiet:
196 return 0
197
198 print 'Recorded:', nin, 'frames in', tin*0.001, 'sec.',
199 if tin: print '-- average', int(nin*10000.0/tin)*0.1, 'frames/sec',
200 print
201
202 if nskipped:
203 print 'Skipped', nskipped, 'frames'
204
205 tout = t1-t0
206 print 'Played:', nout,
207 print 'frames in', tout*0.001, 'sec.',
208 if tout: print '-- average', int(nout*10000.0/tout)*0.1, 'frames/sec',
209 print
210
211 if nmissed:
212 print 'Missed', nmissed, 'frames'
213
214 return 0
Guido van Rossum843d1531992-08-18 14:16:12 +0000215
Guido van Rossum82534fd1992-08-18 17:01:02 +0000216
217# Don't forget to call the main program
218
Guido van Rossum843d1531992-08-18 14:16:12 +0000219main()