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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26