changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Demo / sgi / video / Vunjpeg.py
blob9f21f959ffb66492c54b7ed4c70f3765cba29f5e
1 #!/ufs/guido/bin/sgi/python
3 # Decompress a jpeg or jpeggrey video file to rgb format
6 # Usage:
8 # Vunjpeg [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: Vunjpeg [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] == 'jpeg':
61 format = 'rgb'
62 width, height = vin.getsize()
63 bytes = 4
64 elif info[0] == 'jpeggrey':
65 format = 'grey'
66 width, height = vin.getsize()
67 pf = vin.packfactor
68 width, height = width/pf, height/pf
69 bytes = 1
70 else:
71 sys.stderr.write('Vunjpeg: input not in jpeg[grey] format\n')
72 return 1
73 info = (format,) + info[1:]
74 vout.setinfo(info)
75 vout.writeheader()
76 sts = 0
77 n = 0
78 try:
79 while 1:
80 t, data, cdata = vin.getnextframe()
81 n = n + 1
82 sys.stderr.write('Frame ' + `n` + '...')
83 data, w, h, b = jpeg.decompress(data)
84 if (w, h, b) <> (width, height, bytes):
85 sys.stderr.write('jpeg data has wrong size\n')
86 sts = 1
87 else:
88 vout.writeframe(t, data, None)
89 sys.stderr.write('\n')
90 except EOFError:
91 pass
92 return sts
95 # Don't forget to call the main program
97 main()