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