
    Ch                        d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	 ddl
mZmZ  e	d       ed      	 dd              Z e	d	      	 	 	 dd
       Z e	d      dd       Z e	d      dd       Z e	d      	 dd       Zd Zd Zy)z!Miscellaneous bindings to ffmpeg.    N)FFMPEG_BINARYFFPLAY_BINARY)convert_parameter_to_secondsconvert_path_to_string)ffmpeg_escape_filenamesubprocess_call)	inputfile
outputfile)
start_timeend_timec                 &   |sLt         j                  j                  |       \  }}||fD cg c]  }t        d|z         c}\  }}	d|||	|fz  }t        ddd|z  dt        |       dd||z
  z  dd	d
ddddt        |      g}
t        |
|       yc c}w )a  Makes a new video file playing video file between two times.

    Parameters
    ----------

    inputfile : str
      Path to the file from which the subclip will be extracted.

    start_time : float
      Moment of the input clip that marks the start of the produced subclip.

    end_time : float
      Moment of the input clip that marks the end of the produced subclip.

    outputfile : str, optional
      Path to the output file. Defaults to
      ``<inputfile_name>SUB<start_time>_<end_time><ext>``.
    i  z%sSUB%d_%d%s-yz-ssz%0.2f-iz-tz-map0-vcodeccopy-acodecz-copytsloggerN)ospathsplitextintr   r   r   )r	   r   r   r
   r   nameexttt1t2cmds              h/var/www/html/Telegram-Engagement-Pod-/env/lib/python3.12/site-packages/moviepy/video/io/ffmpeg_tools.pyffmpeg_extract_subclipr!      s    . GG$$Y/	c*4h)?@)?A#dQh-)?@B#tRS&99
 	*y)8j()z*!C$ C'+ As   B)	videofile	audiofiler
   c                 v    t         ddt        |      dt        |       d|d|t        |      g}t        ||       y)a  Merges video file and audio file into one movie file.

    Parameters
    ----------

    videofile : str
      Path to the video file used in the merge.

    audiofile : str
      Path to the audio file used in the merge.

    outputfile : str
      Path to the output file.

    video_codec : str, optional
      Video codec used by FFmpeg in the merge.

    audio_codec : str, optional
      Audio codec used by FFmpeg in the merge.
    r   r   r   r   r   Nr   r   r   )r"   r#   r
   video_codecaudio_codecr   r   s          r    ffmpeg_merge_video_audior(   =   sG    < 	y)y)z*C C'    c                 l    t         ddt        |       dd|z  dd|z  t        |      g	}t        ||       y)	a  Extract the sound from a video file and save it in ``outputfile``.

    Parameters
    ----------

    inputfile : str
      The path to the file from which the audio will be extracted.

    outputfile : str
      The path to the file to which the audio will be stored.

    bitrate : int, optional
      Bitrate for the new audio file.

    fps : int, optional
      Frame rate for the new audio file.
    r   r   z-abz%dkz-arz%dr   Nr%   )r	   r
   bitratefpsr   r   s         r    ffmpeg_extract_audior-   k   sD    ( 	y)s
z*
C C'r)   c                 p    t         dt        |       dd|d   |d   fz  t        |      g}t        ||       y)a+  Resizes a file to new size and write the result in another.

    Parameters
    ----------

    inputfile : str
      Path to the file to be resized.

    outputfile : str
      Path to the output file.

    size : list or tuple
      New size in format ``[width, height]`` for the output file.
    r   -vfzscale=%d:%dr      r   Nr%   )r	   r
   sizer   r   s        r    ffmpeg_resizer2      sE    " 	y)a$q'**z*C C'r)   )r	   r
   
output_dirc                 T   |sHt         j                  j                  |       }t         j                  j                  |      \  }}| d| }t         j                  j	                  ||      }t
        dt        |       ddt        |      g}|r|j                  d       t        ||       y)a>  
    Stabilizes ``filename`` and write the result to ``output``.

    Parameters
    ----------

    inputfile : str
      The name of the shaky video.

    outputfile : str, optional
      The name of new stabilized video. Defaults to appending '_stabilized' to
      the input file name.

    output_dir : str, optional
      The directory to place the output video in. Defaults to the current
      working directory.

    overwrite_file : bool, optional
      If ``outputfile`` already exists in ``output_dir``, then overwrite
      ``outputfile`` Defaults to True.
    _stabilizedr   r/   deshaker   r   N)	r   r   basenamer   joinr   r   appendr   )	r	   r
   r3   overwrite_filer   without_dirr   r   r   s	            r    ffmpeg_stabilize_videor<      s    2 gg&&y1GG$$[1	cv[.
j*5Jy)z*C 

4C'r)   c                      t         dddg} t        j                  | ddd      }|j                  j	                         d   j                         d   }t        j                  d|      j                  d      }||fS )	a  
    Retrieve the FFmpeg version.

    This function retrieves both the full and numeric version of FFmpeg
    by executing the `ffmpeg -version` command. The full version includes
    additional details like build information, while the numeric version
    contains only the version numbers (e.g., '7.0.2').

    Return
    ------
    tuple
        A tuple containing:
        - `full_version` (str): The complete version string (e.g., '7.0.2-static').
        - `numeric_version` (str): The numeric version string (e.g., '7.0.2').

    Example
    -------
    >>> ffmpeg_version()
    ('7.0.2-static', '7.0.2')

    Raises
    ------
    subprocess.CalledProcessError
        If the FFmpeg command fails to execute properly.
    -versionz-vquietTcapture_outputtextcheckr      ^[0-9.]+)	r   
subprocessrunstdout
splitlinessplitrematchgroupr   resultfull_versionnumeric_versions       r    ffmpeg_versionrR      sz    6 		C ^^C4tLF ==++-a0668;Lhh{L9??BO/**r)   c                      t         dg} t        j                  | ddd      }|j                  j	                         d   j                         d   }t        j                  d|      j                  d      }||fS )a  
    Retrieve the FFplay version.

    This function retrieves both the full and numeric version of FFplay
    by executing the `ffplay -version` command. The full version includes
    additional details like build information, while the numeric version
    contains only the version numbers (e.g., '6.0.1').

    Return
    ------
    tuple
        A tuple containing:
        - `full_version` (str): The complete version string (e.g., '6.0.1-static').
        - `numeric_version` (str): The numeric version string (e.g., '6.0.1').

    Example
    -------
    >>> ffplay_version()
    ('6.0.1-static', '6.0.1')

    Raises
    ------
    subprocess.CalledProcessError
        If the FFplay command fails to execute properly.
    r>   Tr@   r   rD   rE   )	r   rF   rG   rH   rI   rJ   rK   rL   rM   rN   s       r    ffplay_versionrT      sr    6 	C
 ^^C4tLF==++-a0668;Lhh{L9??BO/**r)   )Nbar)r   r   rU   )i  iD  rU   )rU   )N TrU   )__doc__r   rK   rF   moviepy.configr   r   moviepy.decoratorsr   r   moviepy.toolsr   r   r!   r(   r-   r2   r<   rR   rT    r)   r    <module>r\      s    ' 	 	  7 S A 3489=B,( : 5,(^ @A
 *( B*(Z 34( 5(@ 34( 5(6 ABKP*( C*(Z&+R#+r)   