blob: 3afe119ea09db87d7b6f50ad065650c2bc58acca [file] [log] [blame]
Guido van Rossum5d731fc1991-11-04 15:54:22 +00001# Copy a video file, interactively, frame-by-frame.
2
3import sys
4import getopt
5from gl import *
6from DEVICE import *
7
8def loadframe(f, w, h, pf):
9 line = f.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000010 if not line or line == '\n':
Guido van Rossum5d731fc1991-11-04 15:54:22 +000011 raise EOFError
12 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000013 if type(x) == type(0):
Guido van Rossum5d731fc1991-11-04 15:54:22 +000014 if pf: size = w*h*4
15 else: size = w*h*pf
16 else:
17 time, size = x
18 data = f.read(size)
19 if len(data) <> size:
20 raise EOFError
21 if pf:
22 windata = unpackrect(w/pf, h/pf, 1, data)
23 rectzoom(pf, pf)
24 lrectwrite(0, 0, w/pf-1, h/pf-1, windata)
25 else:
26 lrectwrite(0, 0, w-1, h-1, data)
27 return time, data
28
29def report(time, iframe):
30 print 'Frame', iframe, ': t =', time
31
32def usage():
33 sys.write('usage: vcopy infile outfile\n')
34 sys.exit(2)
35
36def help():
37 print 'Command summary:'
38 print 'n get next image from input'
39 print 'w write current image to output'
40
41def main():
42 opts, args = getopt.getopt(sys.argv[1:], '')
43 if len(args) <> 2:
44 usage()
45 [ifile, ofile] = args
46 ifp = open(ifile, 'r')
47 ofp = open(ofile, 'w')
48 #
49 line = ifp.readline()
Guido van Rossumf47d0481992-02-11 14:50:22 +000050 if line[:4] == 'CMIF':
Guido van Rossum5d731fc1991-11-04 15:54:22 +000051 line = ifp.readline()
52 x = eval(line[:-1])
Guido van Rossumf47d0481992-02-11 14:50:22 +000053 if len(x) == 3:
Guido van Rossum5d731fc1991-11-04 15:54:22 +000054 w, h, pf = x
55 else:
56 w, h = x
57 pf = 2
58 if pf:
59 w = (w/pf)*pf
60 h = (h/pf)*pf
61 #
62 ofp.write('CMIF video 1.0\n')
63 ofp.write(`w, h, pf` + '\n')
64 #
65 foreground()
66 prefsize(w, h)
67 wid = winopen(ifile + ' -> ' + ofile)
68 RGBmode()
69 gconfig()
70 qdevice(KEYBD)
71 qdevice(ESCKEY)
72 qdevice(WINQUIT)
73 qdevice(WINSHUT)
74 #
75 help()
76 #
77 time, data = loadframe(ifp, w, h, pf)
78 iframe = 1
79 report(time, iframe)
80 #
81 while 1:
82 dev, val = qread()
83 if dev in (ESCKEY, WINQUIT, WINSHUT):
84 break
Guido van Rossumf47d0481992-02-11 14:50:22 +000085 if dev == REDRAW:
Guido van Rossum5d731fc1991-11-04 15:54:22 +000086 reshapeviewport()
Guido van Rossumf47d0481992-02-11 14:50:22 +000087 elif dev == KEYBD:
Guido van Rossum5d731fc1991-11-04 15:54:22 +000088 c = chr(val)
Guido van Rossumf47d0481992-02-11 14:50:22 +000089 if c == 'n':
Guido van Rossum5d731fc1991-11-04 15:54:22 +000090 try:
91 time, data = loadframe(ifp, w, h, pf)
92 iframe = iframe+1
93 report(time, iframe)
94 except EOFError:
95 print 'EOF'
96 ringbell()
Guido van Rossumf47d0481992-02-11 14:50:22 +000097 elif c == 'w':
Guido van Rossum5d731fc1991-11-04 15:54:22 +000098 ofp.write(`time, len(data)` + '\n')
99 ofp.write(data)
100 print 'Frame', iframe, 'written.'
101 else:
102 print 'Character', `c`, 'ignored'
Guido van Rossumf47d0481992-02-11 14:50:22 +0000103 elif dev == INPUTCHANGE:
Guido van Rossum5d731fc1991-11-04 15:54:22 +0000104 pass
105 else:
106 print '(dev, val) =', (dev, val)
107
108main()