o
    ٷi('                     @  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   ?/home/ubuntu/.local/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.   eo_stdout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__)rB   NN)r   r   r   r   r   r   r   r   r%   r>   r?   r@   r    __classcell__r   r   rD   r   rA      s    rA   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   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                   sJ   || _ || _|| _|| _t|}t|}d| j |||}t | d S )Nz2`{}` exited with status {}

STDOUT:
{}

STDERR:
{})r   rG   r)   r+   _safe_decodeformatrC   r    )r   r   rG   r)   r+   stdout_displaystderr_displaymessagerD   r   r   r       s   zFFRuntimeError.__init__)
r   r   rG   rH   r)   rI   r+   rI   r   r   rE   r   r   rD   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)rO   r   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   rY   rZ   list	itertoolschain)rX   r   r   r   r   r      s   	

r   stream_datarI   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)r^   bytesdecode)rb   r   r   r   rJ      s
   
rJ   )F)rO   rP   r   rQ   r   rR   )rX   r   r   rQ   r   rR   )rb   rI   r   r   )
__future__r   r4   r`   rY   r   typingr   r   r   r   psutilr   __annotations__ImportErrorr   r	   rA   	Exceptionr6   r9   r   r   rJ   r   r   r   r   <module>   s*    
m"