Guido van Rossum | 5d731fc | 1991-11-04 15:54:22 +0000 | [diff] [blame] | 1 | # Copy a video file, interactively, frame-by-frame. |
| 2 | |
| 3 | import sys |
| 4 | import getopt |
| 5 | from gl import * |
| 6 | from DEVICE import * |
| 7 | |
| 8 | def loadframe(f, w, h, pf): |
| 9 | line = f.readline() |
| 10 | if not line or line = '\n': |
| 11 | raise EOFError |
| 12 | x = eval(line[:-1]) |
| 13 | if type(x) = type(0): |
| 14 | 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 | |
| 29 | def report(time, iframe): |
| 30 | print 'Frame', iframe, ': t =', time |
| 31 | |
| 32 | def usage(): |
| 33 | sys.write('usage: vcopy infile outfile\n') |
| 34 | sys.exit(2) |
| 35 | |
| 36 | def help(): |
| 37 | print 'Command summary:' |
| 38 | print 'n get next image from input' |
| 39 | print 'w write current image to output' |
| 40 | |
| 41 | def 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() |
| 50 | if line[:4] = 'CMIF': |
| 51 | line = ifp.readline() |
| 52 | x = eval(line[:-1]) |
| 53 | if len(x) = 3: |
| 54 | 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 |
| 85 | if dev = REDRAW: |
| 86 | reshapeviewport() |
| 87 | elif dev = KEYBD: |
| 88 | c = chr(val) |
| 89 | if c = 'n': |
| 90 | 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() |
| 97 | elif c = 'w': |
| 98 | ofp.write(`time, len(data)` + '\n') |
| 99 | ofp.write(data) |
| 100 | print 'Frame', iframe, 'written.' |
| 101 | else: |
| 102 | print 'Character', `c`, 'ignored' |
| 103 | elif dev = INPUTCHANGE: |
| 104 | pass |
| 105 | else: |
| 106 | print '(dev, val) =', (dev, val) |
| 107 | |
| 108 | main() |