Guido van Rossum | f06ee5f | 1996-11-27 19:52:01 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
Guido van Rossum | 99e237f | 1992-08-20 11:53:14 +0000 | [diff] [blame] | 2 | |
| 3 | # Manipulate the time base of CMIF movies |
| 4 | |
| 5 | |
| 6 | # Possibilities: |
| 7 | # |
| 8 | # - resample at a fixed rate |
| 9 | # - divide the time codes by a speed factor (to make it go faster/slower) |
| 10 | # - drop frames that are less than n msec apart (to accomodate slow players) |
| 11 | |
| 12 | |
| 13 | # Usage: |
| 14 | # |
| 15 | # Vtime [-m msec] [-r msec] [-s speed] [infile [outfile]] |
| 16 | |
| 17 | |
| 18 | # Options: |
| 19 | # |
| 20 | # -m n : drop frames closer than n msec (default 0) |
| 21 | # -r n : regenerate input time base n msec apart |
| 22 | # -s speed : speed change factor after other processing (default 1.0) |
| 23 | # infile : input file (default film.video) |
| 24 | # outfile : output file (default out.video) |
| 25 | |
| 26 | |
| 27 | import sys |
| 28 | sys.path.append('/ufs/guido/src/video') |
| 29 | import VFile |
| 30 | import getopt |
| 31 | import string |
| 32 | |
| 33 | |
| 34 | # Global options |
| 35 | |
| 36 | speed = 1.0 |
| 37 | mindelta = 0 |
| 38 | regen = None |
| 39 | |
| 40 | |
| 41 | # Main program -- mostly command line parsing |
| 42 | |
| 43 | def main(): |
| 44 | global speed, mindelta |
| 45 | opts, args = getopt.getopt(sys.argv[1:], 'm:r:s:') |
| 46 | for opt, arg in opts: |
| 47 | if opt == '-m': |
| 48 | mindelta = string.atoi(arg) |
| 49 | elif opt == '-r': |
| 50 | regen = string.atoi(arg) |
| 51 | elif opt == '-s': |
| 52 | speed = float(eval(arg)) |
| 53 | if len(args) < 1: |
| 54 | args.append('film.video') |
| 55 | if len(args) < 2: |
| 56 | args.append('out.video') |
| 57 | if len(args) > 2: |
| 58 | sys.stderr.write('usage: Vtime [options] [infile [outfile]]\n') |
| 59 | sys.exit(2) |
| 60 | sts = process(args[0], args[1]) |
| 61 | sys.exit(sts) |
| 62 | |
| 63 | |
| 64 | # Copy one file to another |
| 65 | |
| 66 | def process(infilename, outfilename): |
| 67 | try: |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 68 | vin = VFile.BasicVinFile(infilename) |
Guido van Rossum | 99e237f | 1992-08-20 11:53:14 +0000 | [diff] [blame] | 69 | except IOError, msg: |
| 70 | sys.stderr.write(infilename + ': I/O error: ' + `msg` + '\n') |
| 71 | return 1 |
| 72 | except VFile.Error, msg: |
| 73 | sys.stderr.write(msg + '\n') |
| 74 | return 1 |
| 75 | except EOFError: |
| 76 | sys.stderr.write(infilename + ': EOF in video file\n') |
| 77 | return 1 |
| 78 | |
| 79 | try: |
Guido van Rossum | 21a3ff9 | 1993-12-17 15:11:41 +0000 | [diff] [blame] | 80 | vout = VFile.BasicVoutFile(outfilename) |
Guido van Rossum | 99e237f | 1992-08-20 11:53:14 +0000 | [diff] [blame] | 81 | except IOError, msg: |
| 82 | sys.stderr.write(outfilename + ': I/O error: ' + `msg` + '\n') |
| 83 | return 1 |
| 84 | |
| 85 | vout.setinfo(vin.getinfo()) |
| 86 | vout.writeheader() |
| 87 | |
| 88 | told = 0 |
| 89 | nin = 0 |
| 90 | nout = 0 |
| 91 | tin = 0 |
| 92 | tout = 0 |
| 93 | |
| 94 | while 1: |
| 95 | try: |
| 96 | tin, data, cdata = vin.getnextframe() |
| 97 | except EOFError: |
| 98 | break |
| 99 | nin = nin + 1 |
| 100 | if regen: |
| 101 | tout = nin * regen |
| 102 | else: |
| 103 | tout = tin |
| 104 | tout = int(tout / speed) |
| 105 | if tout - told < mindelta: |
| 106 | continue |
| 107 | told = tout |
| 108 | vout.writeframe(tout, data, cdata) |
| 109 | nout = nout + 1 |
| 110 | |
| 111 | vout.close() |
| 112 | vin.close() |
| 113 | |
| 114 | |
| 115 | # Don't forget to call the main program |
| 116 | |
| 117 | main() |