def encode_mp4(ipattern, ofile): """ Creates a mp4 video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: # two pass support with newer versions requires two calls to ffmpeg cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass %d " cmd += "-an -b:v 18000000 -f mp4 %s" # pass 1 cmd_pass1 = cmd % (enc_bin, ipattern, 1, ofile) res = sexe(cmd_pass1) if res == 0: # pass 2 cmd_pass2 = cmd % (enc_bin, ipattern, 2, ofile) res = sexe(cmd_pass2) else: cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-mbd -rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 " cmd += "-an -b 18000000 -f mp4 %s" cmd = cmd % (enc_bin, ipattern, ofile) res = sexe(cmd) return res else: raise VisItException("ffmpeg not found: Unable to encode mp4.")
def encode_mp4(ipattern, ofile): """ Creates a mp4 video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > 0.09: # two pass support with newer versions requires two calls to ffmpeg cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass %d " cmd += "-an -b:v 18000000 -f mp4 %s" # pass 1 cmd_pass1 = cmd % (enc_bin, ipattern, 1, ofile) res = sexe(cmd_pass1) if res == 0: # pass 2 cmd_pass2 = cmd % (enc_bin, ipattern, 2, ofile) res = sexe(cmd_pass2) else: cmd = "echo y | %s -f image2 -i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-mbd -rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 " cmd += "-an -b 18000000 -f mp4 %s" cmd = cmd % (enc_bin, ipattern, ofile) res = sexe(cmd) return res else: raise VisItException("ffmpeg not found: Unable to encode mp4.")
def extract(ifile, opattern): """ Extracts a sequence of images from a a movie. Requires ipattern to use printf style # format like "file%04d.png". """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: cmd = "%s -i %s -f image2 %s" % (enc_bin, ifile, opattern) sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to extract frames.")
def encode_avi(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates an avi video file (mjpeg) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += "-f image2 -i %s -vcodec mjpeg -q:v 1 -an " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s " else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += "-f image2 -i %s -vcodec mjpeg -qscale 1 -an " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd, echo=True) else: raise VisItException("ffmpeg not found: Unable to encode avi.")
def encode_divx(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates divx avi video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -vcodec mpeg4 -q:v 1 -f avi " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += "-vtag DX50 -an %s " else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -vcodec mpeg4 -qscale 1 -f avi " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += "-vtag DX50 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd, echo=True) else: raise VisItException("ffmpeg not found: Unable to encode divx avi.")
def ffmpeg_version(): """ Returns a floating point number that reflects the version of ffmpeg. 0.10.3 would be returned as 0.1003 1.2 would be returned as 1.02 SVN-r24044 would be returned as -1. """ res = sexe("ffmpeg -version",ret_output=True)[1].strip() idx = res.find("version ") ret = -1. if idx != -1 and res[idx+8:idx+8+3] != "SVN": idx2 = res[idx+8:].find(" ") idx3 = res[idx+8:].find("\n") if idx3 > -1 and idx3 < idx2: idx2 = idx3 version = res[idx+8:idx+8+idx2+1] points = [float(x) for x in string.split(version, ".")] ver = 0. mult = 1. for p in points: ver = ver + mult * p mult = mult / 100. ret = ver return ret
def encode_mp4(ipattern, ofile, input_frame_rate, output_frame_rate): """ Creates a mp4 video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not enc_bin is None: if ffmpeg_version() > .09: # two pass support with newer versions requires two calls to ffmpeg cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass %d " cmd += "-passlogfile %s " % ffmpeg_log_file_prefix(ofile) cmd += "-an -b:v 18000000 -f mp4 " if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s" # pass 1 cmd_pass1 = cmd % (enc_bin, ipattern, 1, ofile) res = sexe(cmd_pass1, echo=True) if res == 0: # pass 2 cmd_pass2 = cmd % (enc_bin, ipattern, 2, ofile) res = sexe(cmd_pass2, echo=True) else: cmd = "echo y | %s " if not input_frame_rate is None: cmd += " -framerate %s " % input_frame_rate cmd += ffmpeg_input_type(ipattern) cmd += "-i %s -qmin 1 -qmax 2 -g 100 -an -vcodec mpeg4 " cmd += "-mbd -rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 " cmd += "-an -b 18000000 -f mp4" if not output_frame_rate is None: cmd += " -r %s " % output_frame_rate cmd += " %s" cmd = cmd % (enc_bin, ipattern, ofile) res = sexe(cmd, echo=True) # clean up the log file if it exists if os.path.isfile(ffmpeg_log_file_for_pass(ofile)): os.remove(ffmpeg_log_file_for_pass(ofile)) return res else: raise VisItException("ffmpeg not found: Unable to encode mp4.")
def ffmpeg_bin(): """ Returns path to the 'ffmpeg' binary, or None if this binary is not available. """ res = sexe("which ffmpeg", ret_output=True)[1].strip() if os.path.exists(res): return res else: return None
def ffmpeg_bin(): """ Returns path to the 'ffmpeg' binary, or None if this binary is not available. """ if (platform.system() == "Windows"): return distutils.spawn.find_executable("ffmpeg") else: res = sexe("which ffmpeg", ret_output=True)[1].strip() if os.path.exists(res): return res else: return None
def img2sm_bin(): """ Returns path to the 'img2sm' script, or None if this script is not available. """ res = sexe("which img2sm", ret_output=True)[1].strip() if os.path.exists(res): return res if os.environ.has_key("SYS_TYPE"): res = pjoin("/usr/gapps/asciviz/blockbuster/latest", os.environ["SYS_TYPE"], "bin/img2sm") if os.path.exists(res): return res return None
def encode_sm(ipattern, ofile, stereo=False): """ Creates a 'streaming movie' or sm file. """ enc_bin = img2sm_bin() if not enc_bin is None: cmd = "%s -rle" % enc_bin if stereo: cmd += " -stereo" cmd += " %s %s " % (ipattern, ofile) return sexe(cmd) else: raise VisItException("img2sm not found: Unable to encode streaming movie.")
def __save_window(self, obase, res, ores, screen_cap, ts): prev_lvl = visit.SuppressMessages(2) res = [int(v) for v in res] if ores is None: ores = res obase = os.path.abspath(obase) odir, ofile = os.path.split(obase) if ts is None: print "[rendering %s/%s.png]" % (odir, ofile) tmp_ofile = "%s___.tmp" % ofile else: print "[rendering %s/%s%04d.png]" % (odir, ofile, ts) tmp_ofile = "%s.%04d___.tmp" % (ofile, ts) sa = visit.SaveWindowAttributes() sa.outputToCurrentDirectory = 0 sa.outputDirectory = odir sa.fileName = tmp_ofile sa.format = sa.PNG sa.width, sa.height = res sa.screenCapture = 0 sa.saveTiled = 0 sa.resConstraint = sa.NoConstraint visit.SetSaveWindowAttributes(sa) a = visit.GetAnnotationAttributes() a.userInfoFlag = 0 visit.SetAnnotationAttributes(a) fname = visit.SaveWindow() if fname == "/dev/null/SaveWindow_Error.txt" or not os.path.isfile( fname): raise VisItException("Error saving window.") des = fname[:fname.find("___.tmp")] des += ".png" shutil.move(fname, des) if ores[0] != res[0] or ores[1] != res[1]: stargs = (res[0], res[1], ores[0], ores[1]) print "[resizing output (from %dx%d to %dx%d)]" % stargs sexe("convert -resize %dx%d %s %s" % (ores[0], ores[1], des, des)) visit.SuppressMessages(prev_lvl) return des
def img2sm_bin(): """ Returns path to the 'img2sm' script, or None if this script is not available. """ res = sexe("which img2sm", ret_output=True)[1].strip() if os.path.exists(res): return res if os.environ.has_key('SYS_TYPE'): res = pjoin("/usr/gapps/asciviz/blockbuster/latest", os.environ["SYS_TYPE"], "bin/img2sm") if os.path.exists(res): return res return None
def __save_window(self,obase,res,ores,screen_cap,ts): prev_lvl = visit.SuppressMessages(2) res = [int(v) for v in res] if ores is None: ores = res obase = os.path.abspath(obase) odir, ofile = os.path.split(obase) if ts is None: print "[rendering %s/%s.png]" % (odir,ofile) else: print "[rendering %s/%s%04d.png]" % (odir,ofile,ts) sa = visit.SaveWindowAttributes() sa.outputToCurrentDirectory = 0 sa.outputDirectory = odir sa.fileName = ofile + "___.tmp" sa.format = sa.PNG sa.width, sa.height = res sa.screenCapture = 0 sa.saveTiled = 0 sa.resConstraint = sa.NoConstraint visit.SetSaveWindowAttributes(sa) a = visit.GetAnnotationAttributes() a.userInfoFlag = 0 visit.SetAnnotationAttributes(a) fname = visit.SaveWindow() if fname == "/dev/null/SaveWindow_Error.txt": raise VisItException("Error saving window.") des = fname[:fname.find("___.tmp")] if not ts is None: des += "%04d" % ts des += ".png" shutil.move(fname,des) if ores[0] != res[0] or ores[1] != res[1]: stargs = (res[0],res[1],ores[0],ores[1]) print "[resizing output (from %dx%d to %dx%d)]" % stargs sexe("convert -resize %dx%d %s %s" % (ores[0],ores[1],des,des)) visit.SuppressMessages(prev_lvl) return des
def encode_sm(ipattern, ofile, stereo=False): """ Creates a 'streaming movie' or sm file. """ enc_bin = img2sm_bin() if not enc_bin is None: cmd = "%s -rle" % enc_bin if stereo: cmd += " -stereo" cmd += " %s %s " % (ipattern, ofile) return sexe(cmd) else: raise VisItException( "img2sm not found: Unable to encode streaming movie.")
def encode_sm(ipattern, ofile, stereo=False): """ Creates a 'streaming movie' or sm file. """ enc_bin = img2sm_bin() if not enc_bin is None: cmd = "%s -c rle %s " % (enc_bin, ipattern) if stereo: cmd += " -S " cmd += ofile return sexe(cmd, echo=True) else: raise VisItException( "img2sm not found: Unable to encode streaming movie.")
def encode_avi(ipattern, ofile): """ Creates an avi video file (mjpeg) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -q:v 1 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -qscale 1 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode avi.")
def encode_avi(ipattern, ofile): """ Creates an avi video file (mjpeg) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > 0.09: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -q:v 1 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mjpeg -qscale 1 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode avi.")
def encode_divx(ipattern, ofile): """ Creates divx avi video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > 0.09: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -q:v 1 -f avi " cmd += "-vtag DX50 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -qscale 1 -f avi " cmd += "-vtag DX50 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode divx avi.")
def encode_divx(ipattern, ofile): """ Creates divx avi video file (mpeg4) using ffmpeg. """ enc_bin = ffmpeg_bin() if not ffmpeg_bin is None: if ffmpeg_version() > .09: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -q:v 1 -f avi " cmd += "-vtag DX50 -an %s " else: cmd = "echo y | %s -f image2 -i %s -vcodec mpeg4 -qscale 1 -f avi " cmd += "-vtag DX50 -an %s " cmd = cmd % (enc_bin, ipattern, ofile) return sexe(cmd) else: raise VisItException("ffmpeg not found: Unable to encode divx avi.")
def ffmpeg_version(): """ Returns a floating point number that reflects the version of ffmpeg. 0.10.3 would be returned as 0.1003 1.2 would be returned as 1.02 SVN-r24044 would be returned as -1. N-55356-gb11b7ce would be returned as 1.0 Note: This was created b/c the admissible command line syntax changed between versions of ffmpeg, and we wanted to support both the old and new way of doing things. """ ret = -1.0 res = sexe("ffmpeg -version", ret_output=True)[1].strip() idx = res.find("version ") # if no "version" text, return old style args if idx == -1: return res res = res[idx + len("version "):] res = res.replace("\n", " ") try: # github version if res.startswith("N-"): # assume github version supports "new" cmd line syntax (return 1.0) res = 1.0 # svn version if res.startswith("SVN"): # assume svn version supports old version (return -1.0) res = -1.0 else: #try to actually parse the version # version = res.split(" ")[0].strip() points = [float(x) for x in version.split(".")] ver = 0.0 mult = 1.0 for p in points: ver = ver + mult * p mult = mult / 100.0 ret = ver except: # fallback, assume this is a new(er) format, that will adhere to the # new style command line options ret = 1.0 return ret
def ffmpeg_version(): """ Returns a floating point number that reflects the version of ffmpeg. 0.10.3 would be returned as 0.1003 1.2 would be returned as 1.02 SVN-r24044 would be returned as -1. N-55356-gb11b7ce would be returned as 1.0 Note: This was created b/c the admissible command line syntax changed between versions of ffmpeg, and we wanted to support both the old and new way of doing things. """ ret = -1.0 res = sexe("ffmpeg -version", ret_output=True)[1].strip() idx = res.find("version ") # if no "version" text, return old style args if idx == -1: return res res = res[idx + len("version ") :] res = res.replace("\n", " ") try: # github version if res.startswith("N-"): # assume github version supports "new" cmd line syntax (return 1.0) res = 1.0 # svn version if res.startswith("SVN"): # assume svn version supports old version (return -1.0) res = -1.0 else: # try to actually parse the version # version = res.split(" ")[0].strip() points = [float(x) for x in version.split(".")] ver = 0.0 mult = 1.0 for p in points: ver = ver + mult * p mult = mult / 100.0 ret = ver except: # fallback, assume this is a new(er) format, that will adhere to the # new style command line options ret = 1.0 return ret