Ditched '_find_SET()', since it was a no-value-added wrapper around
[python/dscho.git] / Demo / sgi / video / Vmkjpeg.py
blob0c9687a8c5c5fbcd5086d7d6ff905d50f834a65e
1 #! /usr/bin/env python
3 # Compress an rgb or grey video file to jpeg format
6 # Usage:
8 # Vmkjpeg [infile [outfile]]
11 # Options:
13 # infile : input file (default film.video)
14 # outfile : output file (default out.video)
17 import sys
18 import jpeg
19 sys.path.append('/ufs/guido/src/video')
20 import VFile
23 # Main program -- mostly command line parsing
25 def main():
26 args = sys.argv[1:]
27 if len(args) < 1:
28 args.append('film.video')
29 if len(args) < 2:
30 args.append('out.video')
31 if len(args) > 2:
32 sys.stderr.write('usage: Vmkjpeg [infile [outfile]]\n')
33 sys.exit(2)
34 sts = process(args[0], args[1])
35 sys.exit(sts)
38 # Copy one file to another
40 def process(infilename, outfilename):
41 try:
42 vin = VFile.BasicVinFile(infilename)
43 except IOError, msg:
44 sys.stderr.write(infilename + ': I/O error: ' + `msg` + '\n')
45 return 1
46 except VFile.Error, msg:
47 sys.stderr.write(msg + '\n')
48 return 1
49 except EOFError:
50 sys.stderr.write(infilename + ': EOF in video file\n')
51 return 1
53 try:
54 vout = VFile.BasicVoutFile(outfilename)
55 except IOError, msg:
56 sys.stderr.write(outfilename + ': I/O error: ' + `msg` + '\n')
57 return 1
59 info = vin.getinfo()
60 if info[0] == 'rgb':
61 width, height = vin.getsize()
62 bytes = 4
63 format = 'jpeg'
64 elif info[0] == 'grey':
65 width, height = vin.getsize()
66 pf = vin.packfactor
67 width, height = width / pf, height / pf
68 bytes = 1
69 format = 'jpeggrey'
70 else:
71 sys.stderr.write('Vmkjpeg: input not in rgb or grey format\n')
72 return 1
73 info = (format,) + info[1:]
74 vout.setinfo(info)
75 vout.writeheader()
76 n = 0
77 try:
78 while 1:
79 t, data, cdata = vin.getnextframe()
80 n = n + 1
81 sys.stderr.write('Frame ' + `n` + '...')
82 data = jpeg.compress(data, width, height, bytes)
83 vout.writeframe(t, data, None)
84 sys.stderr.write('\n')
85 except EOFError:
86 pass
87 return 0
90 # Don't forget to call the main program
92 main()