1 |
#!/usr/bin/env python |
#!/usr/bin/env python |
2 |
|
|
3 |
|
# $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:h", |
"s:d:o:f:p:h", |
14 |
["framestem=", "output=", "format=", "help"], |
["framestem=", "dirname=", "output=", "format=", "pad=", "help"], |
15 |
) |
) |
16 |
|
|
17 |
def usage(): |
def usage(): |
18 |
print "Usage:" |
print "Usage:" |
19 |
print " make_movie -s <framestem> -o <output filename> -f <format>\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)" |
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 |
31 |
|
pad = 1 |
32 |
|
|
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'): |
41 |
format = arg |
format = arg |
42 |
|
elif option in ('-p', '--pad'): |
43 |
|
pad = int(arg) |
44 |
elif option in ('-h', '--help'): |
elif option in ('-h', '--help'): |
45 |
usage() |
usage() |
46 |
sys.exit(0) |
sys.exit(0) |
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 = [] |
62 |
fnames.append(fname) |
fnames.append(fname) |
63 |
count += 1 |
count += 1 |
64 |
|
|
65 |
|
if count == 0: |
66 |
|
raise ValueError, "No matching files found" |
67 |
|
|
68 |
# do a conversion if necessary |
# do a conversion if necessary |
69 |
if format != "pnm": |
if format != "pnm": |
70 |
print "Converting frames to pnm" |
print "Converting frames to pnm" |
74 |
front = r.findall(fname) |
front = r.findall(fname) |
75 |
front = front[0] |
front = front[0] |
76 |
# make the conversion string to pass to 'convert' |
# make the conversion string to pass to 'convert' |
77 |
convString = "convert %s%s %spnm" % (front, format, front) |
convString = "convert %s/%s%s %s/%spnm" % \ |
78 |
|
(dirname, front, format, dirname, front) |
79 |
retVal = os.system(convString) |
retVal = os.system(convString) |
80 |
if retVal != 0: |
if retVal != 0: |
81 |
raise SystemError, "Conversion of %s%s failed" % (front, format) |
raise SystemError, "Conversion of %s%s failed" % (front, format) |
114 |
firstFile = fnames[0] |
firstFile = fnames[0] |
115 |
lastFile = fnames[-1] |
lastFile = fnames[-1] |
116 |
|
|
117 |
r = re.compile("([a-zA-Z])(\\d+)(\\.\\w+)") |
r = re.compile("([a-zA-Z-_\.\d])(\\d+)(\\.\\w+)$") |
118 |
firstNum = r.findall(firstFile) |
firstNum = r.findall(firstFile) |
119 |
firstNum = firstNum[0][1] |
firstNum = firstNum[0][1] |
120 |
lastNum = r.findall(lastFile) |
lastNum = r.findall(lastFile) |
121 |
lastNum = lastNum[0][1] |
lastNum = lastNum[0][1] |
122 |
|
|
123 |
|
|
124 |
# finish off the params file string |
# finish off the params file string |
125 |
paramsFileString += "%s*.pnm [%s-%s]\n" % (fnameStem, firstNum, lastNum) |
if pad == 1: |
126 |
|
paramsFileString += "%s/%s*.pnm [%s-%s]\n" % \ |
127 |
|
(dirname, fnameStem, firstNum, lastNum) |
128 |
|
elif pad > 1: |
129 |
|
# positive padding: add duplicate frames (slow the movie down) |
130 |
|
for i in range(int(firstNum), int(lastNum)+1): |
131 |
|
for j in range(pad): |
132 |
|
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
133 |
|
elif pad < 1: |
134 |
|
# negative padding: i.e. remove frames (speed the movie up) |
135 |
|
for i in range(int(firstNum), int(lastNum)+1, abs(pad)): |
136 |
|
paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i) |
137 |
|
|
138 |
paramsFileString += """END_INPUT |
paramsFileString += """END_INPUT |
139 |
PIXEL HALF |
PIXEL HALF |
141 |
""" |
""" |
142 |
|
|
143 |
# write the string to file |
# write the string to file |
144 |
fp = open( "%s.params" % (fnameStem,), "w" ) |
fp = open( "%s/%s.params" % (dirname,fnameStem,), "w" ) |
145 |
fp.write(paramsFileString + '\n') |
fp.write(paramsFileString + '\n') |
146 |
fp.close() |
fp.close() |
147 |
print "Done params file generation" |
print "Done params file generation" |
148 |
|
|
149 |
# now do the conversion to mpeg |
# now do the conversion to mpeg |
150 |
print "Performing conversion to mpeg" |
print "Performing conversion to mpeg" |
151 |
convertString = "ppmtompeg %s.params" % (fnameStem,) |
convertString = "ppmtompeg %s/%s.params" % (dirname, fnameStem) |
152 |
result = os.system(convertString) |
result = os.system(convertString) |
153 |
if result != 0: |
if result != 0: |
154 |
print "An error occurred in mpeg conversion" |
print "An error occurred in mpeg conversion" |