/[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 149 - (hide annotations)
Thu Sep 1 03:31:39 2005 UTC (15 years, 3 months ago) by jgs
Original Path: trunk/esys2/tools/vizTools/make_movie.py
File MIME type: text/x-python
File size: 4205 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-09-01

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26