blob: bbfa6b823851d3b9c6fc744a61882c065c74843c [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Jack Janseneeec33f1993-02-17 15:52:56 +00002
Guido van Rossum245be3a1993-02-18 18:09:18 +00003# Convert CMIF movie file(s) to a sequence of rgb images
Jack Janseneeec33f1993-02-17 15:52:56 +00004
5
6# Help function
7
8def help():
Guido van Rossum245be3a1993-02-18 18:09:18 +00009 print 'Usage: video2rgb [options] [file] ...'
Jack Janseneeec33f1993-02-17 15:52:56 +000010 print
11 print 'Options:'
12 print '-q : quiet, no informative messages'
13 print '-m : create monochrome (greyscale) image files'
Guido van Rossum245be3a1993-02-18 18:09:18 +000014 print '-f prefix : create image files with names "prefix0000.rgb"'
15 print 'file ... : file(s) to convert; default film.video'
Jack Janseneeec33f1993-02-17 15:52:56 +000016
17
18# Imported modules
19
20import sys
Jack Jansenc82cfc81993-02-19 10:06:28 +000021sys.path.append('/ufs/jack/src/av/video') # Increase chance of finding VFile
Jack Janseneeec33f1993-02-17 15:52:56 +000022import VFile
23import time
24import getopt
25import string
26import imgfile
27import imgconv
28
29
30# Global options
31
32quiet = 0
33prefix = 'film'
34seqno = 0
35mono = 0
36
37
38# Main program -- mostly command line parsing
39
40def 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
75def process(filename):
76 try:
Guido van Rossum21a3ff91993-12-17 15:11:41 +000077 vin = VFile.VinFile(filename)
Jack Janseneeec33f1993-02-17 15:52:56 +000078 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
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000104 bpp = 1
Jack Janseneeec33f1993-02-17 15:52:56 +0000105 else:
106 depth = 3
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000107 bpp = 4
Jack Janseneeec33f1993-02-17 15:52:56 +0000108
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000109 convert(vin, cf, width, height, depth, bpp, vin.packfactor)
Jack Janseneeec33f1993-02-17 15:52:56 +0000110
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000111def convert(vin, cf, width, height, depth, bpp, pf):
Jack Janseneeec33f1993-02-17 15:52:56 +0000112 global seqno
113
Jack Jansenc82cfc81993-02-19 10:06:28 +0000114 if type(pf) == type(()):
115 xpf, ypf = pf
116 elif pf == 0:
117 xpf = ypf = 1
118 else:
119 xpf = ypf = pf
Jack Janseneeec33f1993-02-17 15:52:56 +0000120 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
Jack Jansenc82cfc81993-02-19 10:06:28 +0000128 data = cf(data, width/xpf, height/abs(ypf))
Jack Janseneeec33f1993-02-17 15:52:56 +0000129 if pf:
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000130 data = applypackfactor(data, width, height, pf, bpp)
Jack Janseneeec33f1993-02-17 15:52:56 +0000131 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)
Jack Janseneeec33f1993-02-17 15:52:56 +0000138
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000139def applypackfactor(image, w, h, pf, bpp):
140 import imageop
Jack Janseneeec33f1993-02-17 15:52:56 +0000141 if type(pf) == type(()):
142 xpf, ypf = pf
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000143 elif pf == 0:
Jack Janseneeec33f1993-02-17 15:52:56 +0000144 xpf = ypf = 1
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000145 else:
146 xpf = ypf = pf
147 w1 = w/xpf
148 h1 = h/abs(ypf)
Jack Janseneeec33f1993-02-17 15:52:56 +0000149 if ypf < 0:
Jack Janseneeec33f1993-02-17 15:52:56 +0000150 ypf = -ypf
Guido van Rossumb616ebe1993-02-25 00:19:14 +0000151 image = imageop.crop(image, bpp, w1, h1, 0, h1-1, w1-1, 0)
152 return imageop.scale(image, bpp, w1, h1, w, h)
Jack Janseneeec33f1993-02-17 15:52:56 +0000153
154# Don't forget to call the main program
155
156try:
157 main()
158except KeyboardInterrupt:
159 print '[Interrupt]'