
    g,                     \    d Z ddlZddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ  G d d      Zy)z"MoviePy audio reading with ffmpeg.    N)FFMPEG_BINARY)cross_platform_popen_paramsffmpeg_escape_filename)ffmpeg_parse_infosc                   T    e Zd ZdZ	 	 	 	 	 ddZddZd Zd Zd Zd Z	d Z
d	 Zd
 Zy)FFMPEG_AudioReadera  A class to read the audio in either video files or audio files
    using ffmpeg. ffmpeg will read any audio and transform them into
    raw data.

    Parameters
    ----------

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

    buffersize
      The size of the buffer to use. Should be bigger than the buffer
      used by ``write_audiofile``

    print_infos
      Print the ffmpeg infos on the file being read (for debugging)

    fps
      Desired frames per second in the decoded signal that will be
      received from ffmpeg

    nbytes
      Desired number of bytes (1,2,4) in the signal that will be
      received from ffmpeg
    c                    || _         || _        || _        dd|z  z  | _        dd|z  z  | _        || _        t        ||      }|d   | _        |d   | _        || _	        d | _
        t        | j                  | j                  z        | _        t        | j                  dz   |      | _        d | _        d| _        | j#                          | j%                  d       y )Nzs%dle   z	pcm_s%dle)decode_filedurationaudio_bitrate   )filenamenbytesfpsformatcodec	nchannelsr   r   bitrateinfosprocintn_framesmin
buffersizebufferbuffer_startframe
initializebuffer_around)	selfr   r   r   print_infosr   r   r   r   s	            a/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/moviepy/audio/io/readers.py__init__zFFMPEG_AudioReader.__init__)   s     !V, AJ/
""8Ej)_-
	DHHt}}45dmma/<!"1    c                 Z   | j                          |dk7  r2t        d|      }dd||z
  z  dt        | j                        ddd|z  g}ndt        | j                        dg}t        g|z   ddd	| j
                  d
| j                  dd| j                  z  dd| j                  z  dgz   }t        | j                  t        j                  t        j                  t        j                  d      }t        j                  |fi || _        t!        j"                  | j                  |z        | _        y)z!Opens the file, creates the pipe.r   r   z-ssz%.05fz-iz-vnz	-loglevelerrorz-fz-acodecz-arz%dz-ac-)bufsizestdoutstderrstdinN)closer   r   r   r   r   r   r   r   r   r   spPIPEDEVNULLPopenr   nproundpos)r    
start_timeoffseti_argcmdpopen_paramss         r"   r   zFFMPEG_AudioReader.initializeI   s!   

?J'F:./&t}}5& E 1$--@%HE O 

