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