
    g                     p    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y)
z"MoviePy audio writing with ffmpeg.    N)FFMPEG_BINARY)requires_duration)cross_platform_popen_paramsffmpeg_escape_filenamec                   D    e Zd ZdZ	 	 	 	 	 	 	 d	dZd Zd Zd Zd Zd Z	y)
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.
    Nc
                    |t         j                  }|| _        || _        || _        | 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}
||
j                  dg       n|
j                  dt        |      ddg       |
j                  d
|gdd|z  gz          |
j                  ddg       ||
j                  d|g       |	|
j                  |	       |
j                  t        |      g       t        t         j                  |t         j                  d      }t        j                  |
fi || _        y )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               l/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py__init__zFFMPEG_AudioWriter.__init__5   sq    ?ggG 
==&&s+B/ "'')Gvq6z"1v:&99
  JJwJJ4[A9fUV

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

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

*84562zzWrwwG
 HHS1L1	    c                 r   	 | j                   j                  j                  |j                                y# t        $ r}| j                   j                         \  }}||j                         }n5| j                  j                  d       | j                  j                         }| d| j                   d| }d|v r|d| j                   dz  }t	        |      d|v r+|d	| j                   d
| j                   dz  }t	        |      d|v r|dz  }t	        |      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	!IIOO!!,"6"6"89 /	!"ii335OA|'+224 !!!$#||002 %   $f\ND 
 !L0::>** FTT> %. / 0<?559ZZL A115
 ;CC, %.  )L8' %.  (<7O
 %. _/	!s   36 	D6C2D11D6c                 z   t        | d      r| j                  r| j                  j                  j                          d| j                  _        | j                  j                  5| j                  j                  j                          d| j                  _        | j                  j                          d| _        yyy)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 TYYIIOO!!#"DIIOyy+		  &&(#'		 IINNDI &/ r*   c                 $    | j                          y 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                 $    | j                          y r>   r?   )r   exc_type	exc_value	tracebacks       r(   __exit__zFFMPEG_AudioWriter.__exit__   s    

r*   )   rI   
libfdk_aacNNNN)
__name__
__module____qualname____doc__r)   r7   r:   r@   rC   rH   rB   r*   r(   r   r      s?    &X 52n3!j
r*   r   c
           
      V   |rt        |dz   d      }
nd}
t        j                  |	      }	 |	d|z         t        |||| j                  |||
|      }| j                  |d|||	      D ]  }|j                  |        |j                          |r|
j                           |	d	       y)
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_chunksr7   r:   )clipr   rS   r!   
buffersizer   r#   write_logfiler%   rT   r   writerchunks                r(   ffmpeg_audiowriter^      s    " x&($/''/F
2X=>#	F !!tFF "  # 	E"#
 LLN
$%r*   )	libvorbisNFNbar)rN   
subprocessr   rV   moviepy.configr   moviepy.decoratorsr   moviepy.toolsr   r   r   r^   rB   r*   r(   <module>re      sI    (   ( 0 Mk k\  *& *&r*   