/[escript]/trunk/pyvisi/py_src/movie.py
ViewVC logotype

Contents of /trunk/pyvisi/py_src/movie.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2314 - (show annotations)
Tue Mar 17 04:16:24 2009 UTC (13 years, 6 months ago) by gross
File MIME type: text/x-python
File size: 10600 byte(s)
proper error handling for ppn2mpeg execution added.


1
2 ########################################################
3 #
4 # Copyright (c) 2003-2008 by University of Queensland
5 # Earth Systems Science Computational Center (ESSCC)
6 # http://www.uq.edu.au/esscc
7 #
8 # Primary Business: Queensland, Australia
9 # Licensed under the Open Software License version 3.0
10 # http://www.opensource.org/licenses/osl-3.0.php
11 #
12 ########################################################
13
14 __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15 Earth Systems Science Computational Center (ESSCC)
16 http://www.uq.edu.au/esscc
17 Primary Business: Queensland, Australia"""
18 __license__="""Licensed under the Open Software License version 3.0
19 http://www.opensource.org/licenses/osl-3.0.php"""
20 __url__="http://www.uq.edu.au/esscc/escript-finley"
21
22 """
23 @var __author__: name of author
24 @var __copyright__: copyrights
25 @var __license__: licence agreement
26 @var __url__: url entry point on documentation
27 @var __version__: version
28 @var __date__: date of the version
29 """
30
31 __author__="John Ngui, john.ngui@uq.edu.au"
32
33
34 from esys.pyvisi.constant import ImageFormat
35 from esys.escript import getMPIWorldMax, getMPIRankWorld
36 import os
37
38
39 class Movie:
40 """
41 Class that creates a file called 'make_movie' by default (if a parameter
42 file name is not speficied) which contains a list of parameters required
43 by the 'ppmtompeg' command to generate a movie from a series of images.
44
45 @attention: A movie cannot be generated from postscript (.ps) images.
46 """
47
48 def __init__(self, parameter_file = "make_movie"):
49 """
50 Initialise the generation of the movie. If a parameter file name
51 is supplied, the file will not be deleted once the movie has been
52 generated. Otherwise, the temporary parameter file created will
53 automatically be removed at the end.
54
55 @type parameter_file: String
56 @param parameter_file: Name of the file containing the list of
57 parameters required by the 'ppmtompeg' command.
58 """
59
60 self.__parameter_file = parameter_file
61 return
62
63
64 def imageRange(self, input_directory, first_image, last_image):
65 """
66 The image range from which the movie is to be generated from.
67
68 @type input_directory: String
69 @param input_directory: Directory in which the series of images can
70 be found
71 @type first_image: String
72 @param first_image: First image name (including the extension)
73 @type last_image: String
74 @param last_image: Last image name (including the extension)
75 """
76
77 # Keeps track whether an image range or image list was provided as
78 # the source for generating the movie.
79 self.__image_range_used = True
80 self.__input_directory = input_directory
81 self.__first_image = first_image
82 self.__last_image = last_image
83
84 self.__splitInput()
85 self.__retrieveFirstImageDetails()
86 self.__retrieveLastImageDetails()
87 self.__retrieveConversionCommand()
88 return
89
90 def imageList(self, input_directory, image_list):
91 """
92 The image list from which the movie is to be generated from.
93
94 @type input_directory: String
95 @param input_directory: Directory in which the series of images can
96 be found
97 @type image_list: List
98 @param image_list: List of images name (including the extension)
99 """
100
101 self.__image_range_used = False
102 self.__input_directory = input_directory
103 self.__images = ""
104
105 self.__first_image = image_list[0] # Get first image in the list.
106 self.__splitInput()
107 self.__retrieveConversionCommand()
108
109 for i in image_list:
110 self.__images += i + '\n'
111 return
112
113 def makeMovie(self, movie):
114 """
115 Generate the movie.
116
117 @type movie : String
118 @param movie: Movie name (including the .mpg extension)
119 """
120
121 self.__movie = movie
122 self.__generateParameterFile()
123
124 # If a parameter file name was not specified, then the default file
125 # will be deleted automatically once the movie has been generated.
126 # However, if a paramter file name was specified, the file will be
127 # maintained.
128 if getMPIRankWorld() == 0:
129 ret = os.system('ppmtompeg ' + self.__parameter_file) / 256
130 else:
131 ret=0
132 ret=getMPIWorldMax(ret)
133 if ret > 0:
134 raise RuntimeError, "Could not generate movie %s"%'ppmtompeg ' + self.__parameter_file
135
136 if(self.__parameter_file == "make_movie"):
137 if getMPIRankWorld() == 0:
138 ret = os.system('rm ' + self.__parameter_file) / 256
139 else:
140 ret=0
141 ret=getMPIWorldMax(ret)
142 if ret > 0:
143 raise RuntimeError, 'Could not remove file '+ self.__parameter_file
144 return
145
146 def __splitInput(self):
147 """
148 Split the image label (i.e. temp-0001.jpg) to separate the
149 image name (i.e. temp-0001) and image format (i.e. jpg).
150 """
151
152 first_image_split = self.__first_image.split('.')
153 # Image format (i.e. jpg)
154 self.__image_format = first_image_split[len(first_image_split) - 1]
155 # First image name.
156 self.__first_image = \
157 self.__first_image.rstrip('.' + self.__image_format)
158
159 if(self.__image_range_used == True):
160 # Last image name.
161 self.__last_image = \
162 self.__last_image.rstrip('.' + self.__image_format)
163 return
164
165 def __retrieveFirstImageDetails(self):
166 """
167 Retrieve the first image name details.
168 """
169
170 # For a image called 'temp-0001.jpg', self.__first_image_number
171 # will be '0001', while self.__image_prefix will be 'temp-'.
172 self.__first_image_number = ""
173 self.__image_prefix = ""
174
175 for i in range(len(self.__first_image)):
176 if(self.__first_image[i].isdigit()): # Retrieve first image number.
177 self.__first_image_number = \
178 self.__first_image_number + self.__first_image[i]
179 else: # Retrieve image prefix.
180 self.__image_prefix = \
181 self.__image_prefix + self.__first_image[i]
182 return
183
184 def __retrieveLastImageDetails(self):
185 """
186 Retrieve the last image name details.
187 """
188
189 self.__last_image_number = ""
190
191 for i in range(len(self.__last_image)):
192 if(self.__last_image[i].isdigit()): # Retrieve last image number.
193 self.__last_image_number = \
194 self.__last_image_number + self.__last_image[i]
195
196 return
197
198 def __retrieveConversionCommand(self):
199 """
200 Retrieve the conversion command (depending on the image format)
201 required by the 'ppmtompeg' command.
202 """
203
204 if(self.__image_format.endswith(ImageFormat.JPG)):
205 self.__command = 'jpeg'
206 elif(self.__image_format.endswith(ImageFormat.BMP)):
207 self.__command = ImageFormat.BMP
208 elif(self.__image_format.endswith(ImageFormat.PNM)):
209 self.__command = ImageFormat.PNM
210 elif(self.__image_format.endswith(ImageFormat.PNG)):
211 self.__command = ImageFormat.PNG
212 elif(self.__image_format.endswith(ImageFormat.TIF)):
213 self.__command = 'tiff'
214 else:
215 raise IOError("ERROR: '" + self.__image_format + \
216 "' is an invalid image format.")
217
218 return
219
220
221 def __generateParameterFile(self):
222 """
223 Write the list of parameters into the file.
224 """
225
226 if(self.__image_range_used == True): # Image range was provided.
227 input = self.__image_prefix + '*.' + self.__image_format + ' [' + \
228 self.__first_image_number + '-' + \
229 self.__last_image_number + ']\n'
230 else: # Image list was provided
231 input = self.__images
232
233
234 ret=0
235 if getMPIRankWorld() == 0:
236 try:
237 parameter_file = open(self.__parameter_file, 'w')
238
239 if os.name == 'nt' :
240 tmp_input_name = self.__input_directory.replace('\\','/')
241 tmp_movie_name = self.__movie.replace('\\','/')
242 else:
243 tmp_input_name = self.__input_directory
244 tmp_movie_name = self.__movie
245
246 parameter_file.write('PATTERN IBBPBBPBBPBBPBBP\n' +
247 'OUTPUT ' + tmp_movie_name + '\n'
248 'BASE_FILE_FORMAT PNM\n' +
249 'INPUT_CONVERT ' + self.__command + 'topnm *\n' +
250 'GOP_SIZE 16\n' +
251 'SLICES_PER_FRAME 10\n' +
252 'INPUT_DIR ' + tmp_input_name + '\n' +
253 'INPUT\n' +
254 input +
255 'END_INPUT\n' +
256 'PIXEL HALF\n' +
257 'RANGE 10\n' +
258 'PSEARCH_ALG LOGARITHMIC\n' +
259 'BSEARCH_ALG CROSS2\n' +
260 'IQSCALE 8\n' +
261 'PQSCALE 10\n' +
262 'BQSCALE 25\n' +
263 'REFERENCE_FRAME DECODED\n' +
264 'FORCE_ENCODE_LAST_FRAME\n' +
265 'ASPECT_RATIO 1\n' +
266 'FRAME_RATE 24')
267
268 parameter_file.close()
269 if os.name == 'nt':
270 tmp_name = self.__parameter_file.replace('\\','/')
271 ret = os.system('dos2unix ' + tmp_name) / 256
272 if ret >0:
273 raise RuntimeError,"execution of %s has failed."%('dos2unix ' + tmp_name)
274 except Exception, e:
275 ret=1
276 ret=getMPIWorldMax(ret)
277 if ret > 0:
278 if getMPIRankWorld() == 0:
279 raise e
280 else:
281 raise RuntimeError, "Movie generation on processor 0 failed."
282 return

  ViewVC Help
Powered by ViewVC 1.1.26