Added 'list_only' option (and modified 'run()' to respect it).
[python/dscho.git] / Demo / sgi / video / video2rgb.py
blobbbfa6b823851d3b9c6fc744a61882c065c74843c
1 #! /usr/bin/env python
3 # Convert CMIF movie file(s) to a sequence of rgb images
6 # Help function
8 def help():
9 print 'Usage: video2rgb [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 "prefix0000.rgb"'
15 print 'file ... : file(s) to convert; default film.video'
18 # Imported modules
20 import sys
21 sys.path.append('/ufs/jack/src/av/video') # Increase chance of finding VFile
22 import VFile
23 import time
24 import getopt
25 import string
26 import imgfile
27 import imgconv
30 # Global options
32 quiet = 0
33 prefix = 'film'
34 seqno = 0
35 mono = 0
38 # Main program -- mostly command line parsing
40 def main():
41 global quiet, prefix, mono
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)
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)
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)
69 # Exit with proper exit status
70 sys.exit(sts)
73 # Process one movie file
75 def process(filename):
76 try:
77 vin = VFile.VinFile(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
88 if not quiet:
89 vin.printinfo()
91 width, height = int(vin.width), int(vin.height)
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
102 if mono:
103 depth = 1
104 bpp = 1
105 else:
106 depth = 3
107 bpp = 4
109 convert(vin, cf, width, height, depth, bpp, vin.packfactor)
111 def convert(vin, cf, width, height, depth, bpp, pf):
112 global seqno
114 if type(pf) == type(()):
115 xpf, ypf = pf
116 elif pf == 0:
117 xpf = ypf = 1
118 else:
119 xpf = ypf = pf
120 while 1:
121 try:
122 time, data, cdata = vin.getnextframe()
123 except EOFError:
124 return
125 if cdata:
126 print 'Film contains chromdata!'
127 return
128 data = cf(data, width/xpf, height/abs(ypf))
129 if pf:
130 data = applypackfactor(data, width, height, pf, bpp)
131 s = `seqno`
132 s = '0'*(4-len(s)) + s
133 fname = prefix + s + '.rgb'
134 seqno = seqno + 1
135 if not quiet:
136 print 'Writing',fname,'...'
137 imgfile.write(fname, data, width, height, depth)
139 def applypackfactor(image, w, h, pf, bpp):
140 import imageop
141 if type(pf) == type(()):
142 xpf, ypf = pf
143 elif pf == 0:
144 xpf = ypf = 1
145 else:
146 xpf = ypf = pf
147 w1 = w/xpf
148 h1 = h/abs(ypf)
149 if ypf < 0:
150 ypf = -ypf
151 image = imageop.crop(image, bpp, w1, h1, 0, h1-1, w1-1, 0)
152 return imageop.scale(image, bpp, w1, h1, w, h)
154 # Don't forget to call the main program
156 try:
157 main()
158 except KeyboardInterrupt:
159 print '[Interrupt]'