o
    d"                     @   sp   d dl Z d dlZd dlZdZG dd deZG dd deZG dd deZG d	d
 d
eZ	dd Z
dd ZdS )    Nz0.3.1c                   @   s.   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).
    ffmpegNc                 C   s   || _ |g| _|p
g }t|rg }|D ]	}|t|7 }qnt|}|  j|7  _|  jt|dd7  _|  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)add_input_optionN)

executable_cmd_is_sequenceshlexsplit_merge_args_opts
subprocesslist2cmdlinecmdprocess)selfr   global_optionsinputsoutputsnormalized_global_optionsopt r   5D:\Projects\ConvertPro\env\Lib\site-packages\ffmpy.py__init__   s   

zFFmpeg.__init__c                 C   s   d | jj| jS )Nz<{0!r} {1!r}>)format	__class____name__r   )r   r   r   r   __repr__<   s   zFFmpeg.__repr__c              
   K   s   zt j| jft j|||d|| _W n ty. } z|jtjkr)td	| j
 d}~ww | jj|d}| jjdkrJt| j| jj|d |d |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
        )stdinstdoutstderrenvzExecutable '{0}' not foundN)inputr      )r   Popenr   PIPEr   OSErrorerrnoENOENTFFExecutableNotFoundErrorr   r   communicate
returncodeFFRuntimeErrorr   )r   
input_datar   r   r   kwargseoutr   r   r   run?   s.   
z
FFmpeg.run)r   NNN)NNNN)r   
__module____qualname____doc__r   r   r/   r   r   r   r   r      s    
/r   c                       s"   e Zd ZdZd fdd	Z  ZS )FFprobez=Wrapper for `ffprobe <https://www.ffmpeg.org/ffprobe.html>`_.ffprobe Nc                    s   t 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)superr3   r   )r   r   r   r   r   r   r   r   y   s   

zFFprobe.__init__)r4   r5   Nr   r0   r1   r2   r   __classcell__r   r   r7   r   r3   v   s    r3   c                   @   s   e Zd ZdZdS )r'   z3Raise when FFmpeg/FFprobe executable was not found.N)r   r0   r1   r2   r   r   r   r   r'      s    r'   c                       s    e Zd ZdZ fddZ  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``.
    c                    sN   || _ || _|| _|| _d| j ||pd |pd }tt| | d S )Nz6`{0}` exited with status {1}

STDOUT:
{2}

STDERR:
{3}    )	r   	exit_coder   r   r   decoder6   r*   r   )r   r   r;   r   r   messager7   r   r   r      s   zFFRuntimeError.__init__r8   r   r   r7   r   r*      s    r*   c                 C   s   t | do
t| t S )zCheck if the object is a sequence (list, tuple etc.).

    :param object obj: an object to be checked
    :return: True if the object is iterable but is not a string, False otherwise
    :rtype: bool
    __iter__)hasattr
isinstancestr)objr   r   r   r      s   r   c                 K   sb   g }| s|S |   D ]$\}}t|st|pd}||7 }|s q
d|v 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
    r5   r   z-i)itemsr   r   r	   append)args_opts_dictr,   mergedargr   r   r   r   r
      s   
r
   )r%   r   r   __version__objectr   r3   	Exceptionr'   r*   r   r
   r   r   r   r   <module>   s    n
