blob: fb7afab7ad72e20b3b49b94629c79ccd7ba860d8 [file] [log] [blame]
Guido van Rossum5e044b71993-02-25 14:20:13 +00001#! /ufs/guido/bin/sgi/python
Guido van Rossum349f2b51992-12-24 11:41:14 +00002
Guido van Rossum5e044b71993-02-25 14:20:13 +00003# Universal (non-interactive) CMIF video file copier.
Guido van Rossum349f2b51992-12-24 11:41:14 +00004
Guido van Rossum5e044b71993-02-25 14:20:13 +00005
6# Possibilities:
7#
8# - Manipulate the time base:
9# = resample at a fixed rate
10# = divide the time codes by a speed factor (to make it go faster/slower)
11# = drop frames that are less than n msec apart (to accomodate slow players)
12# - Convert to a different format
13# - Magnify (scale) the image
14
15
16# Usage function (keep this up-to-date if you change the program!)
Guido van Rossum349f2b51992-12-24 11:41:14 +000017
18def usage():
Guido van Rossum5e044b71993-02-25 14:20:13 +000019 print 'Usage: Vcopy [options] [infile [outfile]]'
20 print
21 print 'Options:'
22 print
23 print '-t type : new image type (default unchanged)'
24 print
25 print '-M magnify : image magnification factor (default unchanged)'
26 print '-w width : output image width (default height*4/3 if -h used)'
27 print '-h height : output image height (default width*3/4 if -w used)'
28 print
29 print '-p pf : new x and y packfactor (default unchanged)'
30 print '-x xpf : new x packfactor (default 1 if -y used)'
31 print '-y ypf : new y packfactor (default 1 if -x used)'
32 print
33 print '-m delta : drop frames closer than delta msec (default 0)'
34 print '-r delta : regenerate input time base delta msec apart'
35 print '-s speed : speed change factor (default unchanged)'
36 print
37 print 'infile : input file (default film.video)'
38 print 'outfile : output file (default out.video)'
Guido van Rossum349f2b51992-12-24 11:41:14 +000039
Guido van Rossum5e044b71993-02-25 14:20:13 +000040
41import sys
42sys.path.append('/ufs/guido/src/video')
43
44import VFile
45import imgconv
46import imageop
47import getopt
48import string
49
50
51# Global options
52
53speed = 1.0
54mindelta = 0
55regen = None
56newpf = None
57newtype = None
58magnify = None
59newwidth = None
60newheight = None
61
62
63# Function to turn a string into a float
64
65atof_error = 'atof_error' # Exception if it fails
66
67def atof(s):
68 try:
69 return float(eval(s))
70 except:
71 raise atof_error
72
73
74# Main program -- mostly command line parsing
Guido van Rossum349f2b51992-12-24 11:41:14 +000075
76def main():
Guido van Rossum5e044b71993-02-25 14:20:13 +000077 global speed, mindelta, regen, newpf, newtype, \
78 magnify, newwidth, newheight
79
80 # Parse command line
81 try:
82 opts, args = getopt.getopt(sys.argv[1:], \
83 'M:h:m:p:r:s:t:w:x:y:')
84 except getopt.error, msg:
85 sys.stdout = sys.stderr
86 print 'Error:', msg, '\n'
Guido van Rossum349f2b51992-12-24 11:41:14 +000087 usage()
Guido van Rossum5e044b71993-02-25 14:20:13 +000088 sys.exit(2)
89
90 xpf = ypf = None
Guido van Rossum349f2b51992-12-24 11:41:14 +000091
Guido van Rossum5e044b71993-02-25 14:20:13 +000092 # Interpret options
93 try:
94 for opt, arg in opts:
95 if opt == '-M': magnify = atof(arg)
96 if opt == '-h': height = string.atoi(arg)
97 if opt == '-m': mindelta = string.atoi(arg)
98 if opt == '-p': xpf = ypf = string.atoi(arg)
99 if opt == '-r': regen = string.atoi(arg)
100 if opt == '-s': speed = atof(arg)
101 if opt == '-t': newtype = arg
102 if opt == '-w': newwidth = string.atoi(arg)
103 if opt == '-x': xpf = string.atoi(arg)
104 if opt == '-y': ypf = string.atoi(arg)
105 except string.atoi_error:
106 sys.stdout = sys.stderr
107 print 'Option', opt, 'requires integer argument'
108 sys.exit(2)
109 except atof_error:
110 sys.stdout = sys.stderr
111 print 'Option', opt, 'requires float argument'
112 sys.exit(2)
Guido van Rossum349f2b51992-12-24 11:41:14 +0000113
Guido van Rossum5e044b71993-02-25 14:20:13 +0000114 if xpf or ypf:
115 if not xpf: xpf = 1
116 if not ypf: ypf = 1
117 newpf = (xpf, ypf)
118
119 if newwidth or newheight:
120 if magnify:
121 sys.stdout = sys.stderr
122 print 'Options -w or -h are incompatible with -M'
123 sys.exit(2)
124 if not newheight:
125 newheight = newwidth * 3 / 4
126 elif not newwidth:
127 newwidth = newheight * 4 / 3
128
129 # Check filename arguments
130 if len(args) < 1:
131 args.append('film.video')
132 if len(args) < 2:
133 args.append('out.video')
134 if len(args) > 2:
135 usage()
136 sys.exit(2)
137 if args[0] == args[1]:
138 sys.stderr.write('Input file can\'t be output file\n')
139 sys.exit(2)
140
141 # Do the right thing
142 sts = process(args[0], args[1])
143
144 # Exit
145 sys.exit(sts)
146
147
148# Copy one file to another
149
150def process(infilename, outfilename):
151 global newwidth, newheight
152
153 try:
154 vin = VFile.BasicVinFile().init(infilename)
155 except IOError, msg:
156 sys.stderr.write(infilename + ': I/O error: ' + `msg` + '\n')
157 return 1
158 except VFile.Error, msg:
159 sys.stderr.write(msg + '\n')
160 return 1
161 except EOFError:
162 sys.stderr.write(infilename + ': EOF in video file\n')
163 return 1
164
165 try:
166 vout = VFile.BasicVoutFile().init(outfilename)
167 except IOError, msg:
168 sys.stderr.write(outfilename + ': I/O error: ' + `msg` + '\n')
169 return 1
170
171 vout.setinfo(vin.getinfo())
172
173 scale = 0
174 flip = 0
175
176 if newtype:
177 vout.setformat(newtype)
178 try:
179 convert = imgconv.getconverter(vin.format, vout.format)
180 except imgconv.error, msg:
181 sys.stderr.write(str(msg) + '\n')
182 return 1
183
184 if newpf:
185 vout.setpf(newpf)
186 scale = 1
187
188 if newwidth and newheight:
189 scale = 1
190
191 if vin.upside_down <> vout.upside_down or \
192 vin.mirror_image <> vout.mirror_image:
193 flip = 1
194
195 inwidth, inheight = vin.getsize()
196 inwidth = inwidth / vin.xpf
197 inheight = inheight / vin.ypf
198
199 if magnify:
200 newwidth = int(vout.width * magnify)
201 newheight = int(vout.height * magnify)
202 scale = 1
203
204 if scale:
205 vout.setsize(newwidth, newheight)
206 else:
207 newwidth, newheight = vout.getsize()
208
209 if vin.packfactor <> vout.packfactor:
210 scale = 1
211
212 if scale or flip:
213 if vout.bpp not in (8, 32):
214 sys.stderr.write('Can\'t scale or flip this type\n')
215 return 1
216
217 newwidth = newwidth / vout.xpf
218 newheight = newheight / vout.ypf
219
220 vout.writeheader()
221
222 told = 0
223 nin = 0
224 nout = 0
225 tin = 0
226 tout = 0
227
Guido van Rossum349f2b51992-12-24 11:41:14 +0000228 while 1:
Guido van Rossum5e044b71993-02-25 14:20:13 +0000229 try:
230 tin, data, cdata = vin.getnextframe()
231 except EOFError:
Guido van Rossum349f2b51992-12-24 11:41:14 +0000232 break
Guido van Rossum5e044b71993-02-25 14:20:13 +0000233 nin = nin + 1
234 if regen:
235 tout = nin * regen
Guido van Rossum349f2b51992-12-24 11:41:14 +0000236 else:
Guido van Rossum5e044b71993-02-25 14:20:13 +0000237 tout = tin
238 tout = int(tout / speed)
239 if tout - told < mindelta:
240 continue
241 told = tout
242 if newtype:
243 data = convert(data, inwidth, inheight)
244 if newwidth and newheight:
245 data = imageop.scale(data, vout.bpp/8, \
246 inwidth, inheight, newwidth, newheight)
247 if vin.upside_down <> vout.upside_down or \
248 vin.mirror_image <> vout.mirror_image:
249 x0, y0 = 0, 0
250 x1, y1 = newwidth-1, neheight-1
251 if vin.upside_down <> vout.upside_down:
252 y1, y0 = y0, y1
253 if vin.mirror_image <> vout.mirror_image:
254 x1, x0 = x0, x1
255 data = imageop.crop(data, vout.bpp/8, \
256 newwidth, newheight, x0, y0, x1, y1)
257 vout.writeframe(tout, data, cdata)
258 nout = nout + 1
Guido van Rossum349f2b51992-12-24 11:41:14 +0000259
Guido van Rossum5e044b71993-02-25 14:20:13 +0000260 vout.close()
261 vin.close()
262
263
264# Don't forget to call the main program
265
266try:
267 main()
268except KeyboardInterrupt:
269 print '[Interrupt]'