
    -i                     ~    d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
  G d d          Ze	 	 	 	 	 dd            ZdS )z"MoviePy audio writing with ffmpeg.    N)FFMPEG_BINARY)requires_duration)cross_platform_popen_paramsffmpeg_escape_filenamec                   F    e Zd ZdZ	 	 	 	 	 	 	 ddZd Zd Zd Zd	 Zd
 Z	dS )FFMPEG_AudioWritera  
    A class to write an AudioClip into an audio file.

    Parameters
    ----------

    filename
      Name of any video or audio file, like ``video.mp4`` or ``sound.wav`` etc.

    size
      Size (width,height) in pixels of the output video.

    fps_input
      Frames per second of the input audio (given by the AudioClip being
      written down).

    nbytes : int, optional
      Number of bytes per sample. Default is 2 (16-bit audio).

    nchannels : int, optional
      Number of audio channels. Default is 2 (stereo).

    codec : str, optional
        The codec to use for the output. Default is ``libfdk_aac``.

    bitrate:
      A string indicating the bitrate of the final video. Only
      relevant for codecs which accept a bitrate.

    input_video : str, optional
      Path to an input video file. If provided, the audio will be muxed with this video.
      If not provided, the output will be audio-only.

    logfile : file-like object or None, optional
      A file object where FFMPEG logs will be written. If None, logs are suppressed.

    ffmpeg_params : list of str, optional
      Additional FFMPEG command-line parameters to customize the output.
       
libfdk_aacNc
                    |t           j        }|| _        || _        || _        | j                            d          d         | _        t          dd|t           j        k    rdndddd	|z  z  d
dd	|z  z  dd|z  dd|z  ddg}
||
                    dg           n&|
                    dt          |          ddg           |
                    d
|gdd|z  gz              |
                    ddg           ||
                    d|g           |	|
                    |	           |
                    t          |          g           t          t           j        |t           j        d          }t          j        |
fi || _        d S )N.z-yz	-loglevelerrorinfoz-fzs%dle   z-acodecz	pcm_s%dlez-arz%dz-acz-i-z-vnz-vcodeccopyz-strictz-2z-ab)stdoutstderrstdin)spPIPElogfilefilenamecodecsplitextr   extendr   r   DEVNULLPopenproc)selfr   	fps_inputnbytes	nchannelsr   bitrateinput_videor   ffmpeg_paramscmdpopen_paramss               a/var/www/html/speakWrite/venv/lib/python3.11/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py__init__zFFMPEG_AudioWriter.__init__5   s    ?gG 
=&&s++B/ "'))GGvq6z"1v:&99
  JJwJJ4[AA9fUVVV

Iu%y0@(AABBB

It$%%%JJw'((($JJ}%%%

*84456662zWrwGG
 
 HS11L11			    c                    	 | j         j                            |                                           dS # t          $ r}| j                                         \  }}||                                }n3| j                            d           | j        	                                }| d| j
         d| }d|v r|d| j         dz  }n.d|v r|d	| j         d
| j         dz  }nd|v r|dz  }n	d|v r|dz  }t	          |          d}~ww xY w)zGSend the audio frame (a chunck of ``AudioClip``) to ffmpeg for writtingNr   zK

MoviePy error: FFMPEG encountered the following error while writing file z:

 zUnknown encoderz\

The audio export failed because FFMPEG didn't find the specified codec for audio encoding z. Please install this codec or change the codec when calling write_videofile or write_audiofile.
For instance for mp3:
   >>> write_videofile('myvid.mp4', audio_codec='libmp3lame')zincorrect codec parameters ?zN

The audio export failed, possibly because the codec specified for the video z, is not compatible with the given extension z. Please specify a valid 'codec' argument in write_audiofile or 'audio_codoc'argument in write_videofile. This would be 'libmp3lame' for mp3, 'libvorbis' for ogg...zbitrate not specifiedzr

The audio export failed, possibly because the bitrate you specified was too high or too low for the audio codec.zInvalid encoder typezd

The audio export failed because the codec or file extension you provided is not suitable for audio)r    r   writetobytesIOErrorcommunicatedecoder   seekreadr   r   r   )r!   frames_arrayerr_ffmpeg_errorr   s         r*   write_frameszFFMPEG_AudioWriter.write_framesl   s   1	!IO!!,"6"6"8"899999 /	! /	! /	!"i3355OA|'+2244 !!!$$$#|0022  D D $D D5AD D 
 !L00T:>*T T T 0<??C59ZC C15C C C )L88' (<77O
 %.. _/	!s   15 
D	CDD	c                 <   t          | d          r| j        r| j        j                                         d| j        _        | j        j        *| j        j                                         d| j        _        | j                                         d| _        dS dS dS )z@Closes the writer, terminating the subprocess if is still alive.r    N)hasattrr    r   closer   waitr!   s    r*   r<   zFFMPEG_AudioWriter.close   s    4   	TY 	IO!!###"DIOy+	 &&(((#'	 INNDIII	 	 	 	r,   c                 .    |                                   d S Nr<   r>   s    r*   __del__zFFMPEG_AudioWriter.__del__   s    

r,   c                     | S r@    r>   s    r*   	__enter__zFFMPEG_AudioWriter.__enter__   s    r,   c                 .    |                                   d S r@   rA   )r!   exc_type	exc_value	tracebacks       r*   __exit__zFFMPEG_AudioWriter.__exit__   s    

r,   )r	   r	   r
   NNNN)
__name__
__module____qualname____doc__r+   r9   r<   rB   rE   rJ   rD   r,   r*   r   r      s        & &X 52 52 52 52n3! 3! 3!j
 
 
        r,   r   	libvorbisFbarc
           
         |rt          |dz   d          }
nd}
t          j        |	          }	 |	d|z             t          |||| j        |||
|          }|                     |d|||	          D ]}|                    |           |                                 |r|
                                  |	d	           dS )
z[
    A function that wraps the FFMPEG_AudioWriter to write an AudioClip
    to a file.
    z.logzw+NzMoviePy - Writing audio in %s)message)r   r%   r   r'   T)	chunksizequantizer#   fpsloggerzMoviePy - Done.)openproglogdefault_bar_loggerr   r$   iter_chunksr9   r<   )clipr   rU   r#   
buffersizer   r%   write_logfiler'   rV   r   writerchunks                r*   ffmpeg_audiowriter`      s   "  x&($//'//F
F2X=>>>>#	 	 	F !!tFF "   # # 	E""""
LLNNN 
F$%%%%%%r,   )rO   NFNrP   )rN   
subprocessr   rX   moviepy.configr   moviepy.decoratorsr   moviepy.toolsr   r   r   r`   rD   r,   r*   <module>re      s    ( (      ( ( ( ( ( ( 0 0 0 0 0 0 M M M M M M M Mk k k k k k k k\  *& *& *& *& *& *&r,   