txxt~~% 	$ 3??''''	
 HHS1L1	88DHHz12r$   c                     | j                   j                  j                  | j                  |z  | j                  z        }| j                   j                  j                          | j                  |z   | _        y)ah  Skip a chunk of audio data by reading and discarding the specified number of
        frames from the audio stream. The audio stream is read from the `proc` stdout.
        After skipping the chunk, the `pos` attribute is updated accordingly.

        Parameters
        ----------
        chunksize (int):
          The number of audio frames to skip.
        N)r   r)   readr   r   flushr3   )r    	chunksize_s      r"   
skip_chunkzFFMPEG_AudioReader.skip_chunkz   sV     II!!$..9"<t{{"JK		 88i'r$   c                    t        t        |            }| j                  j                  j	                  | j
                  |z  | j                  z        }dddd| j                     }t        t        d      rt        j                  ||      }nt        j                  ||      }d|z  dd	| j                  z  d
z
  z  z  j                  t        t        |      | j
                  z        | j
                  f      }t        j                  |t        |      z
  | j
                  f|j                        }t        j                  ||g      }| j                   |z   | _        |S )a  Read a chunk of audio data from the audio stream.

        This method reads a chunk of audio data from the audio stream. The
        specified number of frames, given by `chunksize`, is read from the
        `proc` stdout. The audio data is returned as a NumPy array, where
        each row corresponds to a frame and each column corresponds to a
        channel. If there is not enough audio left to read, the remaining
        portion is padded with zeros, ensuring that the returned array has
        the desired length. The `pos` attribute is updated accordingly.

        Parameters
        ----------
        chunksize (float):
          The desired number of audio frames to read.

        int8int16int32)r         
frombuffer)dtype      ?rC   r
   r   )r   r2   r   r)   r:   r   r   hasattrr1   rE   
fromstringreshapelenzerosrF   concatenater3   )r    r<   s	data_typeresultpads         r"   
read_chunkzFFMPEG_AudioReader.read_chunk   s   $ i()	II!!$..9"<t{{"JK7w7D	2|$]]1I6F]]1I6F,q4;;':!;;DDVt~~-.?
 hh	CK/@U.88i'r$   c                    || j                   k  s|| j                   dz   kD  r+d|z  | j                  z  }| j                  |       || _         y|| j                   kD  r| j                  || j                   z
         || _         y)a*  Read a frame at time t. Note for coders: getting an arbitrary
        frame in the video with ffmpeg can be painfully slow if some
        decoding has to be done. This function tries to avoid fectching
        arbitrary frames whenever possible, by moving between adjacent
        frames.
        i@B rG   N)r3   r   r   r>   )r    r3   ts      r"   seekzFFMPEG_AudioReader.seek   sn     $((Ntxx''9 :c	DHH$AOOA  488^OOC$((N+r$   c                    t        |t        j                        r|dk\  || j                  k  z  }|j	                         s6t        d| j                  z  d|d   |d   fz  z   d| j                  z  z         t        j                  | j                  |z        j                  t              |   }|j                         |j                         }}|| j                  dz  z   }t        j                  ||d      }|t        |      k7  r?|d| }||d	 }	t        j                   | j#                  |      | j#                  |	      g      S d|| j$                  z
  cxk  rt        | j&                        k  sn | j)                  |       n>d|| j$                  z
  cxk  rt        | j&                        k  sn | j)                  |       	 t        j*                  t        |      | j,                  f      }
|| j$                  z
  }| j&                  |   |
|<   |
S t        | j                  |z        }|dk  s|| j8                  kD  rt        j*                  | j,                        S d|| j$                  z
  cxk  rt        | j&                        k  sn | j)                  |       | j&                  || j$                  z
     S # t.        $ r}t1        j2                  d| j                  z  d
|d   |d   fz  z   dj                         |j                         fz  z   dt        | j&                        z  z   t5        |      z   t6               t        | j&                        dz
  ||t        | j&                        k\  <   | j&                  |   
|<   |
cY d	}~S d	}~ww xY w)a  Retrieve the audio frame(s) corresponding to the given timestamp(s).

        Parameters
        ----------
        tt (float or numpy.ndarray):
          The timestamp(s) at which to retrieve the audio frame(s).
          If `tt` is a single float value, the frame corresponding to that
          timestamp is returned. If `tt` is a NumPy array of timestamps, an
          array of frames corresponding to each timestamp is returned.
        r   zError in file %s, z&Accessing time t=%.02f-%.02f seconds, zwith clip duration=%f seconds, rC   right)sideNzAt time t=%.02f-%.02f seconds, zindices wanted: %d-%d, zbut len(buffer)=%d
r   )
isinstancer1   ndarrayr   anyIOErrorr   r2   r   astyper   r   maxr   searchsortedrK   rM   	get_framer   r   r   rL   r   
IndexErrorwarningswarnstrUserWarningr   )r    ttin_timeframesfr_minfr_maxmax_frame_thresholdthreshold_idxin_time_headin_time_tailrP   indicesr&   inds                 r"   ra   zFFMPEG_AudioReader.get_frame   s)    b"**% Qw2#56G ;;=(DMM:>"Q%BPQ7$--GH  XXtxx"}.55c:7CF#ZZ\6::<FF
 #)4??a+?"?OOF4GgVMF+&q7&}~6~~^^L14>>,3OP  &4#9#99MS=MM""6*FT%;%;;Os4;;?OO""6*3r7DNN";< 4#9#99"&++g"6w$ dhhm$CQw#-xx//# 6 66J#dkk:JJ""3' ;;sT%;%;;<<3  (DMM:72a5"R&/IJ/7;;='++-2PPQ -DKK0@AB %j	!
   8;4;;7G!7K3t{{#334"&++g"6ws    AJ6 6	N?CNNNc                 L   t        d|| j                  dz  z
        }| j                  | j                  | j                  z   }||cxk  r|| j                  z   k  rZn nW||z
  }| j                  |z
  }| j	                  |      }t        j                  | j                  | d |g      | _        || _        y| j                  |       | j	                  | j                        | _        || _        y| j                  |       | j	                  | j                        | _        || _        y)zBFill the buffer with frames, centered on frame_number if possible.r   rC   N)r_   r   r   r   rR   r1   vstackrU   )r    frame_numbernew_bufferstartcurrent_f_end	conservedr<   arrays          r"   r   z FFMPEG_AudioReader.buffer_around  s     a10D!DE;;" 22T__DMR4??1RR)O;	 OOi7		2 iiiZ[)A5(IJ "1 		/*"oodoo>
 "1 IIo&//$//:DK!0r$   c                 X   | j                   r| j                   j                         || j                   j                          | j                   j                  j	                          | j                   j
                  j	                          | j                   j                          d| _         yy)z@Closes the reader, terminating the subprocess if is still alive.N)r   poll	terminater)   r,   r*   waitr    s    r"   r,   zFFMPEG_AudioReader.close$  sp    99yy~~'		##%		  &&(		  &&(		 DI r$   c                 $    | j                          y )N)r,   r}   s    r"   __del__zFFMPEG_AudioReader.__del__.  s    

r$   N)FFiD  rC   rC   )r   )__name__
__module____qualname____doc__r#   r   r>   rR   rU   ra   r   r,   r    r$   r"   r   r      sJ    > @/3b(#JP=d1,r$   r   )r   
subprocessr-   rc   numpyr1   moviepy.configr   moviepy.toolsr   r   moviepy.video.io.ffmpeg_readerr   r   r   r$   r"   <module>r      s&    (    ( M =c cr$   