<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import glob <EOL> import copy <EOL> import subprocess <EOL> from . pymosh import Index <EOL> from . pymosh . mpeg4 import is_iframe <EOL> from . util import IS_TTY , HR , hilite <EOL> import sequence <EOL> import readline <EOL> class Moshion ( ) : <EOL> def __init__ ( self , inseq , moshseq , start , end , outdir , moshstart = None , bitrate = "<STR_LIT>" , keyframe = "<STR_LIT>" , threshold = "<STR_LIT>" , interval = "<STR_LIT>" , avionly = False , uber = False ) : <EOL> self . uber = uber <EOL> self . inseq = os . path . normpath ( inseq . replace ( '<STR_LIT:\\>' , os . sep ) ) <EOL> self . filebase = self . inseq . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] . split ( os . sep ) [ - <NUM_LIT:1> ] <EOL> self . fileext = self . inseq . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> self . moshseq = os . path . normpath ( moshseq . replace ( '<STR_LIT:\\>' , os . sep ) ) <EOL> self . start = int ( start ) <EOL> self . end = int ( end ) <EOL> self . avionly = avionly <EOL> if moshstart is None : <EOL> self . moshstart = int ( self . start ) <EOL> else : <EOL> self . moshstart = int ( moshstart ) <EOL> self . input_frame_list = list ( range ( int ( self . start ) , int ( self . end ) + <NUM_LIT:1> ) ) <EOL> self . mosh_frame_list = list ( range ( int ( self . moshstart ) , int ( self . moshstart ) + len ( self . input_frame_list ) + <NUM_LIT:1> ) ) <EOL> self . bitrate = bitrate <EOL> self . keyframe = keyframe <EOL> self . threshold = threshold <EOL> self . interval = interval <EOL> self . outdir = os . path . normpath ( outdir . replace ( '<STR_LIT:\\>' , os . sep ) ) <EOL> self . to_mosh = [ ] <EOL> self . moshed = [ ] <EOL> if not os . path . isdir ( self . outdir ) : <EOL> os . makedirs ( self . outdir , <NUM_LIT:0> <NUM_LIT> ) <EOL> self . outfiledir = os . path . join ( self . outdir , self . fileext ) <EOL> if not os . path . isdir ( self . outfiledir ) and not self . avionly : os . makedirs ( self . outfiledir , <NUM_LIT:0> <NUM_LIT> ) <EOL> outfilename = "<STR_LIT>" . format ( os . sep , self . filebase , self . fileext ) <EOL> self . outseq = os . path . join ( self . outdir , outfilename ) <EOL> self . file_list_dict = self . get_file_list_dict ( ) <EOL> self . write_file_list ( ) <EOL> def get_file_list_dict ( self ) : <EOL> files = { } <EOL> if not self . uber : <EOL> file_list_name = '<STR_LIT>' <EOL> file_list = [ '<STR_LIT>' ] <EOL> for frame in self . input_frame_list : <EOL> file_list . append ( "<STR_LIT>" % self . inseq % frame ) <EOL> for frame in self . mosh_frame_list : <EOL> file_list . append ( "<STR_LIT>" % self . moshseq % frame ) <EOL> files [ file_list_name ] = file_list <EOL> else : <EOL> file_list_name = '<STR_LIT>' <EOL> uber_frame_list = list ( self . input_frame_list ) <EOL> while uber_frame_list : <EOL> file_list = [ '<STR_LIT>' ] <EOL> for frame in uber_frame_list : <EOL> file_list . append ( "<STR_LIT>" % self . inseq % frame ) <EOL> for frame in self . mosh_frame_list : <EOL> file_list . append ( "<STR_LIT>" % self . moshseq % frame ) <EOL> files [ file_list_name % uber_frame_list . pop ( ) ] = file_list <EOL> return files <EOL> def write_file_list ( self ) : <EOL> for ffile , flist in self . file_list_dict . iteritems ( ) : <EOL> file_path = os . path . join ( self . outdir , ffile ) <EOL> out_file = file ( file_path , '<STR_LIT:w>' ) <EOL> out_file . write ( '<STR_LIT:\n>' . join ( flist ) ) <EOL> out_file . close ( ) <EOL> def get_encode_opts ( self , list_file , avi ) : <EOL> opts = [ '<STR_LIT>' ] <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT:0>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ list_file ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , self . keyframe ] ) <EOL> opts . extend ( [ '<STR_LIT>' , self . threshold ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ avi ] ) <EOL> return opts <EOL> def write_movie ( self ) : <EOL> self . to_mosh = [ ] <EOL> if not self . uber : <EOL> msg = '<STR_LIT>' <EOL> if IS_TTY : print hilite ( msg , <NUM_LIT:7> ) <EOL> for key , value in self . file_list_dict . iteritems ( ) : <EOL> list_file = os . path . join ( self . outdir , key ) <EOL> avi_filename = "<STR_LIT>" . format ( self . filebase ) <EOL> self . to_mosh . append ( avi_filename ) <EOL> avi = os . path . join ( self . outdir , avi_filename ) <EOL> return subprocess . call ( self . get_encode_opts ( list_file , avi ) , close_fds = True ) <EOL> else : <EOL> msg = '<STR_LIT>' <EOL> if IS_TTY : print hilite ( msg , <NUM_LIT:7> ) <EOL> print hilite ( self . input_frame_list , <NUM_LIT:30> ) <EOL> file_list_name = '<STR_LIT>' <EOL> for frame in self . input_frame_list : <EOL> list_file = os . path . join ( self . outdir , file_list_name % frame ) <EOL> avi_filename = "<STR_LIT>" . format ( self . filebase , '<STR_LIT>' % frame ) <EOL> self . to_mosh . append ( avi_filename ) <EOL> avi = os . path . join ( self . outdir , avi_filename ) <EOL> subprocess . call ( self . get_encode_opts ( list_file , avi ) , close_fds = True ) <EOL> def mosh ( self ) : <EOL> if self . to_mosh is None : <EOL> return False <EOL> else : <EOL> for avi_filename in self . to_mosh : <EOL> moshed_avi_filename = avi_filename . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> msg = '<STR_LIT>' . format ( HR , moshed_avi_filename ) <EOL> if IS_TTY : print hilite ( msg , <NUM_LIT:15> ) <EOL> input_avi = os . path . join ( self . outdir , avi_filename ) <EOL> moshed_avi = os . path . join ( self . outdir , moshed_avi_filename ) <EOL> f = Index ( input_avi ) <EOL> buf = [ None ] <EOL> def process_frame ( frame ) : <EOL> """<STR_LIT>""" <EOL> if buf [ <NUM_LIT:0> ] == None or not is_iframe ( frame ) : <EOL> buf [ <NUM_LIT:0> ] = frame <EOL> else : <EOL> frame = buf [ <NUM_LIT:0> ] <EOL> return frame <EOL> for stream in f . video : <EOL> newstream = [ ] <EOL> newstream . append ( stream [ <NUM_LIT:0> ] ) <EOL> ix = <NUM_LIT:0> <EOL> jx = <NUM_LIT:0> <EOL> for i in stream [ <NUM_LIT:1> : ] : <EOL> ix += <NUM_LIT:1> <EOL> jx += <NUM_LIT:1> <EOL> if ix < self . interval : <EOL> newstream . append ( process_frame ( stream [ jx ] ) ) <EOL> else : <EOL> newstream . append ( newstream [ - <NUM_LIT:1> ] ) <EOL> if ix > self . interval * <NUM_LIT:2> : <EOL> ix = <NUM_LIT:0> <EOL> stream . replace ( newstream ) <EOL> f . rebuild ( ) <EOL> f . write ( moshed_avi ) <EOL> self . moshed . append ( moshed_avi_filename ) <EOL> def get_export_opts ( self , seekto_str , fcount , moshed_avi ) : <EOL> framerate = <NUM_LIT> <EOL> opts = [ '<STR_LIT>' ] <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , moshed_avi ] ) <EOL> opts . extend ( [ '<STR_LIT>' , seekto_str ] ) <EOL> opts . extend ( [ '<STR_LIT>' , str ( fcount ) ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> opts . extend ( [ self . outseq ] ) <EOL> print "<STR_LIT:U+0020>" . join ( opts ) <EOL> return opts <EOL> def write_sequence ( self ) : <EOL> framerate = <NUM_LIT> <EOL> seekto_frame = float ( len ( self . input_frame_list ) + <NUM_LIT:1.0> ) <EOL> seekto_secs = seekto_frame / framerate <EOL> seekto_str = "<STR_LIT>" % seekto_secs <EOL> if not self . uber : <EOL> fcount = len ( self . input_frame_list ) <EOL> for key , fname in enumerate ( self . moshed ) : <EOL> msg = '<STR_LIT>' . format ( HR , self . outseq , fcount , fname ) <EOL> if IS_TTY : print hilite ( msg , <NUM_LIT:11> ) <EOL> avi = os . path . join ( self . outdir , fname ) <EOL> subprocess . call ( self . get_export_opts ( seekto_str , fcount , avi ) , close_fds = True ) <EOL> else : <EOL> uber_frame_list = list ( self . moshed ) <EOL> while uber_frame_list : <EOL> fcount = len ( uber_frame_list ) <EOL> fname = uber_frame_list . pop ( <NUM_LIT:0> ) <EOL> avi = os . path . join ( self . outdir , fname ) <EOL> msg = '<STR_LIT>' . format ( HR , self . outseq , fcount , fname ) <EOL> if IS_TTY : print hilite ( msg , <NUM_LIT:11> ) <EOL> subprocess . call ( self . get_export_opts ( seekto_str , fcount , avi ) , close_fds = True ) <EOL> def do_full_mosh ( self ) : <EOL> self . write_movie ( ) <EOL> self . mosh ( ) <EOL> self . write_sequence ( ) </s>
