Jack Jansen | eeec33f | 1993-02-17 15:52:56 +0000 | [diff] [blame] | 1 | #! /ufs/guido/bin/sgi/python |
| 2 | |
| 3 | # Play CMIF movie files |
| 4 | |
| 5 | |
| 6 | # Help function |
| 7 | |
| 8 | def help(): |
| 9 | print 'Usage: Vplay [options] [file] ...' |
| 10 | print |
| 11 | print 'Options:' |
| 12 | print '-q : quiet, no informative messages' |
| 13 | print '-m : create monochrome (greyscale) image files' |
| 14 | print '-f prefix : create image files with names "prefix000.rgb"' |
| 15 | print 'file ... : file(s) to play; default film.video' |
| 16 | |
| 17 | |
| 18 | # Imported modules |
| 19 | |
| 20 | import sys |
| 21 | sys.path.append('/ufs/guido/src/video') # Increase chance of finding VFile |
| 22 | import VFile |
| 23 | import time |
| 24 | import getopt |
| 25 | import string |
| 26 | import imgfile |
| 27 | import imgconv |
| 28 | |
| 29 | |
| 30 | # Global options |
| 31 | |
| 32 | quiet = 0 |
| 33 | prefix = 'film' |
| 34 | seqno = 0 |
| 35 | mono = 0 |
| 36 | |
| 37 | |
| 38 | # Main program -- mostly command line parsing |
| 39 | |
| 40 | def main(): |
| 41 | global quiet, prefix, mono |
| 42 | |
| 43 | # Parse command line |
| 44 | try: |
| 45 | opts, args = getopt.getopt(sys.argv[1:], 'qmf:') |
| 46 | except getopt.error, msg: |
| 47 | sys.stdout = sys.stderr |
| 48 | print 'Error:', msg, '\n' |
| 49 | help() |
| 50 | sys.exit(2) |
| 51 | |
| 52 | # Interpret options |
| 53 | try: |
| 54 | for opt, arg in opts: |
| 55 | if opt == '-q': quiet = 1 |
| 56 | if opt == '-f': prefix = arg |
| 57 | if opt == '-m': mono = 1 |
| 58 | except string.atoi_error: |
| 59 | sys.stdout = sys.stderr |
| 60 | print 'Option', opt, 'requires integer argument' |
| 61 | sys.exit(2) |
| 62 | |
| 63 | # Process all files |
| 64 | if not args: args = ['film.video'] |
| 65 | sts = 0 |
| 66 | for filename in args: |
| 67 | sts = (process(filename) or sts) |
| 68 | |
| 69 | # Exit with proper exit status |
| 70 | sys.exit(sts) |
| 71 | |
| 72 | |
| 73 | # Process one movie file |
| 74 | |
| 75 | def process(filename): |
| 76 | try: |
| 77 | vin = VFile.VinFile().init(filename) |
| 78 | except IOError, msg: |
| 79 | sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n') |
| 80 | return 1 |
| 81 | except VFile.Error, msg: |
| 82 | sys.stderr.write(msg + '\n') |
| 83 | return 1 |
| 84 | except EOFError: |
| 85 | sys.stderr.write(filename + ': EOF in video header\n') |
| 86 | return 1 |
| 87 | |
| 88 | if not quiet: |
| 89 | vin.printinfo() |
| 90 | |
| 91 | width, height = int(vin.width), int(vin.height) |
| 92 | |
| 93 | try: |
| 94 | if mono: |
| 95 | cf = imgconv.getconverter(vin.format, 'grey') |
| 96 | else: |
| 97 | cf = imgconv.getconverter(vin.format, 'rgb') |
| 98 | except imgconv.error: |
| 99 | print 'Sorry, no converter available for type',vin.format |
| 100 | return |
| 101 | |
| 102 | if mono: |
| 103 | depth = 1 |
| 104 | else: |
| 105 | depth = 3 |
| 106 | |
| 107 | pf = testpackfactor(vin.packfactor) |
| 108 | if pf == None: |
| 109 | print 'Sorry, packfactor not supported:', vin.packfactor |
| 110 | convert(vin, cf, width, height, depth, pf) |
| 111 | |
| 112 | def convert(vin, cf, width, height, depth, pf): |
| 113 | global seqno |
| 114 | |
| 115 | while 1: |
| 116 | try: |
| 117 | time, data, cdata = vin.getnextframe() |
| 118 | except EOFError: |
| 119 | return |
| 120 | if cdata: |
| 121 | print 'Film contains chromdata!' |
| 122 | return |
| 123 | if pf: |
| 124 | data = applypackfactor(data, width, height, pf) |
| 125 | s = `seqno` |
| 126 | s = '0'*(4-len(s)) + s |
| 127 | fname = prefix + s + '.rgb' |
| 128 | seqno = seqno + 1 |
| 129 | if not quiet: |
| 130 | print 'Writing',fname,'...' |
| 131 | imgfile.write(fname, data, width, height, depth) |
| 132 | |
| 133 | def testpackfactor(pf): |
| 134 | if type(pf) == type(()): |
| 135 | xpf, ypf = pf |
| 136 | else: |
| 137 | if pf in (0, 1): |
| 138 | return 0 |
| 139 | return None |
| 140 | if xpf <> 1: |
| 141 | return None |
| 142 | return pf |
| 143 | |
| 144 | def applypackfactor(image, w, h, pf): |
| 145 | if type(pf) == type(()): |
| 146 | xpf, ypf = pf |
| 147 | else: |
| 148 | xpf = ypf = 1 |
| 149 | rows = [] |
| 150 | for i in range(0, (w*h+ypf-1)/abs(ypf), w): |
| 151 | rows.append(image[i:i+w]) |
| 152 | if ypf < 0: |
| 153 | rows.reverse() |
| 154 | ypf = -ypf |
| 155 | image = '' |
| 156 | for i in range(0, h): |
| 157 | image = image + rows[i/ypf] |
| 158 | return image |
| 159 | |
| 160 | # Don't forget to call the main program |
| 161 | |
| 162 | try: |
| 163 | main() |
| 164 | except KeyboardInterrupt: |
| 165 | print '[Interrupt]' |