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

  1. #! /usr/bin/env python
  2.  
  3. # Compress an rgb or grey video file to jpeg format
  4.  
  5.  
  6. # Usage:
  7. #
  8. # Vmkjpeg [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: Vmkjpeg [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] == '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
  88.  
  89.  
  90. # Don't forget to call the main program
  91.  
  92. main()
  93.