/[escript]/trunk/tools/vizTools/make_movie.py
ViewVC logotype

Diff of /trunk/tools/vizTools/make_movie.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/tools/vizTools/make_movie.py revision 147 by jgs, Fri Aug 12 01:45:47 2005 UTC trunk/tools/vizTools/make_movie.py revision 371 by cochrane, Fri Dec 16 00:57:13 2005 UTC
# Line 3  Line 3 
3  # $Id$  # $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:p:h",      "s:d:o:f:p:t:h",
14      ["framestem=", "output=", "format=", "pad=", "help"],      ["framestem=", "dirname=", "output=", "format=", "pad=", "pattern=",
15        "help"],
16      )      )
17    
18  def usage():  def usage():
# Line 19  def usage(): Line 20  def usage():
20      print "  make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n"      print "  make_movie -s <framestem> -o <output filename> -f <format> -p <pad>\n"
21      print "  Arguments:"      print "  Arguments:"
22      print "    -s/--framestem: The frame filename stem (the stuff before .pnm) (required)"      print "    -s/--framestem: The frame filename stem (the stuff before .pnm) (required)"
23        print "    -d/--dirname: The directory of frames (optional)"
24      print "    -o/--output: Output mpeg filename (optional)"      print "    -o/--output: Output mpeg filename (optional)"
25      print "    -f/--format: Input frame image format (optional)"      print "    -f/--format: Input frame image format (optional)"
26      print "    -p/--pad: How many frames to pad the movie (optional)"      print "    -p/--pad: How many frames to pad the movie (optional)"
27        print "    -h/--help: Display this information (optional)"
28    
29    dirname = "./"
30  mpegName = None  mpegName = None
31  fnameStem = None  fnameStem = None
32  format = "pnm"  # if format not specified assume pnm  format = "pnm"  # if format not specified assume pnm
33  pad = 1  pad = 1
34    pattern = "IBBPBBPBB"
35    
36  for option, arg in opts:  for option, arg in opts:
37      if option in ('-s', '--stem'):      if option in ('-s', '--stem'):
38      fnameStem = arg      fnameStem = arg
39        elif option in ('-d', '--dirname'):
40        dirname = arg
41      elif option in ('-o', '--output'):      elif option in ('-o', '--output'):
42      mpegName = arg      mpegName = arg
43      elif option in ('-f', '--format'):      elif option in ('-f', '--format'):
44      format = arg      format = arg
45      elif option in ('-p', '--pad'):      elif option in ('-p', '--pad'):
46      pad = int(arg)      pad = int(arg)
47        elif option in ('-t', '--pattern'):
48        pattern = arg
49      elif option in ('-h', '--help'):      elif option in ('-h', '--help'):
50      usage()      usage()
51      sys.exit(0)      sys.exit(0)
# Line 49  if mpegName is None: Line 58  if mpegName is None:
58      mpegName = fnameStem + ".mpg"      mpegName = fnameStem + ".mpg"
59    
60  # determine the number of files to convert  # determine the number of files to convert
61  dirList = os.listdir('./')  dirList = os.listdir(dirname)
62  r = re.compile( "%s\\d+\\.%s"%(fnameStem,format) )  r = re.compile( "%s\\d+\\.%s"%(fnameStem,format) )
63  count = 0  # counter for the number of files found  count = 0  # counter for the number of files found
64  fnames = []  fnames = []
# Line 70  if format != "pnm": Line 79  if format != "pnm":
79      front = r.findall(fname)      front = r.findall(fname)
80      front = front[0]      front = front[0]
81      # make the conversion string to pass to 'convert'      # make the conversion string to pass to 'convert'
82      convString = "convert %s%s %spnm" % (front, format, front)      convString = "convert %s/%s%s %s/%spnm" % \
83            (dirname, front, format, dirname, front)
84      retVal = os.system(convString)      retVal = os.system(convString)
85      if retVal != 0:      if retVal != 0:
86          raise SystemError, "Conversion of %s%s failed" % (front, format)          raise SystemError, "Conversion of %s%s failed" % (front, format)
# Line 87  print "Generating params file..." Line 97  print "Generating params file..."
97  paramsFileString = """REFERENCE_FRAME DECODED  paramsFileString = """REFERENCE_FRAME DECODED
98  FRAME_RATE 24  FRAME_RATE 24
99  OUTPUT %s  OUTPUT %s
100  PATTERN IBBPBBPBB  PATTERN %s
101  FORCE_ENCODE_LAST_FRAME  FORCE_ENCODE_LAST_FRAME
102  GOP_SIZE 20  GOP_SIZE 20
103  BSEARCH_ALG CROSS2  BSEARCH_ALG CROSS2
# Line 101  BASE_FILE_FORMAT PNM Line 111  BASE_FILE_FORMAT PNM
111  INPUT_DIR .  INPUT_DIR .
112  INPUT_CONVERT *  INPUT_CONVERT *
113  INPUT  INPUT
114  """ % (mpegName)  """ % (mpegName, pattern)
115    
116  # need to determine the first number and last number of the list of files  # need to determine the first number and last number of the list of files
117  # sort the files first just in case  # sort the files first just in case
# Line 118  lastNum = lastNum[0][1] Line 128  lastNum = lastNum[0][1]
128    
129  # finish off the params file string  # finish off the params file string
130  if pad == 1:  if pad == 1:
131      paramsFileString += "%s*.pnm [%s-%s]\n" % (fnameStem, firstNum, lastNum)      paramsFileString += "%s/%s*.pnm [%s-%s]\n" % \
132            (dirname, fnameStem, firstNum, lastNum)
133  elif pad > 1:  elif pad > 1:
134      # positive padding: add duplicate frames (slow the movie down)      # positive padding: add duplicate frames (slow the movie down)
135      for i in range(int(firstNum), int(lastNum)+1):      for i in range(int(firstNum), int(lastNum)+1):
136      for j in range(pad):      for j in range(pad):
137          paramsFileString += "%s%04d.pnm\n" % (fnameStem, i)          paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i)
138  elif pad < 1:  elif pad < 1:
139      # negative padding: i.e. remove frames (speed the movie up)      # negative padding: i.e. remove frames (speed the movie up)
140      for i in range(int(firstNum), int(lastNum)+1, abs(pad)):      for i in range(int(firstNum), int(lastNum)+1, abs(pad)):
141      paramsFileString += "%s%04d.pnm\n" % (fnameStem, i)      paramsFileString += "%s/%s%04d.pnm\n" % (dirname, fnameStem, i)
142    
143  paramsFileString += """END_INPUT  paramsFileString += """END_INPUT
144  PIXEL HALF  PIXEL HALF
# Line 135  ASPECT_RATIO 1 Line 146  ASPECT_RATIO 1
146  """  """
147    
148  # write the string to file  # write the string to file
149  fp = open( "%s.params" % (fnameStem,), "w" )  fp = open( "%s/%s.params" % (dirname,fnameStem,), "w" )
150  fp.write(paramsFileString + '\n')  fp.write(paramsFileString + '\n')
151  fp.close()  fp.close()
152  print "Done params file generation"  print "Done params file generation"
153    
154  # now do the conversion to mpeg  # now do the conversion to mpeg
155  print "Performing conversion to mpeg"  print "Performing conversion to mpeg"
156  convertString = "ppmtompeg %s.params" % (fnameStem,)  convertString = "ppmtompeg %s/%s.params" % (dirname, fnameStem)
157  result = os.system(convertString)  result = os.system(convertString)
158  if result != 0:  if result != 0:
159      print "An error occurred in mpeg conversion"      print "An error occurred in mpeg conversion"

Legend:
Removed from v.147  
changed lines
  Added in v.371

  ViewVC Help
Powered by ViewVC 1.1.26