Rewrite to use test_support's fine fcmp instead -- I didn't know that
[python/dscho.git] / Demo / sgi / video / Vtime.py
blob651deee00d3b66e6745863adb01df38ca2443f6c
1 #! /usr/bin/env python
3 # Manipulate the time base of CMIF movies
6 # Possibilities:
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 accommodate slow players)
13 # Usage:
15 # Vtime [-m msec] [-r msec] [-s speed] [infile [outfile]]
18 # Options:
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)
27 import sys
28 sys.path.append('/ufs/guido/src/video')
29 import VFile
30 import getopt
31 import string
34 # Global options
36 speed = 1.0
37 mindelta = 0
38 regen = None
41 # Main program -- mostly command line parsing
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)
64 # Copy one file to another
66 def process(infilename, outfilename):
67 try:
68 vin = VFile.BasicVinFile(infilename)
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
79 try:
80 vout = VFile.BasicVoutFile(outfilename)
81 except IOError, msg:
82 sys.stderr.write(outfilename + ': I/O error: ' + `msg` + '\n')
83 return 1
85 vout.setinfo(vin.getinfo())
86 vout.writeheader()
88 told = 0
89 nin = 0
90 nout = 0
91 tin = 0
92 tout = 0
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
111 vout.close()
112 vin.close()
115 # Don't forget to call the main program
117 main()