<s> """<STR_LIT>""" <EOL> import encoder <EOL> import filecmp <EOL> import json <EOL> import os <EOL> import re <EOL> import subprocess <EOL> class FileCodec ( encoder . Codec ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , formatter = None ) : <EOL> super ( FileCodec , self ) . __init__ ( name , formatter = formatter ) <EOL> self . extension = '<STR_LIT>' <EOL> def _EncodeFile ( self , parameters , bitrate , videofile , encodedfile ) : <EOL> commandline = self . EncodeCommandLine ( <EOL> parameters , bitrate , videofile , encodedfile ) <EOL> print commandline <EOL> with open ( os . path . devnull , '<STR_LIT:r>' ) as nullinput : <EOL> times_start = os . times ( ) <EOL> returncode = subprocess . call ( commandline , shell = True , stdin = nullinput ) <EOL> times_end = os . times ( ) <EOL> subprocess_cpu = times_end [ <NUM_LIT:2> ] - times_start [ <NUM_LIT:2> ] <EOL> elapsed_clock = times_end [ <NUM_LIT:4> ] - times_start [ <NUM_LIT:4> ] <EOL> print "<STR_LIT>" % ( <EOL> subprocess_cpu , elapsed_clock ) <EOL> if returncode : <EOL> raise Exception ( "<STR_LIT>" % returncode ) <EOL> return ( subprocess_cpu , elapsed_clock ) <EOL> def _DecodeFile ( self , videofile , encodedfile , workdir ) : <EOL> tempyuvfile = os . path . join ( workdir , <EOL> videofile . basename + '<STR_LIT>' ) <EOL> if os . path . isfile ( tempyuvfile ) : <EOL> print "<STR_LIT>" , tempyuvfile <EOL> os . unlink ( tempyuvfile ) <EOL> commandline = self . DecodeCommandLine ( videofile , encodedfile , tempyuvfile ) <EOL> print commandline <EOL> with open ( os . path . devnull , '<STR_LIT:r>' ) as nullinput : <EOL> subprocess_cpu_start = os . times ( ) [ <NUM_LIT:2> ] <EOL> returncode = subprocess . call ( commandline , shell = True , <EOL> stdin = nullinput ) <EOL> if returncode : <EOL> raise Exception ( '<STR_LIT>' % returncode ) <EOL> subprocess_cpu = os . times ( ) [ <NUM_LIT:2> ] - subprocess_cpu_start <EOL> print "<STR_LIT>" % subprocess_cpu <EOL> commandline = encoder . Tool ( "<STR_LIT>" ) + "<STR_LIT>" % ( <EOL> videofile . filename , tempyuvfile , videofile . width , <EOL> videofile . height ) <EOL> print commandline <EOL> psnr = subprocess . check_output ( commandline , shell = True , stdin = nullinput ) <EOL> commandline = [ '<STR_LIT>' , tempyuvfile ] <EOL> md5 = subprocess . check_output ( commandline , shell = False ) <EOL> yuv_md5 = md5 . split ( '<STR_LIT:U+0020>' ) [ <NUM_LIT:0> ] <EOL> os . unlink ( tempyuvfile ) <EOL> return psnr , subprocess_cpu , yuv_md5 <EOL> def Execute ( self , parameters , bitrate , videofile , workdir ) : <EOL> encodedfile = os . path . join ( workdir , <EOL> '<STR_LIT>' % ( videofile . basename , self . extension ) ) <EOL> subprocess_cpu , elapsed_clock = self . _EncodeFile ( parameters , bitrate , <EOL> videofile , encodedfile ) <EOL> result = { } <EOL> result [ '<STR_LIT>' ] = subprocess_cpu <EOL> result [ '<STR_LIT>' ] = elapsed_clock <EOL> result [ '<STR_LIT>' ] = self . EncoderVersion ( ) <EOL> bitrate = videofile . MeasuredBitrate ( os . path . getsize ( encodedfile ) ) <EOL> psnr , decode_cputime , yuv_md5 = self . _DecodeFile ( <EOL> videofile , encodedfile , workdir ) <EOL> result [ '<STR_LIT>' ] = decode_cputime <EOL> result [ '<STR_LIT>' ] = yuv_md5 <EOL> print "<STR_LIT>" , bitrate , "<STR_LIT>" , psnr <EOL> result [ '<STR_LIT>' ] = int ( bitrate ) <EOL> result [ '<STR_LIT>' ] = float ( psnr ) <EOL> result [ '<STR_LIT>' ] = videofile . ClipTime ( ) <EOL> result . update ( self . ResultData ( encodedfile ) ) <EOL> return result <EOL> def EncodeCommandLine ( self , parameters , bitrate , videofile , encodedfile ) : <EOL> """<STR_LIT>""" <EOL> raise encoder . Error ( '<STR_LIT>' ) <EOL> def DecodeCommandLine ( self , videofile , encodedfile , yuvfile ) : <EOL> """<STR_LIT>""" <EOL> raise encoder . Error ( '<STR_LIT>' ) <EOL> def ResultData ( self , encodedfile ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def VerifyEncode ( self , parameters , bitrate , videofile , workdir ) : <EOL> """<STR_LIT>""" <EOL> old_encoded_file = '<STR_LIT>' % ( workdir , videofile . basename , <EOL> self . extension ) <EOL> if not os . path . isfile ( old_encoded_file ) : <EOL> raise encoder . Error ( '<STR_LIT>' % old_encoded_file ) <EOL> new_encoded_file = '<STR_LIT>' % ( workdir , videofile . basename , <EOL> self . extension ) <EOL> self . _EncodeFile ( parameters , bitrate , videofile , <EOL> new_encoded_file ) <EOL> if not VideoFilesEqual ( old_encoded_file , new_encoded_file , self . extension ) : <EOL> return False <EOL> os . unlink ( new_encoded_file ) <EOL> return True <EOL> def EncoderVersion ( self ) : <EOL> raise encoder . Error ( '<STR_LIT>' ) <EOL> def MatroskaFrameInfo ( encodedfile ) : <EOL> commandline = '<STR_LIT>' % encodedfile <EOL> print commandline <EOL> mkvinfo = subprocess . check_output ( commandline , shell = True ) <EOL> frameinfo = [ ] <EOL> for line in mkvinfo . splitlines ( ) : <EOL> match = re . search ( r'<STR_LIT>' , line ) <EOL> if match : <EOL> frameinfo . append ( { '<STR_LIT:size>' : int ( match . group ( <NUM_LIT:1> ) ) * <NUM_LIT:8> } ) <EOL> return frameinfo <EOL> def FfmpegFrameInfo ( encodedfile ) : <EOL> commandline = '<STR_LIT>' % ( <EOL> encoder . Tool ( '<STR_LIT>' ) , encodedfile ) <EOL> ffprobeinfo = subprocess . check_output ( commandline , shell = True ) <EOL> probeinfo = json . loads ( ffprobeinfo ) <EOL> previous_position = <NUM_LIT:0> <EOL> frameinfo = [ ] <EOL> for frame in probeinfo [ '<STR_LIT>' ] : <EOL> current_position = int ( frame [ '<STR_LIT>' ] ) <EOL> if previous_position != <NUM_LIT:0> : <EOL> frameinfo . append ( { '<STR_LIT:size>' : <NUM_LIT:8> * ( current_position - previous_position ) } ) <EOL> previous_position = current_position <EOL> frameinfo . append ( { '<STR_LIT:size>' : <NUM_LIT:8> * <EOL> ( os . path . getsize ( encodedfile ) - previous_position ) } ) <EOL> return frameinfo <EOL> def VideoFilesEqual ( old_encoded_file , new_encoded_file , extension ) : <EOL> if extension == '<STR_LIT>' : <EOL> old_checksum = subprocess . check_output ( ( encoder . Tool ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> old_encoded_file ) ) <EOL> new_checksum = subprocess . check_output ( ( encoder . Tool ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' , <EOL> new_encoded_file ) ) <EOL> return old_checksum == new_checksum <EOL> else : <EOL> return filecmp . cmp ( old_encoded_file , new_encoded_file ) </s>
