
    Pi('                       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n# e$ r
 ej        ZY nw xY w 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d"dZdS )#    )annotationsN)IOAnyMappingSequence)Popenztype[subprocess.Popen | Popen]popenc                  >    e Zd ZdZ	 	 	 	 dddZddZ	 	 	 	 dddZdS )FFmpegzhWrapper for various `FFmpeg <https://www.ffmpeg.org/>`_ related applications (ffmpeg,
    ffprobe).
    ffmpegN
executablestrglobal_optionsSequence[str] | str | Noneinputs/Mapping[str, Sequence[str] | str | None] | NoneoutputsreturnNonec                ,   || _         |g| _        | xj        t          |d          z  c_        || xj        t          |d          z  c_        || xj        t          |          z  c_        t	          j        | j                  | _        d| _        dS )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)r   _cmd_normalize_options_merge_args_opts
subprocesslist2cmdlinecmdprocess)selfr   r   r   r   s        _/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ffmpy/ffmpy.py__init__zFFmpeg.__init__   s    B %L			'DIIII		II)&TJJJJIIII)'222II*49558<    c                2    d| j         j        d| j        dS )N< >)	__class____name__r   )r    s    r!   __repr__zFFmpeg.__repr__F   s"    <4>*<<tx<<<<r#   
input_databytes | NonestdoutIO | int | NonestderrenvMapping[str, str] | Nonekwargsr   !tuple[bytes | None, bytes | None]c                x   	 t          | j        ft          j        |||d|| _        n@# t
          $ r3}|j        t          j        k    rt          d| j	         d           d}~ww xY w| j        
                    |          \  }}| j        j        dk    r!t          | j        | j        j        ||          ||fS )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
        )stdinr-   r/   r0   zExecutable 'z' not foundN)inputr   )r	   r   r   PIPEr   OSErrorerrnoENOENTFFExecutableNotFoundErrorr   communicate
returncodeFFRuntimeErrorr   )	r    r+   r-   r/   r0   r2   eo_stdouto_stderrs	            r!   runz
FFmpeg.runI   s    L	 	!+TW [a DLL  	 	 	w%,&&/0[t0[0[0[\\\		 "\55J5GG(<"a'' 4<+BHhWWW!!s   &) 
A&.A!!A&)r   NNN)
r   r   r   r   r   r   r   r   r   r   )r   r   )NNNN)r+   r,   r-   r.   r/   r.   r0   r1   r2   r   r   r3   )r)   
__module____qualname____doc__r"   r*   rB    r#   r!   r   r      s          #59BFCG,= ,= ,= ,= ,=\= = = =
 $("&"&(,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>`_.ffprobeNr   r   r   r   r   r   r   r   c                P    t                                          |||           dS )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#   )rI   NN)r   r   r   r   r   r   r   r   r)   rC   rD   rE   r"   __classcell__r(   s   @r!   rH   rH      s^        GG $59BF	^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^r#   rH   c                      e Zd ZdZdS )r;   z3Raise when FFmpeg/FFprobe executable was not found.N)r)   rC   rD   rE   rF   r#   r!   r;   r;      s        ====r#   r;   c                  $     e Zd ZdZd fdZ xZS )r>   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``.
    r   r   	exit_codeintr-   bytes | str | Noner/   r   r   c                    || _         || _        || _        || _        t	          |          }t	          |          }d                    | j         |||          }t                                          |           d S )Nz2`{}` exited with status {}

STDOUT:
{}

STDERR:
{})r   rQ   r-   r/   _safe_decodeformatrK   r"   )	r    r   rQ   r-   r/   stdout_displaystderr_displaymessager(   s	           r!   r"   zFFRuntimeError.__init__   s~     "%f--%f--LSSH	
 
 	!!!!!r#   )
r   r   rQ   rR   r-   rS   r/   rS   r   r   rL   rN   s   @r!   r>   r>      sG         " " " " " " " " " "r#   r>   Fargs_opts_dict(Mapping[str, Sequence[str] | str | None]r   boolr   	list[str]c                    g }|                                  D ]F\  }}|t          |          z  }|s|r|                    d           |                    |           G|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)rZ   r   mergedargopts        r!   r   r      sz     F"((** 	 	S$S))) 	 	 MM$cMr#   optionsr   r   c                    | g S t          | t                    rt          j        |           S |r%t	          t          j        d | D                        S t	          |           S )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
    Nc                6    g | ]}t          j        |          S rF   )shlexsplit).0os     r!   
<listcomp>z&_normalize_options.<locals>.<listcomp>   s     )J)J)JQ%+a..)J)J)Jr#   )
isinstancer   rg   rh   list	itertoolschain)rd   r   s     r!   r   r      sh     		GS	!	! !{7### 	!	)J)J')J)J)JKLLL== r#   stream_datarS   r   c                d    | dS t          | t                    r|                     d          S | S )z1Convert FFmpeg output to text for error messages.N replace)errors)rl   bytesdecode)rp   s    r!   rU   rU      s<    r+u%% 4!!!333r#   )F)rZ   r[   r   r\   r   r]   )rd   r   r   r\   r   r]   )rp   rS   r   r   )
__future__r   r9   rn   rg   r   typingr   r   r   r   psutilr   __annotations__r	   ImportErrorr   rH   	Exceptionr;   r>   r   r   rU   rF   r#   r!   <module>r}      s   " " " " " " "           - - - - - - - - - - - -)))) EE    EEEj" j" j" j" j" j" j" j"Z^ ^ ^ ^ ^f ^ ^ ^2> > > > >	 > > >" " " " "Y " " "D  %    <! ! ! ! !(     s   3 AA