
    g                     \    d Z ddlZddlmZ ddlmZ ddlmZ  G d d      Z	e	 d	d       Z
y)
z"MoviePy audio writing with ffmpeg.    N)FFPLAY_BINARY)requires_duration)cross_platform_popen_paramsc                   :    e Zd ZdZ	 	 d	dZd Zd Zd Zd Zd Z	y)
FFPLAY_AudioPreviewera  
    A class to preview an AudioClip.

    Parameters
    ----------

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

    nbytes:
      Number of bytes to encode the sound: 1 for 8bit sound, 2 for
      16bit, 4 for 32bit sound. Default is 2 bytes, it's fine.

    nchannels:
      Number of audio channels in the clip. Default to 2 channels.

    c                     t         ddddd|z  z  dd|z  dd|z  d	d
g}t        t        j                  t        j                  t        j
                  d      }t        j                  |fi || _        y )Nz	-autoexitz-nodispz-fzs%dle   z-arz%dz-acz-i-)stdoutstderrstdin)r   r   spDEVNULLSTDOUTPIPEPopenproc)self	fps_inputnbytes	nchannelscmdpopen_paramss         o/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/audio/io/ffplay_audiopreviewer.py__init__zFFPLAY_AudioPreviewer.__init__   sx     q6z"99
 3zzRYYI
 HHS1L1	    c                 |   	 | j                   j                  j                  |j                                y# t        $ r|}| j                   j                         \  }}||j                         }n5| j                  j                  d       | j                  j                         }| d| }t	        |      d}~ww xY w)zASend a raw audio frame (a chunck of audio) to ffplay to be playedNr   zB

MoviePy error: FFPLAY encountered the following error while :

 )
r   r   writetobytesIOErrorcommunicatedecodelogfileseekread)r   frames_arrayerr_ffplay_errorerrors         r   write_framesz"FFPLAY_AudioPreviewer.write_frames9   s    	!IIOO!!,"6"6"89 	!"ii335OA|'+224 !!!$#||002 % %( 
 %. 	!s   36 	B;A7B66B;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FFPLAY_AudioPreviewer.closeN   s    4 TYYIIOO!!#"DIIOyy+		  &&(#'		 IINNDI &/ r   c                 $    | j                          y Nr.   r0   s    r   __del__zFFPLAY_AudioPreviewer.__del__Z   s    

r   c                     | S r2    r0   s    r   	__enter__zFFPLAY_AudioPreviewer.__enter__`   s    r   c                 $    | j                          y r2   r3   )r   exc_type	exc_value	tracebacks       r   __exit__zFFPLAY_AudioPreviewer.__exit__c   s    

r   N)   r=   )
__name__
__module____qualname____doc__r   r+   r.   r4   r7   r<   r6   r   r   r   r   
   s-    , 	26!*
r   r   c                 B   |s| j                   sd}n| j                   }t        ||| j                        5 }d}| j                  |d||      D ];  }|r&d}||j	                          ||j                          |j                  |       = 	 ddd       y# 1 sw Y   yxY w)a  
    A function that wraps the FFPLAY_AudioPreviewer to preview an AudioClip

    Parameters
    ----------

    fps
       Frame rate of the sound. 44100 gives top quality, but may cause
       problems if your computer is not fast enough and your clip is
       complicated. If the sound jumps during the preview, lower it
       (11025 is still fine, 5000 is tolerable).

    buffersize
      The sound is not generated all at once, but rather made by bunches
      of frames (chunks). ``buffersize`` is the size of such a chunk.
      Try varying it if you meet audio problems (but you shouldn't
      have to).

    nbytes:
      Number of bytes to encode the sound: 1 for 8bit sound, 2 for
      16bit, 4 for 32bit sound. 2 bytes is fine.

    audio_flag, video_flag:
      Instances of class threading events that are used to synchronize
      video and audio during ``VideoClip.preview()``.
    iD  T)	chunksizequantizer   fpsFN)rE   r   r   iter_chunkssetr/   r+   )	cliprE   
buffersizer   
audio_flag
video_flag	previewerfirst_framechunks	            r   ffplay_audiopreviewrO   g   s    < xxC((C	sFDNN	; *y%% 4C & 
 	*E #)NN$)OO%""5)	** * *s   ABB)Ni  r=   NN)rA   
subprocessr   moviepy.configr   moviepy.decoratorsr   moviepy.toolsr   r   rO   r6   r   r   <module>rT      s8    (  ( 0 5Z Zz KO2* 2*r   