3 |
# $Id$ |
# $Id$ |
4 |
|
|
5 |
""" |
""" |
6 |
Make an mpeg movie from the pnm files in the current directory |
Make an mpeg movie from the pnm files in the specified directory |
7 |
""" |
""" |
8 |
|
|
9 |
import os, sys, re |
import os, sys, re |
10 |
import getopt |
import getopt |
11 |
|
|
12 |
(opts, args) = getopt.getopt(sys.argv[1:], |
(opts, args) = getopt.getopt(sys.argv[1:], |
13 |
"s:o:f:p:h", |
"s:d:o:f:p:h", |
14 |
["framestem=", "output=", "format=", "pad=", "help"], |
["framestem=", "dirname=", "output=", "format=", "pad=", "help"], |
15 |
) |
) |
16 |
|
|
17 |
def usage(): |
def usage(): |
19 |
print " make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n" |
print " make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n" |
20 |
print " Arguments:" |
print " Arguments:" |
21 |
print " -s/--framestem: The frame filename stem (the stuff before .pnm) (required)" |
print " -s/--framestem: The frame filename stem (the stuff before .pnm) (required)" |
22 |
|
print " -d/--dirname: The directory of frames (optional)" |
23 |
print " -o/--output: Output mpeg filename (optional)" |
print " -o/--output: Output mpeg filename (optional)" |
24 |
print " -f/--format: Input frame image format (optional)" |
print " -f/--format: Input frame image format (optional)" |
25 |
print " -p/--pad: How many frames to pad the movie (optional)" |
print " -p/--pad: How many frames to pad the movie (optional)" |
26 |
|
|
27 |
|
dirname = "./" |
28 |
mpegName = None |
mpegName = None |
29 |
fnameStem = None |
fnameStem = None |
30 |
format = "pnm" # if format not specified assume pnm |
format = "pnm" # if format not specified assume pnm |
33 |
for option, arg in opts: |
for option, arg in opts: |
34 |
if option in ('-s', '--stem'): |
if option in ('-s', '--stem'): |
35 |
fnameStem = arg |
fnameStem = arg |
36 |
|
elif option in ('-d', '--dirname'): |
37 |
|
dirname = arg |
38 |
elif option in ('-o', '--output'): |
elif option in ('-o', '--output'): |
39 |
mpegName = arg |
mpegName = arg |
40 |
elif option in ('-f', '--format'): |
elif option in ('-f', '--format'): |
53 |
mpegName = fnameStem + ".mpg" |
mpegName = fnameStem + ".mpg" |
54 |
|
|
55 |
# determine the number of files to convert |
# determine the number of files to convert |
56 |
dirList = os.listdir('./') |
dirList = os.listdir(dirname) |
57 |
r = re.compile( "%s\\d+\\.%s"%(fnameStem,format) ) |
r = re.compile( "%s\\d+\\.%s"%(fnameStem,format) ) |
58 |
count = 0 # counter for the number of files found |
count = 0 # counter for the number of files found |
59 |
fnames = [] |
fnames = [] |
122 |
|
|
123 |
# finish off the params file string |
# finish off the params file string |
124 |
if pad == 1: |
if pad == 1: |
125 |
paramsFileString += "%s*.pnm [%s-%s]\n" % (fnameStem, firstNum, lastNum) |
paramsFileString += "%s/%s*.pnm [%s-%s]\n" % \ |
126 |
|
(dirname, fnameStem, firstNum, lastNum) |
127 |
elif pad > 1: |
elif pad > 1: |
128 |
# positive padding: add duplicate frames (slow the movie down) |
# positive padding: add duplicate frames (slow the movie down) |
129 |
for i in range(int(firstNum), int(lastNum)+1): |
for i in range(int(firstNum), int(lastNum)+1): |
130 |
for j in range(pad): |
for j in range(pad): |
131 |
paramsFileString += "%s%04d.pnm\n" % (fnameStem, i) |
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
132 |
elif pad < 1: |
elif pad < 1: |
133 |
# negative padding: i.e. remove frames (speed the movie up) |
# negative padding: i.e. remove frames (speed the movie up) |
134 |
for i in range(int(firstNum), int(lastNum)+1, abs(pad)): |
for i in range(int(firstNum), int(lastNum)+1, abs(pad)): |
135 |
paramsFileString += "%s%04d.pnm\n" % (fnameStem, i) |
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
136 |
|
|
137 |
paramsFileString += """END_INPUT |
paramsFileString += """END_INPUT |
138 |
PIXEL HALF |
PIXEL HALF |
140 |
""" |
""" |
141 |
|
|
142 |
# write the string to file |
# write the string to file |
143 |
fp = open( "%s.params" % (fnameStem,), "w" ) |
fp = open( "%s/%s.params" % (dirname,fnameStem,), "w" ) |
144 |
fp.write(paramsFileString + '\n') |
fp.write(paramsFileString + '\n') |
145 |
fp.close() |
fp.close() |
146 |
print "Done params file generation" |
print "Done params file generation" |
147 |
|
|
148 |
# now do the conversion to mpeg |
# now do the conversion to mpeg |
149 |
print "Performing conversion to mpeg" |
print "Performing conversion to mpeg" |
150 |
convertString = "ppmtompeg %s.params" % (fnameStem,) |
convertString = "ppmtompeg %s/%s.params" % (dirname, fnameStem) |
151 |
result = os.system(convertString) |
result = os.system(convertString) |
152 |
if result != 0: |
if result != 0: |
153 |
print "An error occurred in mpeg conversion" |
print "An error occurred in mpeg conversion" |