
    Fg$                        S SK Jr  S SKrS SKrS SKrS SKrS SKJrJrJ	r	J
r
   " S S5      r " S S\5      r " S S	\5      r " S
 S\5      r S     SS jjrSSS jjrg)    )annotationsN)IOAnyMappingSequencec                  v    \ rS rSrSr    S         S	S jjrS
S jr    S           SS jjrSrg)FFmpeg
   z`Wrapper for various `FFmpeg <https://www.ffmpeg.org/>`_ related applications (ffmpeg,
ffprobe).
Nc                4   Xl         U/U l        U =R                  [        USS9-  sl        Ub  U =R                  [        USS9-  sl        Ub  U =R                  [        U5      -  sl        [        R
                  " U R                  5      U l        SU l        g)a  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        \/Users/admin/workspace/ai/PDFMathTranslate/myenv/lib/python3.13/site-packages/ffmpy/ffmpy.py__init__FFmpeg.__init__   s|    B %L			'DII	II)&TJJIII)'22I**499504    c                T    SU R                   R                  < SU R                  < S3$ )N< >)	__class____name__r   )r   s    r   __repr__FFmpeg.__repr__=   s&    4>>**-Qtxxl!<<r   c                    [         R                  " U R                  4[         R                  X#US.UD6U l        U R                  R                  US9u  pxU R                  R                  S:w  a+  [        U R                  U R                  R                  Xx5      eXx4$ ! [
         a=  nUR                  [        R                  :X  a  [        SU R                   S35      ee SnAff = f)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   Popenr   PIPEr   OSErrorerrnoENOENTFFExecutableNotFoundErrorr   communicate
returncodeFFRuntimeErrorr   )	r   
input_datar(   r)   r*   kwargseo_stdouto_stderrs	            r   run
FFmpeg.run@   s    L	%++		!+TW[aDL "\\55J5G<<""a' 4<<+B+BHWW!!  	ww%,,&/,t>O{0[\\		s   8B 
C$'8CC$)r   r   r   r   )ffmpegNNN)
r   strr   Sequence[str] | str | Noner   /Mapping[str, Sequence[str] | str | None] | Noner   r?   returnNone)r@   r=   )NNNN)r5   zbytes | Noner(   IO | int | Noner)   rB   r*   zMapping[str, str] | Noner6   r   r@   z!tuple[bytes | None, bytes | None])	r#   
__module____qualname____firstlineno____doc__r   r$   r:   __static_attributes__ r   r   r	   r	   
   s     #59BFCG,5,5 3,5 @	,5
 A,5 
,5\=
 $("&"&(,4" 4"  4"  	4"
 &4" 4" 
+4" 4"r   r	   c                  H   ^  \ rS rSrSr   S       SU 4S jjjrSrU =r$ )FFprobew   z=Wrapper for `ffprobe <https://www.ffmpeg.org/ffprobe.html>`_.c                "   > [         TU ]  XUS9  g)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   FFprobe.__init__z   s    & 	JV\]r   rH   )ffprobeNN)r   r=   r   r>   r   r?   r@   rA   r#   rC   rD   rE   rF   r   rG   __classcell__r"   s   @r   rJ   rJ   w   sH    G $59BF	^^ 3^ @	^
 
^ ^r   rJ   c                      \ rS rSrSrSrg)r1      z3Raise when FFmpeg/FFprobe executable was not found.rH   N)r#   rC   rD   rE   rF   rG   rH   r   r   r1   r1      s    =r   r1   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )r4      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                   > Xl         X l        X0l        X@l        SR	                  U R                   X#=(       d    SR                  5       U=(       d    SR                  5       5      n[        TU ]  U5        g )Nz2`{}` exited with status {}

STDOUT:
{}

STDERR:
{}r   )r   	exit_coder(   r)   formatdecoderM   r   )r   r   rX   r(   r)   messager"   s         r   r   FFRuntimeError.__init__   s[    "LSSHHi-C!7!7!9FMc;Q;Q;S
 	!r   )r   rX   r)   r(   )
r   r=   rX   intr(   bytesr)   r^   r@   rA   rP   rR   s   @r   r4   r4      s    
" 
"r   r4   c                    / nU R                  5        HE  u  p4U[        U5      -  nU(       d  M  U(       a  UR                  S5        UR                  U5        MG     U$ )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      sV     F"((*$S))MM$c + Mr   c           
        U c  / $ [        U [        5      (       a  [        R                  " U 5      $ U(       aA  [	        [
        R                  " U  Vs/ s H  n[        R                  " U5      PM     sn6 5      $ [	        U 5      $ s  snf )aJ  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      sh     		GS	!	!{{7##	')J'Q%++a.')JKLL=  *Ks    B)F)rb   z(Mapping[str, Sequence[str] | str | None]r   boolr@   	list[str])rm   r>   r   ro   r@   rp   )
__future__r   r/   rk   rh   r   typingr   r   r   r   r	   rJ   	Exceptionr1   r4   r   r   rH   r   r   <module>rt      sv    "     - -j" j"Z^f ^2>	 >"Y ",  %< <!r   