
    khY%                    
   U d dl mZ d dlZd dlZd dlZd dlZd dlmZmZm	Z	m
Z
 	 d dlmZ ded<   eZ G d d      Z G d	 d
e      Z G d de      Z G d de      Z	 d	 	 	 	 	 ddZdddZy# e$ r ej                  ZY Ow xY w)    )annotationsN)IOAnyMappingSequence)Popenztype[subprocess.Popen | Popen]popenc                  d    e Zd ZdZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZddZ	 	 	 	 d		 	 	 	 	 	 	 	 	 	 	 d
dZy)FFmpegzhWrapper for various `FFmpeg <https://www.ffmpeg.org/>`_ related applications (ffmpeg,
    ffprobe).
    Nc                <   || _         |g| _        | xj                  t        |d      z  c_        | | xj                  t        |d      z  c_        || xj                  t        |      z  c_        t	        j
                  | j                        | _        d| _        y)aJ  Initialize FFmpeg command line wrapper.

        Compiles FFmpeg command line from passed arguments (executable path, options, inputs and
        outputs). ``inputs`` and ``outputs`` are dictionares containing inputs/outputs as keys and
        their respective options as values. One dictionary value (set of options) must be either a
        single space separated string, or a list or strings without spaces (i.e. each part of the
        option is a separate item of the list, the result of calling ``split()`` on the options
        string). If the value is a list, it cannot be mixed, i.e. cannot contain items with spaces.
        An exception are complex FFmpeg command lines that contain quotes: the quoted part must be
        one string, even if it contains spaces (see *Examples* for more info).
        For more info about FFmpeg command line format see `here
        <https://ffmpeg.org/ffmpeg.html#Synopsis>`_.

        :param str executable: path to ffmpeg executable; by default the ``ffmpeg`` command will be
            searched for in the ``PATH``, but can be overridden with an absolute path to ``ffmpeg``
            executable
        :param iterable global_options: global options passed to ``ffmpeg`` executable (e.g.
            ``-y``, ``-v`` etc.); can be specified either as a list/tuple/set of strings, or one
            space-separated string; by default no global options are passed
        :param dict inputs: a dictionary specifying one or more input arguments as keys with their
            corresponding options (either as a list of strings or a single space separated string) as
            values
        :param dict outputs: a dictionary specifying one or more output arguments as keys with their
            corresponding options (either as a list of strings or a single space separated string) as
            values
        T)split_mixedN)add_minus_i_option)
executable_cmd_normalize_options_merge_args_opts
subprocesslist2cmdlinecmdprocess)selfr   global_optionsinputsoutputss        G/opt/mcp/mcp-sentiment/venv/lib/python3.12/site-packages/ffmpy/ffmpy.py__init__zFFmpeg.__init__   s~    B %L			'DII	II)&TJJIII)'22I**49958<    c                P    d| j                   j                  d| j                  dS )N< >)	__class____name__r   )r   s    r   __repr__zFFmpeg.__repr__F   s&    4>>**-Qtxxl!<<r   c                   	 t        | j                  ft        j                  |||d|| _        | j                  j                  |      \  }}| j                  j                  dk7  r,t        | j                  | j                  j                  ||      ||fS # t
        $ r<}|j                  t        j                  k(  rt        d| j                   d       d}~ww xY w)a  Execute FFmpeg command line.

        ``input_data`` can contain input for FFmpeg in case ``pipe`` protocol is used for input.
        ``stdout`` and ``stderr`` specify where to redirect the ``stdout`` and ``stderr`` of the
        process. By default no redirection is done, which means all output goes to running shell
        (this mode should normally only be used for debugging purposes). If FFmpeg ``pipe`` protocol
        is used for output, ``stdout`` must be redirected to a pipe by passing `subprocess.PIPE` as
        ``stdout`` argument. You can pass custom environment to ffmpeg process with ``env``.

        Returns a 2-tuple containing ``stdout`` and ``stderr`` of the process. If there was no
        redirection or if the output was redirected to e.g. `os.devnull`, the value returned will
        be a tuple of two `None` values, otherwise it will contain the actual ``stdout`` and
        ``stderr`` data returned by ffmpeg process.

        More info about ``pipe`` protocol `here <https://ffmpeg.org/ffmpeg-protocols.html#pipe>`_.

        :param str input_data: input data for FFmpeg to deal with (audio, video etc.) as bytes (e.g.
            the result of reading a file in binary mode)
        :param stdout: redirect FFmpeg ``stdout`` there (default is `None` which means no
            redirection)
        :param stderr: redirect FFmpeg ``stderr`` there (default is `None` which means no
            redirection)
        :param env: custom environment for ffmpeg process
        :param kwargs: any other keyword arguments to be forwarded to `subprocess.Popen
            <https://docs.python.org/3/library/subprocess.html#subprocess.Popen>`_
        :return: a 2-tuple containing ``stdout`` and ``stderr`` of the process
        :rtype: tuple
        :raise: `FFRuntimeError` in case FFmpeg command exits with a non-zero code;
            `FFExecutableNotFoundError` in case the executable path passed was not valid
        )stdinstdoutstderrenvzExecutable 'z' not foundN)inputr   )r	   r   r   PIPEr   OSErrorerrnoENOENTFFExecutableNotFoundErrorr   communicate
