U
    a+dm!                     @   sZ   d dl Z d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 d dlmZ G dd dZdS )    N)DEVNULLPY3)get_setting)ffmpeg_parse_infosc                   @   s\   e Zd ZdZdddZddd	Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd ZdS )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 ``to_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

    FD     c                 C   s   || _ || _|| _dd|  | _dd|  | _|| _t|}|d | _d|krZ|d | _n
|d | _|| _d | _	t
| j| j | _t| jd || _d | _d| _|   | d d S )Nzs%dle   z	pcm_s%dledurationZvideo_duration   )filenamenbytesfpsfacodec	nchannelsr   r
   infosprocintnframesmin
buffersizebufferbuffer_startframe
initializebuffer_around)selfr   r   Zprint_infosr   r   r   r    r   </tmp/pip-unpacked-wheel-0yp4gafk/moviepy/audio/io/readers.py__init__*   s&    

zFFMPEG_AudioReader.__init__r   c                 C   s   |    |dkr<td|}dd||  d| jddd| g}nd| jdg}tdg| dd	d
| jd| jdd| j dd| j dg }| jt	j
t	j
td}tjdkrd|d< t	j|f|| _t| j| | _dS )z# Opens the file, creates the pipe. r   r   z-ssz%.05fz-iz-vnZFFMPEG_BINARYz	-loglevelerrorz-fz-acodecz-arz%dz-ac-)bufsizestdoutstderrstdinnti   creationflagsN)
close_procr   r   r   r   r   r   r   r   spPIPEr   osnamePopenr   nproundpos)r   Z	starttimeoffsetZi_argcmdZpopen_paramsr   r   r   r   C   sB    
        
zFFMPEG_AudioReader.initializec                 C   s6   | j j| j| | j }| j j  | j| | _d S N)r   r#   readr   r   flushr0   )r   	chunksizesr   r   r   
skip_chunkf   s    zFFMPEG_AudioReader.skip_chunkc                 C   s   t t|}| j| | j }| jj|}dddd| j }ttdrVtj	||d}ntj
||d}d| dd	| j d
   t t|| j | jf}| j| | _|S )NZint8Zint16Zint32)r   r      
frombuffer)Zdtype      ?r   r	   r   )r   r/   r   r   r   r#   r4   hasattrr.   r:   
fromstringZreshapelenr0   )r   r6   Lr7   dtresultr   r   r   
read_chunkm   s    
zFFMPEG_AudioReader.read_chunkc                 C   sV   || j k s|| j d kr2d| | j }| | n|| j krL| || j   || _ dS )a4  
        Reads 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 r;   N)r0   r   r   r8   )r   r0   tr   r   r   seek   s    
zFFMPEG_AudioReader.seekc                 C   sP   t | drL| jd k	rL| j  | jj| jjfD ]}|  q.| j  d | _d S )Nr   )r<   r   	terminater#   r$   closewait)r   Zstdr   r   r   r(      s    


zFFMPEG_AudioReader.close_procc              
   C   sb  | j }t|tjr|dk|| jk @ }| sZtd| j d|d |d f  d| j  t| j	| 
t| }| |  }}d|| j   krt| jk sn | | n,d|| j   krt| jk sn | | zdtt|| jf}|| j }t| j| j d k r2|| j d t| j d  }| j| ||< |W S  tk
r }	 ztd| j d|d |d f  d	| | f  d
t| j  t|	 t t| jd ||t| jk< | j| ||< | W Y S d }	~	X Y nX npt| j	| }
|
dk s|
| jkrt| jS d|
| j   krBt| jk sNn | |
 | j|
| j  S d S )Nr   zError in file %s, z&Accessing time t=%.02f-%.02f seconds, zwith clip duration=%d seconds, r   r   zAt time t=%.02f-%.02f seconds, zindices wanted: %d-%d, zbut len(buffer)=%d
)r   
isinstancer.   Zndarrayr
   anyIOErrorr   r/   r   Zastyper   r   maxr   r>   r   r   zerosr   
IndexErrorwarningswarnstrUserWarningr   )r   ttr   Zin_timeframesZfr_minZfr_maxrA   indicesr    indr   r   r   	get_frame   sf    


&
zFFMPEG_AudioReader.get_framec                 C   s   t d|| jd  }| jdk	r| j| j }||  k rD|| j k rn n>|| d }| j| }| |}t| j| d |g| _q| | | | j| _n| | | | j| _|| _dS )z_
        Fills the buffer with frames, centered on ``framenumber``
        if possible
        r   r   Nr   )rL   r   r   r   rB   r.   ZvstackrD   )r   ZframenumberZnew_bufferstartZcurrent_f_endZ	conservedr6   arrayr   r   r   r      s"    





z FFMPEG_AudioReader.buffer_aroundc                 C   s   |    d S r3   )r(   )r   r   r   r   __del__   s    zFFMPEG_AudioReader.__del__N)Fr   r   r   )r   )__name__
__module____qualname____doc__r   r   r8   rB   rD   r(   rW   r   rY   r   r   r   r   r      s         

#	Br   )r+   
subprocessr)   rO   Znumpyr.   Zmoviepy.compatr   r   Zmoviepy.configr   Zmoviepy.video.io.ffmpeg_readerr   r   r   r   r   r   <module>   s   