<s> from __future__ import ( absolute_import , division , print_function , <EOL> unicode_literals ) <EOL> import six <EOL> import re <EOL> import subprocess as sp <EOL> import sys <EOL> import os <EOL> import numpy as np <EOL> from pims . base_frames import FramesSequence <EOL> from pims . frame import Frame <EOL> try : <EOL> from subprocess import DEVNULL <EOL> except ImportError : <EOL> DEVNULL = open ( os . devnull , '<STR_LIT:wb>' ) <EOL> def try_ffmpeg ( FFMPEG_BINARY ) : <EOL> try : <EOL> proc = sp . Popen ( [ FFMPEG_BINARY ] , <EOL> stdout = sp . PIPE , <EOL> stderr = sp . PIPE ) <EOL> proc . wait ( ) <EOL> except : <EOL> return False <EOL> else : <EOL> return True <EOL> FFMPEG_BINARY_SUGGESTIONS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> FFMPEG_BINARY = None <EOL> for name in FFMPEG_BINARY_SUGGESTIONS : <EOL> if try_ffmpeg ( name ) : <EOL> FFMPEG_BINARY = name <EOL> break <EOL> def available ( ) : <EOL> return FFMPEG_BINARY is not None <EOL> _pix_fmt_dict = { '<STR_LIT>' : <NUM_LIT:3> , <EOL> '<STR_LIT>' : <NUM_LIT:4> } <EOL> class FFmpegVideoReader ( FramesSequence ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , filename , process_func = None , pix_fmt = "<STR_LIT>" , <EOL> use_cache = True , as_grey = False ) : <EOL> self . filename = filename <EOL> self . pix_fmt = pix_fmt <EOL> self . _initialize ( use_cache ) <EOL> try : <EOL> self . depth = _pix_fmt_dict [ pix_fmt ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> w , h = self . _size <EOL> self . _stride = self . depth * w * h <EOL> self . _validate_process_func ( process_func ) <EOL> self . _as_grey ( as_grey , process_func ) <EOL> def _initialize ( self , use_cache ) : <EOL> """<STR_LIT>""" <EOL> buffer_filename = '<STR_LIT>' . format ( self . filename ) <EOL> meta_filename = '<STR_LIT>' . format ( self . filename ) <EOL> cmd = [ FFMPEG_BINARY , '<STR_LIT>' , self . filename , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> "<STR_LIT>" , self . pix_fmt , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:->' ] <EOL> proc = sp . Popen ( cmd , stdin = sp . PIPE , <EOL> stdout = sp . PIPE , <EOL> stderr = sp . PIPE ) <EOL> print ( "<STR_LIT>" ) <EOL> if ( os . path . isfile ( buffer_filename ) and os . path . isfile ( meta_filename ) <EOL> and use_cache ) : <EOL> print ( "<STR_LIT>" ) <EOL> self . data_buffer = open ( buffer_filename , '<STR_LIT:rb>' ) <EOL> self . metafile = open ( meta_filename , '<STR_LIT:r>' ) <EOL> self . _len = int ( self . metafile . readline ( ) ) <EOL> w = int ( self . metafile . readline ( ) ) <EOL> h = int ( self . metafile . readline ( ) ) <EOL> self . _size = [ w , h ] <EOL> return <EOL> self . data_buffer = open ( buffer_filename , '<STR_LIT:wb>' ) <EOL> self . metafile = open ( meta_filename , '<STR_LIT:w>' ) <EOL> print ( "<STR_LIT>" ) <EOL> sys . stdout . flush ( ) <EOL> CHUNKSIZE = <NUM_LIT:2> ** <NUM_LIT> <EOL> while True : <EOL> try : <EOL> chunk = proc . stdout . read ( CHUNKSIZE ) <EOL> if len ( chunk ) == <NUM_LIT:0> : <EOL> break <EOL> self . data_buffer . write ( chunk ) <EOL> except EOFError : <EOL> break <EOL> self . data_buffer . close ( ) <EOL> self . data_buffer = open ( buffer_filename , '<STR_LIT:rb>' ) <EOL> self . _process_ffmpeg_stderr ( proc . stderr . read ( ) ) <EOL> proc . terminate ( ) <EOL> for std in proc . stdin , proc . stdout , proc . stderr : <EOL> std . close ( ) <EOL> def _process_ffmpeg_stderr ( self , stderr , verbose = False ) : <EOL> if verbose : <EOL> print ( stderr ) <EOL> lines = stderr . splitlines ( ) <EOL> if "<STR_LIT>" in lines [ - <NUM_LIT:1> ] : <EOL> raise IOError ( "<STR_LIT>" % self . filename ) <EOL> line = [ l for l in lines if '<STR_LIT>' in l ] [ <NUM_LIT:0> ] <EOL> match = re . search ( "<STR_LIT>" , line ) <EOL> self . _size = map ( int , line [ match . start ( ) : match . end ( ) - <NUM_LIT:1> ] . split ( '<STR_LIT:x>' ) ) <EOL> self . _len = int ( lines [ - <NUM_LIT:2> ] . split ( ) [ <NUM_LIT:1> ] ) <EOL> self . metafile . write ( '<STR_LIT>' . format ( self . _len ) ) <EOL> self . metafile . write ( '<STR_LIT>' . format ( self . _size [ <NUM_LIT:0> ] ) ) <EOL> self . metafile . write ( '<STR_LIT>' . format ( self . _size [ <NUM_LIT:1> ] ) ) <EOL> self . metafile . close ( ) <EOL> def __len__ ( self ) : <EOL> return self . _len <EOL> @ property <EOL> def frame_shape ( self ) : <EOL> return self . _size <EOL> def get_frame ( self , j ) : <EOL> self . data_buffer . seek ( self . _stride * j ) <EOL> s = self . data_buffer . read ( self . _stride ) <EOL> w , h = self . _size <EOL> result = np . fromstring ( s , <EOL> dtype = '<STR_LIT>' ) . reshape ( ( h , w , self . depth ) ) <EOL> return Frame ( self . process_func ( result ) , frame_no = j ) <EOL> @ property <EOL> def pixel_type ( self ) : <EOL> raise NotImplemented ( ) <EOL> @ classmethod <EOL> def class_exts ( cls ) : <EOL> return set ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def __repr__ ( self ) : <EOL> return """<STR_LIT>""" . format ( frame_shape = self . frame_shape , <EOL> count = len ( self ) , <EOL> filename = self . filename , <EOL> pix_fmt = self . pix_fmt ) </s>