returncodeFFRuntimeErrorr   )	r   
input_datar'   r(   r)   kwargseo_stdouto_stderrs	            r   runz
FFmpeg.runI   s    L	 		!+TW[aDL "\\55J5G(<<""a' 4<<+B+BHhWW!!  	ww%,,&/,t>O{0[\\		s   .B 	C!7CC)ffmpegNNN)
r   strr   Sequence[str] | str | Noner   /Mapping[str, Sequence[str] | str | None] | Noner   r<   returnNone)r=   r:   )NNNN)r3   zbytes | Noner'   IO | int | Noner(   r?   r)   zMapping[str, str] | Noner4   r   r=   z!tuple[bytes | None, bytes | None])r#   
__module____qualname____doc__r   r$   r8    r   r   r   r      s     #59BFCG,=,= 3,= @	,=
 A,= 
,=\=
 $("&"&(,4" 4"  4"  	4"
 &4" 4" 
+4"r   r   c                  :     e Zd ZdZ	 	 	 d	 	 	 	 	 	 	 d fdZ xZS )FFprobez=Wrapper for `ffprobe <https://www.ffmpeg.org/ffprobe.html>`_.c                *    t         |   |||       y)a  Create an instance of FFprobe.

        Compiles FFprobe command line from passed arguments (executable path, options, inputs).
        FFprobe executable by default is taken from ``PATH`` but can be overridden with an
        absolute path. For more info about FFprobe command line format see
        `here <https://ffmpeg.org/ffprobe.html#Synopsis>`_.

        :param str executable: absolute path to ffprobe executable
        :param iterable global_options: global options passed to ffmpeg executable; can be specified
            either as a list/tuple of strings or a space-separated string
        :param dict inputs: a dictionary specifying one or more inputs as keys with their
            corresponding options as values
        )r   r   r   N)superr   )r   r   r   r   r"   s       r   r   zFFprobe.__init__   s    & 	J~V\]r   )ffprobeNN)r   r:   r   r;   r   r<   r=   r>   r#   r@   rA   rB   r   __classcell__r"   s   @r   rE   rE      sH    G $59BF	^^ 3^ @	^
 
^ ^r   rE   c                      e Zd ZdZy)r/   z3Raise when FFmpeg/FFprobe executable was not found.N)r#   r@   rA   rB   rC   r   r   r/   r/      s    =r   r/   c                  $     e Zd ZdZd fdZ xZS )r2   zRaise when FFmpeg/FFprobe command line execution returns a non-zero exit code.

    The resulting exception object will contain the attributes relates to command line execution:
    ``cmd``, ``exit_code``, ``stdout``, ``stderr``.
    c                    || _         || _        || _        || _        dj	                  | j                   ||xs dj                         |xs dj                               }t        |   |       y )Nz2`{}` exited with status {}

STDOUT:
{}

STDERR:
{}r   )r   	exit_coder'   r(   formatdecoderG   r   )r   r   rO   r'   r(   messager"   s         r   r   zFFRuntimeError.__init__   se    "LSSHHi&-C!7!7!9FMc;Q;Q;S
 	!r   )
r   r:   rO   intr'   bytesr(   rT   r=   r>   rI   rK   s   @r   r2   r2      s    
" 
"r   r2   c                    g }| j                         D ]:  \  }}|t        |      z  }|s|r|j                  d       |j                  |       < |S )a  Merge options with their corresponding arguments.

    Iterates over the dictionary holding arguments (keys) and options (values). Merges each
    options string with its corresponding argument.

    :param dict args_opts_dict: a dictionary of arguments and options
    :param dict kwargs: *input_option* - if specified prepends ``-i`` to input argument
    :return: merged list of strings with arguments and their corresponding options
    :rtype: list
    z-i)itemsr   append)args_opts_dictr   mergedargopts        r   r   r      s]     F"((* 	S$S))MM$c	 Mr   c           
         | g S t        | t              rt        j                  |       S |r<t	        t        j                  | D cg c]  }t        j                  |       c}       S t	        |       S c c}w )a^  Normalize options string or list of strings.

    Splits `options` into a list of strings. If `split_mixed` is `True`, splits (flattens) mixed
    options (i.e. list of strings with spaces) into separate items.

    :param options: options string or list of strings
    :param bool split_mixed: whether to split mixed options into separate items
    )
isinstancer:   shlexsplitlist	itertoolschain)optionsr   os      r   r   r      sa     		GS	!{{7##	')JQ%++a.)JKLL=  *Ks   A3)F)rX   z(Mapping[str, Sequence[str] | str | None]r   boolr=   	list[str])rc   r;   r   re   r=   rf   )
__future__r   r-   ra   r^   r   typingr   r   r   r   psutilr   __annotations__r	   ImportErrorr   rE   	Exceptionr/   r2   r   r   rC   r   r   <module>rm      s    "     - -)) Ej" j"Z^f ^2>	 >"Y ",  %< <!E  Es   A. .BB