home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Demo / sgi / video / Vunjpeg.py < prev    next >
Text File  |  1996-11-27  |  2KB  |  98 lines

  1. #! /usr/bin/env python
  2.  
  3. # Decompress a jpeg or jpeggrey video file to rgb format
  4.  
  5.  
  6. # Usage:
  7. #
  8. # Vunjpeg [infile [outfile]]
  9.  
  10.  
  11. # Options:
  12. #
  13. # infile     : input file (default film.video)
  14. # outfile    : output file (default out.video)
  15.  
  16.  
  17. import sys
  18. import jpeg
  19. sys.path.append('/ufs/guido/src/video')
  20. import VFile
  21.  
  22.  
  23. # Main program -- mostly command line parsing
  24.  
  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)
  36.  
  37.  
  38. # Copy one file to another
  39.  
  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
  52.  
  53.     try:
  54.         vout = VFile.BasicVoutFile(outfilename)
  55.     except IOError, msg:
  56.         sys.stderr.write(outfilename + ': I/O error: ' + `msg` + '\n')
  57.         return 1
  58.  
  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
  93.  
  94.  
  95. # Don't forget to call the main program
  96.  
  97. main()
  98.