1996-11-27 15:52:01 -04:00
|
|
|
#! /usr/bin/env python
|
1993-02-17 11:52:56 -04:00
|
|
|
|
1993-02-18 14:09:18 -04:00
|
|
|
# Convert CMIF movie file(s) to a sequence of rgb images
|
1993-02-17 11:52:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Help function
|
|
|
|
|
|
|
|
def help():
|
1993-02-18 14:09:18 -04:00
|
|
|
print 'Usage: video2rgb [options] [file] ...'
|
1993-02-17 11:52:56 -04:00
|
|
|
print
|
|
|
|
print 'Options:'
|
|
|
|
print '-q : quiet, no informative messages'
|
|
|
|
print '-m : create monochrome (greyscale) image files'
|
1993-02-18 14:09:18 -04:00
|
|
|
print '-f prefix : create image files with names "prefix0000.rgb"'
|
|
|
|
print 'file ... : file(s) to convert; default film.video'
|
1993-02-17 11:52:56 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Imported modules
|
|
|
|
|
|
|
|
import sys
|
1993-02-19 06:06:28 -04:00
|
|
|
sys.path.append('/ufs/jack/src/av/video') # Increase chance of finding VFile
|
1993-02-17 11:52:56 -04:00
|
|
|
import VFile
|
|
|
|
import time
|
|
|
|
import getopt
|
|
|
|
import string
|
|
|
|
import imgfile
|
|
|
|
import imgconv
|
|
|
|
|
|
|
|
|
|
|
|
# Global options
|
|
|
|
|
|
|
|
quiet = 0
|
|
|
|
prefix = 'film'
|
|
|
|
seqno = 0
|
|
|
|
mono = 0
|
|
|
|
|
|
|
|
|
|
|
|
# Main program -- mostly command line parsing
|
|
|
|
|
|
|
|
def main():
|
|
|
|
global quiet, prefix, mono
|
|
|
|
|
|
|
|
# Parse command line
|
|
|
|
try:
|
|
|
|
opts, args = getopt.getopt(sys.argv[1:], 'qmf:')
|
|
|
|
except getopt.error, msg:
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print 'Error:', msg, '\n'
|
|
|
|
help()
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
# Interpret options
|
|
|
|
try:
|
|
|
|
for opt, arg in opts:
|
|
|
|
if opt == '-q': quiet = 1
|
|
|
|
if opt == '-f': prefix = arg
|
|
|
|
if opt == '-m': mono = 1
|
|
|
|
except string.atoi_error:
|
|
|
|
sys.stdout = sys.stderr
|
|
|
|
print 'Option', opt, 'requires integer argument'
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
# Process all files
|
|
|
|
if not args: args = ['film.video']
|
|
|
|
sts = 0
|
|
|
|
for filename in args:
|
|
|
|
sts = (process(filename) or sts)
|
|
|
|
|
|
|
|
# Exit with proper exit status
|
|
|
|
sys.exit(sts)
|
|
|
|
|
|
|
|
|
|
|
|
# Process one movie file
|
|
|
|
|
|
|
|
def process(filename):
|
|
|
|
try:
|
1993-12-17 11:11:41 -04:00
|
|
|
vin = VFile.VinFile(filename)
|
1993-02-17 11:52:56 -04:00
|
|
|
except IOError, msg:
|
|
|
|
sys.stderr.write(filename + ': I/O error: ' + `msg` + '\n')
|
|
|
|
return 1
|
|
|
|
except VFile.Error, msg:
|
|
|
|
sys.stderr.write(msg + '\n')
|
|
|
|
return 1
|
|
|
|
except EOFError:
|
|
|
|
sys.stderr.write(filename + ': EOF in video header\n')
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if not quiet:
|
|
|
|
vin.printinfo()
|
|
|
|
|
|
|
|
width, height = int(vin.width), int(vin.height)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if mono:
|
|
|
|
cf = imgconv.getconverter(vin.format, 'grey')
|
|
|
|
else:
|
|
|
|
cf = imgconv.getconverter(vin.format, 'rgb')
|
|
|
|
except imgconv.error:
|
|
|
|
print 'Sorry, no converter available for type',vin.format
|
|
|
|
return
|
|
|
|
|
|
|
|
if mono:
|
|
|
|
depth = 1
|
1993-02-24 20:19:14 -04:00
|
|
|
bpp = 1
|
1993-02-17 11:52:56 -04:00
|
|
|
else:
|
|
|
|
depth = 3
|
1993-02-24 20:19:14 -04:00
|
|
|
bpp = 4
|
1993-02-17 11:52:56 -04:00
|
|
|
|
1993-02-24 20:19:14 -04:00
|
|
|
convert(vin, cf, width, height, depth, bpp, vin.packfactor)
|
1993-02-17 11:52:56 -04:00
|
|
|
|
1993-02-24 20:19:14 -04:00
|
|
|
def convert(vin, cf, width, height, depth, bpp, pf):
|
1993-02-17 11:52:56 -04:00
|
|
|
global seqno
|
|
|
|
|
1993-02-19 06:06:28 -04:00
|
|
|
if type(pf) == type(()):
|
|
|
|
xpf, ypf = pf
|
|
|
|
elif pf == 0:
|
|
|
|
xpf = ypf = 1
|
|
|
|
else:
|
|
|
|
xpf = ypf = pf
|
1993-02-17 11:52:56 -04:00
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
time, data, cdata = vin.getnextframe()
|
|
|
|
except EOFError:
|
|
|
|
return
|
|
|
|
if cdata:
|
|
|
|
print 'Film contains chromdata!'
|
|
|
|
return
|
1993-02-19 06:06:28 -04:00
|
|
|
data = cf(data, width/xpf, height/abs(ypf))
|
1993-02-17 11:52:56 -04:00
|
|
|
if pf:
|
1993-02-24 20:19:14 -04:00
|
|
|
data = applypackfactor(data, width, height, pf, bpp)
|
1993-02-17 11:52:56 -04:00
|
|
|
s = `seqno`
|
|
|
|
s = '0'*(4-len(s)) + s
|
|
|
|
fname = prefix + s + '.rgb'
|
|
|
|
seqno = seqno + 1
|
|
|
|
if not quiet:
|
|
|
|
print 'Writing',fname,'...'
|
|
|
|
imgfile.write(fname, data, width, height, depth)
|
|
|
|
|
1993-02-24 20:19:14 -04:00
|
|
|
def applypackfactor(image, w, h, pf, bpp):
|
|
|
|
import imageop
|
1993-02-17 11:52:56 -04:00
|
|
|
if type(pf) == type(()):
|
|
|
|
xpf, ypf = pf
|
1993-02-24 20:19:14 -04:00
|
|
|
elif pf == 0:
|
1993-02-17 11:52:56 -04:00
|
|
|
xpf = ypf = 1
|
1993-02-24 20:19:14 -04:00
|
|
|
else:
|
|
|
|
xpf = ypf = pf
|
|
|
|
w1 = w/xpf
|
|
|
|
h1 = h/abs(ypf)
|
1993-02-17 11:52:56 -04:00
|
|
|
if ypf < 0:
|
|
|
|
ypf = -ypf
|
1993-02-24 20:19:14 -04:00
|
|
|
image = imageop.crop(image, bpp, w1, h1, 0, h1-1, w1-1, 0)
|
|
|
|
return imageop.scale(image, bpp, w1, h1, w, h)
|
1993-02-17 11:52:56 -04:00
|
|
|
|
|
|
|
# Don't forget to call the main program
|
|
|
|
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print '[Interrupt]'
|