o
    i-)                     @   s   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	m
Z
 ddlmZ dZejdkr6d	ZndZG d
d de
ZG dd deZG dd deZG dd deZG dd deZG dd dejZdd Zdd Ze ZG dd deZdS )zLRead audio data using the ffmpeg command line tool via its standard
output.
    N)DEFAULT_BUFFER_SIZE   )DecodeError)	AudioFile)ZffmpegZavconvwin32i   c                   @   s   e Zd ZdS )FFmpegErrorN)__name__
__module____qualname__ r   r   ^/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/audioread/ffdec.pyr   &   s    r   c                   @      e Zd ZdZdS )CommunicationErrorz2Raised when the output of FFmpeg is not parseable.Nr   r	   r
   __doc__r   r   r   r   r   *       r   c                   @   r   )UnsupportedErrorz(The file could not be decoded by FFmpeg.Nr   r   r   r   r   r   .   r   r   c                   @   r   )NotInstalledErrorz!Could not find the ffmpeg binary.Nr   r   r   r   r   r   2   r   r   c                   @   r   )ReadTimeoutErrorz4Reading from the ffmpeg command-line tool timed out.Nr   r   r   r   r   r   6   r   r   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	QueueReaderThreadzWA thread that consumes data from a filehandle and sends the data
    over a Queue.
       Fc                    s>   t    || _|| _d| _|| _|rd | _d S t | _d S )NT)super__init__fh	blocksizedaemondiscardqueueQueue)selfr   r   r   	__class__r   r   r   >   s   
zQueueReaderThread.__init__c                 C   s,   	 | j | j}| js| j| |sd S qN)r   readr   r   r   put)r   datar   r   r   runF   s   zQueueReaderThread.run)r   F)r   r	   r
   r   r   r&   __classcell__r   r   r    r   r   :   s    r   c              	   O   sb   t | D ]*\}}|g| }ztj|g|R i |W   S  ty.   |t| d kr, Y qw dS )ak  Like `subprocess.Popen`, but can try multiple commands in case
    some are not available.

    `commands` is an iterable of command names and `command_args` are
    the rest of the arguments that, when appended to the command name,
    make up the full first argument to `subprocess.Popen`. The
    other positional and keyword arguments are passed through.
    r   N)	enumerate
subprocessPopenOSErrorlen)commandscommand_argsargskwargsicommandcmdr   r   r   popen_multipleP   s   	
r4   c                  C   sD   zt tdgtjtjtd} W n
 ty   Y dS w |   | jdkS )zBDetect whether the FFmpeg backend can be used on this system.
    z-version)stdoutstderrcreationflagsFr   )r4   COMMANDSr)   PIPE
PROC_FLAGSr+   communicate
returncode)procr   r   r   	availablec   s   

r>   c                   @   s^   e Zd ZdZef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 )FFmpegAudioFilez9An audio file decoded by the ffmpeg command-line utility.c                 C   s.  t jd}|r"t  d}dd l}|jj|}|jj||B  z;zt	t
d|dddgtjtjtjtd| _W n
 tyB   t w W |r]zdd l}|jj| W t  n t  w n|rwzdd l}|jj| W t  w t  w w t| jj|| _| j  |   t| jj| _| j  d S )	Nwin   r   z-iz-fZs16le-)r5   r6   stdinr7   )sysplatform
startswithwindows_error_mode_lockacquirectypesZwindllZkernel32ZSetErrorModer4   r8   r)   r9   DEVNULLr:   r=   r+   r   releaser   r5   stdout_readerstart	_get_infor6   stderr_reader)r   filename
block_sizewindowsZSEM_NOGPFAULTERRORBOXrI   Zprevious_error_moder   r   r   r   |   sN   		
zFFmpegAudioFile.__init__      $@c              	   c   s    t   }	 d}z| jjj|d}|r|V  nW dS W n' tjyB   t   }|s@|| |kr<tdd| jjj|}Y qY nw q)z*Read blocks of raw PCM data from the file.TN)timeoutzffmpeg output: {}    )	timerL   r   getEmptyr   formatjoinrO   )r   rT   
start_timer%   end_timer   r   r   	read_data   s,   zFFmpegAudioFile.read_datac                 C   s   g }	 | j j }|stdt|tr|dd}|  }d|v r(t	dd|v r/t
 d|v r9|| nd	|v rL|| | d
| dS q)zrReads the tool's output from its stderr stream, extracts the
        relevant information, and parses it.
        Tzstream info not foundutf8ignorezno such filezfile not foundzinvalid data foundz	duration:zaudio: N)r=   r6   readliner   
isinstancebytesdecodestriplowerr+   r   append_parse_inforZ   )r   Z	out_partsliner   r   r   rN      s&   

zFFmpegAudioFile._get_infoc                 C   s   t d|}|rt|d| _nd| _t d|}|rC|d}|dkr)d| _nt d|}|r?ttt| 	d| _nd| _nd| _t d	|}|rvt
tt| }|d d
 d
 |d d
  |d  t|d d  }|| _dS d| _dS )zgGiven relevant data from the ffmpeg output, set audio
        parameter fields on this object.
        z(\d+) hzr   r   zhz, ([^,]+),ZstereorA   z(\d+)\.?(\d)?.z duration: (\d+):(\d+):(\d+).(\d)<      
   N)researchintgroupZ
samplerateZchannelsmatchsummapsplitlistgroupsfloatduration)r   srr   modeZcmatchZdurpartsry   r   r   r   rh      s:   



zFFmpegAudioFile._parse_infoc                 C   s|   t | dr<| j  | jjdu r| j  | j  t | dr$| j  t | dr.| j  | jj	
  | jj
  dS dS )z6Close the ffmpeg process used to perform the decoding.r=   NrO   rL   )hasattrr=   pollr<   killwaitrO   rZ   rL   r5   closer6   r   r   r   r   r     s   







zFFmpegAudioFile.closec                 C   s   |    d S r"   r   r   r   r   r   __del__3  s   zFFmpegAudioFile.__del__c                 C   s   |   S r"   )r]   r   r   r   r   __iter__7  s   zFFmpegAudioFile.__iter__c                 C   s   | S r"   r   r   r   r   r   	__enter__;  s   zFFmpegAudioFile.__enter__c                 C   s   |    dS )NFr   )r   exc_typeexc_valexc_tbr   r   r   __exit__>  s   zFFmpegAudioFile.__exit__N)rS   )r   r	   r
   r   r   r   r]   rN   rh   r   r   r   r   r   r   r   r   r   r?   z   s    
7+r?   )r   r   rn   r)   rD   	threadingrV   ior   
exceptionsr   baser   r8   rE   r:   r   r   r   r   r   Threadr   r4   r>   LockrG   r?   r   r   r   r   <module>   s0   
