/[escript]/trunk/tools/vizTools/make_movie.py
ViewVC logotype

Contents of /trunk/tools/vizTools/make_movie.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 371 - (show annotations)
Fri Dec 16 00:57:13 2005 UTC (13 years, 8 months ago) by cochrane
File MIME type: text/x-python
File size: 4390 byte(s)
Added ability to specify the compression pattern at the command line.

1 #!/usr/bin/env python
2
3 # $Id$
4
5 """
6 Make an mpeg movie from the pnm files in the specified directory
7 """
8
9 import os, sys, re
10 import getopt
11
12 (opts, args) = getopt.getopt(sys.argv[1:],
13 "s:d:o:f:p:t:h",
14 ["framestem=", "dirname=", "output=", "format=", "pad=", "pattern=",
15 "help"],
16 )
17
18 def usage():
19 print "Usage:"
20 print " make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n"
21 print " Arguments:"
22 print " -s/--framestem: The frame filename stem (the stuff before .pnm) (required)"
23 print " -d/--dirname: The directory of frames (optional)"
24 print " -o/--output: Output mpeg filename (optional)"
25 print " -f/--format: Input frame image format (optional)"
26 print " -p/--pad: How many frames to pad the movie (optional)"
27 print " -h/--help: Display this information (optional)"
28
29 dirname = "./"
30 mpegName = None
31 fnameStem = None
32 format = "pnm" # if format not specified assume pnm
33 pad = 1
34 pattern = "IBBPBBPBB"
35
36 for option, arg in opts:
37 if option in ('-s', '--stem'):
38 fnameStem = arg
39 elif option in ('-d', '--dirname'):
40 dirname = arg
41 elif option in ('-o', '--output'):
42 mpegName = arg
43 elif option in ('-f', '--format'):
44 format = arg
45 elif option in ('-p', '--pad'):
46 pad = int(arg)
47 elif option in ('-t', '--pattern'):
48 pattern = arg
49 elif option in ('-h', '--help'):
50 usage()
51 sys.exit(0)
52
53 if fnameStem is None:
54 print "You must supply the stem of the frame filenames\n"
55 usage()
56 sys.exit(0)
57 if mpegName is None:
58 mpegName = fnameStem + ".mpg"
59
60 # determine the number of files to convert
61 dirList = os.listdir(dirname)
62 r = re.compile( "%s\\d+\\.%s"%(fnameStem,format) )
63 count = 0 # counter for the number of files found
64 fnames = []
65 for fname in dirList:
66 if r.match(fname):
67 fnames.append(fname)
68 count += 1
69
70 if count == 0:
71 raise ValueError, "No matching files found"
72
73 # do a conversion if necessary
74 if format != "pnm":
75 print "Converting frames to pnm"
76 r = re.compile("%s\\d+\\." % (fnameStem))
77 for fname in fnames:
78 # strip off the bit after the last dot
79 front = r.findall(fname)
80 front = front[0]
81 # make the conversion string to pass to 'convert'
82 convString = "convert %s/%s%s %s/%spnm" % \
83 (dirname, front, format, dirname, front)
84 retVal = os.system(convString)
85 if retVal != 0:
86 raise SystemError, "Conversion of %s%s failed" % (front, format)
87
88 # use ppmtompeg to convert the pnms to a movie
89 print "Making movie"
90
91 # now automatically generate the ppmtompeg params file
92 print "Generating params file..."
93
94 # old pattern
95 # PATTERN IBBBPBBBPBBBPBBBPBBB
96
97 paramsFileString = """REFERENCE_FRAME DECODED
98 FRAME_RATE 24
99 OUTPUT %s
100 PATTERN %s
101 FORCE_ENCODE_LAST_FRAME
102 GOP_SIZE 20
103 BSEARCH_ALG CROSS2
104 PSEARCH_ALG TWOLEVEL
105 IQSCALE 10
106 PQSCALE 11
107 BQSCALE 16
108 RANGE 8
109 SLICES_PER_FRAME 1
110 BASE_FILE_FORMAT PNM
111 INPUT_DIR .
112 INPUT_CONVERT *
113 INPUT
114 """ % (mpegName, pattern)
115
116 # need to determine the first number and last number of the list of files
117 # sort the files first just in case
118 fnames.sort()
119 firstFile = fnames[0]
120 lastFile = fnames[-1]
121
122 r = re.compile("([a-zA-Z-_\.\d])(\\d+)(\\.\\w+)$")
123 firstNum = r.findall(firstFile)
124 firstNum = firstNum[0][1]
125 lastNum = r.findall(lastFile)
126 lastNum = lastNum[0][1]
127
128
129 # finish off the params file string
130 if pad == 1:
131 paramsFileString += "%s/%s*.pnm [%s-%s]\n" % \
132 (dirname, fnameStem, firstNum, lastNum)
133 elif pad > 1:
134 # positive padding: add duplicate frames (slow the movie down)
135 for i in range(int(firstNum), int(lastNum)+1):
136 for j in range(pad):
137 paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i)
138 elif pad < 1:
139 # negative padding: i.e. remove frames (speed the movie up)
140 for i in range(int(firstNum), int(lastNum)+1, abs(pad)):
141 paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i)
142
143 paramsFileString += """END_INPUT
144 PIXEL HALF
145 ASPECT_RATIO 1
146 """
147
148 # write the string to file
149 fp = open( "%s/%s.params" % (dirname,fnameStem,), "w" )
150 fp.write(paramsFileString + '\n')
151 fp.close()
152 print "Done params file generation"
153
154 # now do the conversion to mpeg
155 print "Performing conversion to mpeg"
156 convertString = "ppmtompeg %s/%s.params" % (dirname, fnameStem)
157 result = os.system(convertString)
158 if result != 0:
159 print "An error occurred in mpeg conversion"
160
161 # now clean up a bit
162 #for i in range(count):
163 # rmFname = "%s_%04d.ppm" % (fnameStem,i)
164 # os.unlink(rmFname)
165
166 print "\nDone!"
167

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26