o
    côj('  ã                   @  sâ   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
 zd dlmZ ded< W n ey;   ejZY nw eZG dd„ dƒZG d	d
„ d
eƒZG dd„ deƒZG dd„ deƒZ	d"d#dd„Zd"d$dd„Zd%d d!„ZdS )&é    )ÚannotationsN)ÚIOÚAnyÚMappingÚSequence)ÚPopenztype[subprocess.Popen | Popen]Úpopenc                   @  sB   e Zd ZdZ				dddd„Zddd„Z				d 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                 C  st   || _ |g| _|  jt|dd�7  _|dur!|  jt|dd�7  _|dur.|  jt|ƒ7  _t | 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   © r   ú\/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/ffmpy/ffmpy.pyÚ__init__   s   !
zFFmpeg.__init__c                 C  s   d| j j›d| j›d�S )Nú<ú ú>)Ú	__class__Ú__name__r   )r   r   r   r   Ú__repr__F   s   zFFmpeg.__repr__Ú
input_dataúbytes | NoneÚstdoutúIO | int | NoneÚstderrÚenvúMapping[str, str] | NoneÚkwargsr   ú!tuple[bytes | None, bytes | None]c           	   
   K  s˜   zt | jftj|||dœ|¤Ž| _W n ty. } z|jtjkr)td| j	› d�ƒ‚‚ d}~ww | jj
|d�\}}| jjdkrHt| 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+   r,   zExecutable 'z' not foundN)Úinputr   )r   r   r   ÚPIPEr   ÚOSErrorÚerrnoÚENOENTÚFFExecutableNotFoundErrorr   ÚcommunicateÚ
returncodeÚFFRuntimeErrorr   )	r   r'   r)   r+   r,   r.   ÚeZo_stdoutZo_stderrr   r   r   ÚrunI   s$   &ÿ
ÿÿ€üz
FFmpeg.run)r
   NNN)
r   r   r   r   r   r   r   r   r   r   )r   r   )NNNN)r'   r(   r)   r*   r+   r*   r,   r-   r.   r   r   r/   )r%   Ú
__module__Ú__qualname__Ú__doc__r    r&   r;   r   r   r   r   r	      s    û
.ûr	   c                      s*   e Zd ZdZ			dd‡ fdd„Z‡  ZS )ÚFFprobez=Wrapper for `ffprobe <https://www.ffmpeg.org/ffprobe.html>`_.ÚffprobeNr   r   r   r   r   r   r   r   c                   s   t ƒ j|||d� 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$   r   r   r    ƒ   s   zFFprobe.__init__)r@   NN)r   r   r   r   r   r   r   r   ©r%   r<   r=   r>   r    Ú__classcell__r   r   rB   r   r?   €   s    ür?   c                   @  s   e Zd ZdZdS )r6   z3Raise when FFmpeg/FFprobe executable was not found.N)r%   r<   r=   r>   r   r   r   r   r6   ™   s    r6   c                      s"   e Zd ZdZd‡ fdd„Z‡  ZS )r9   zêRaise 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                   sJ   || _ || _|| _|| _t|ƒ}t|ƒ}d | j |||¡}tƒ  |¡ d S )Nz2`{}` exited with status {}

STDOUT:
{}

STDERR:
{})r   rE   r)   r+   Ú_safe_decodeÚformatrA   r    )r   r   rE   r)   r+   Zstdout_displayZstderr_displayÚmessagerB   r   r   r    ¤   s   üzFFRuntimeError.__init__)
r   r   rE   rF   r)   rG   r+   rG   r   r   rC   r   r   rB   r   r9   �   s    r9   FÚargs_opts_dictú(Mapping[str, Sequence[str] | str | None]r   Úboolr   ú	list[str]c                 C  sD   g }|   ¡ D ]\}}|t|ƒ7 }|sq|r| d¡ | |¡ q|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)rK   r   ZmergedÚargÚoptr   r   r   r   ½   s   
r   Úoptionsr   r   c                 C  sD   | du rg S t | tƒrt | ¡S |rttjd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                 S  s   g | ]}t  |¡‘qS r   )ÚshlexÚsplit)Ú.0Úor   r   r   Ú
<listcomp>ê   s    z&_normalize_options.<locals>.<listcomp>)Ú
isinstancer   rT   rU   ÚlistÚ	itertoolsÚchain)rS   r   r   r   r   r   Û   s   	

r   Ústream_datarG   r   c                 C  s&   | du rdS t | tƒr| jdd�S | S )z1Convert FFmpeg output to text for error messages.NÚ Úreplace)Úerrors)rY   ÚbytesÚdecode)r]   r   r   r   rH   ï   s
   
rH   )F)rK   rL   r   rM   r   rN   )rS   r   r   rM   r   rN   )r]   rG   r   r   )Ú
__future__r   r4   r[   rT   r   Útypingr   r   r   r   Zpsutilr   Ú__annotations__ÚImportErrorr   r	   r?   Ú	Exceptionr6   r9   r   r   rH   r   r   r   r   Ú<module>   s*    
ÿm"þ