
    W|h                     r    S r SSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
   " S S5      r\     S	S j5       rg)
z"MoviePy audio writing with ffmpeg.    N)FFMPEG_BINARY)requires_duration)cross_platform_popen_paramsffmpeg_escape_filenamec                   N    \ rS rSrSr       SS jrS rS rS rS r	S	 r
S
rg)FFMPEG_AudioWriter   a6  
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
                    Uc  [         R                  nXl        Xl        XPl        U R                  R                  S5      S   U l        [        SSU[         R                  :X  a  SOSSSS	U-  -  S
SS	U-  -  SSU-  SSU-  SS/n
Uc  U
R                  S/5        OU
R                  S[        U5      SS/5        U
R                  S
U/SSU-  /-   5        U
R                  SS/5        Ub  U
R                  SU/5        U	b  U
R                  U	5        U
R                  [        U5      /5        [        [         R                  U[         R                  S.5      n[         R                  " U
40 UD6U l        g )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               ]/home/james-whalen/.local/lib/python3.13/site-packages/moviepy/audio/io/ffmpeg_audiowriter.py__init__FFMPEG_AudioWriter.__init__5   sk    ?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                 H    U R                   R                  R                  UR                  5       5        g! [         a  nU R                   R                  5       u  p4Ub  UR                  5       nO5U R                  R                  S5        U R                  R                  5       nU SU R                   SU 3nSU;   a  USU R                   S3-  nO>SU;   a!  US	U R                   S
U R                   S3-  nOSU;   a  US-  nOSU;   a  US-  n[	        U5      eSnAff = f)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_framesFFMPEG_AudioWriter.write_framesl   sV   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 
D! CDD!c                    [        U S5      (       a  U R                  (       a  U R                  R                  R                  5         SU R                  l        U R                  R                  b5  U R                  R                  R                  5         SU R                  l        U R                  R                  5         SU l        ggg)z@Closes the writer, terminating the subprocess if is still alive.r   N)hasattrr   r   closer   waitr    s    r)   r=   FFMPEG_AudioWriter.close   s    4  TYYIIOO!!#"DIIOyy+		  &&(#'		 IINNDI &/ r,   c                 $    U R                  5         g Nr=   r?   s    r)   __del__FFMPEG_AudioWriter.__del__   s    

r,   c                     U $ rB    r?   s    r)   	__enter__FFMPEG_AudioWriter.__enter__   s    r,   c                 $    U R                  5         g rB   rC   )r    exc_type	exc_value	tracebacks       r)   __exit__FFMPEG_AudioWriter.__exit__   s    

r,   )r   r   r   r   r   )   rP   
libfdk_aacNNNN)__name__
__module____qualname____firstlineno____doc__r*   r9   r=   rD   rH   rN   __static_attributes__rG   r,   r)   r   r      s?    &X 52n3!j
r,   r   c
                 V   U(       a  [        US-   S5      n
OSn
[        R                  " U	5      n	U	" SU-  S9  [        UUUU R                  UUU
US9nU R                  USX2U	S9 H  nUR                  U5        M     UR                  5         U(       a  U
R                  5         U	" S	S9  g)
zO
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   r\   r"   
buffersizer   r$   write_logfiler&   r]   r   writerchunks                r)   ffmpeg_audiowriterg      s    " x&($/''/F
2X=>#	F !!tFF "  	E"
 LLN
$%r,   )	libvorbisNFNbar)rV   
subprocessr   r_   moviepy.configr   moviepy.decoratorsr   moviepy.toolsr   r   r   rg   rG   r,   r)   <module>rn      sI    (   ( 0 Mk k\  *& *&r,   