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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26