1 |
|
2 |
######################################################## |
3 |
# |
4 |
# Copyright (c) 2003-2008 by University of Queensland |
5 |
# Earth Systems Science Computational Center (ESSCC) |
6 |
# http://www.uq.edu.au/esscc |
7 |
# |
8 |
# 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 |
# |
12 |
######################################################## |
13 |
|
14 |
__copyright__="""Copyright (c) 2003-2008 by University of Queensland |
15 |
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 |
__url__="http://www.uq.edu.au/esscc/escript-finley" |
21 |
|
22 |
|
23 |
""" |
24 |
Make an mpeg movie from the pnm files in the specified directory |
25 |
""" |
26 |
|
27 |
import os, sys, re |
28 |
import getopt |
29 |
|
30 |
(opts, args) = getopt.getopt(sys.argv[1:], |
31 |
"s:d:o:f:p:t:h", |
32 |
["framestem=", "dirname=", "output=", "format=", "pad=", "pattern=", |
33 |
"help"], |
34 |
) |
35 |
|
36 |
def usage(): |
37 |
print "Usage:" |
38 |
print " make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n" |
39 |
print " Arguments:" |
40 |
print " -s/--framestem: The frame filename stem (the stuff before .pnm) (required)" |
41 |
print " -d/--dirname: The directory of frames (optional)" |
42 |
print " -o/--output: Output mpeg filename (optional)" |
43 |
print " -f/--format: Input frame image format (optional)" |
44 |
print " -p/--pad: How many frames to pad the movie (optional)" |
45 |
print " -h/--help: Display this information (optional)" |
46 |
|
47 |
dirname = "./" |
48 |
mpegName = None |
49 |
fnameStem = None |
50 |
format = "pnm" # if format not specified assume pnm |
51 |
pad = 1 |
52 |
pattern = "IBBPBBPBB" |
53 |
|
54 |
for option, arg in opts: |
55 |
if option in ('-s', '--stem'): |
56 |
fnameStem = arg |
57 |
elif option in ('-d', '--dirname'): |
58 |
dirname = arg |
59 |
elif option in ('-o', '--output'): |
60 |
mpegName = arg |
61 |
elif option in ('-f', '--format'): |
62 |
format = arg |
63 |
elif option in ('-p', '--pad'): |
64 |
pad = int(arg) |
65 |
elif option in ('-t', '--pattern'): |
66 |
pattern = arg |
67 |
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 |
dirList = os.listdir(dirname) |
80 |
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 |
if count == 0: |
89 |
raise ValueError, "No matching files found" |
90 |
|
91 |
# 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 |
convString = "convert %s/%s%s %s/%spnm" % \ |
101 |
(dirname, front, format, dirname, front) |
102 |
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 |
PATTERN %s |
119 |
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 |
""" % (mpegName, pattern) |
133 |
|
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 |
r = re.compile("([a-zA-Z-_\.\d])(\\d+)(\\.\\w+)$") |
141 |
firstNum = r.findall(firstFile) |
142 |
firstNum = firstNum[0][1] |
143 |
lastNum = r.findall(lastFile) |
144 |
lastNum = lastNum[0][1] |
145 |
|
146 |
|
147 |
# finish off the params file string |
148 |
if pad == 1: |
149 |
paramsFileString += "%s/%s*.pnm [%s-%s]\n" % \ |
150 |
(dirname, fnameStem, firstNum, lastNum) |
151 |
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 |
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
156 |
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 |
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
160 |
|
161 |
paramsFileString += """END_INPUT |
162 |
PIXEL HALF |
163 |
ASPECT_RATIO 1 |
164 |
""" |
165 |
|
166 |
# write the string to file |
167 |
fp = open( "%s/%s.params" % (dirname,fnameStem,), "w" ) |
168 |
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 |
convertString = "ppmtompeg %s/%s.params" % (dirname, fnameStem) |
175 |
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 |
|