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