
    tKg-)                     d   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(  rd	ZndZ G d
 de
      Z G d de      Z G d de      Z G d de      Z G d de      Z G d dej*                        Zd Zd Z ej2                         Z G d de      Zy)zLRead audio data using the ffmpeg command line tool via its standard
output.
    N)DEFAULT_BUFFER_SIZE   )DecodeError)	AudioFile)ffmpegavconvwin32i   c                       e Zd Zy)FFmpegErrorN)__name__
__module____qualname__     S/home/alanp/www/video.onchill/myenv/lib/python3.12/site-packages/audioread/ffdec.pyr   r   &   s    r   r   c                       e Zd ZdZy)CommunicationErrorz2Raised when the output of FFmpeg is not parseable.Nr   r   r   __doc__r   r   r   r   r   *   s    <r   r   c                       e Zd ZdZy)UnsupportedErrorz(The file could not be decoded by FFmpeg.Nr   r   r   r   r   r   .   s    2r   r   c                       e Zd ZdZy)NotInstalledErrorz!Could not find the ffmpeg binary.Nr   r   r   r   r   r   2   s    +r   r   c                       e Zd ZdZy)ReadTimeoutErrorz4Reading from the ffmpeg command-line tool timed out.Nr   r   r   r   r   r   6   s    >r   r   c                   *     e Zd ZdZd fd	Zd Z xZS )QueueReaderThreadzWA thread that consumes data from a filehandle and sends the data
    over a Queue.
    c                     t         |           || _        || _        d| _        || _        |rd | _        y t        j                         | _        y )NT)super__init__fh	blocksizedaemondiscardqueueQueue)selfr!   r"   r$   	__class__s       r   r    zQueueReaderThread.__init__>   s?    "$T
%++-
r   c                     	 | j                   j                  | j                        }| j                  s| j                  j                  |       |sy PN)r!   readr"   r$   r%   put)r'   datas     r   runzQueueReaderThread.runF   s=    77<</D<<

t$ r   )i   F)r   r   r   r   r    r.   __classcell__)r(   s   @r   r   r   :   s    8r   r   c                     t        |       D ]&  \  }}|g|z   }	 t        j                  |g|i |c S  y# t        $ r |t	        |       dz
  k(  r Y Gw xY w)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cmds          r   popen_multipler=   P   sm      )
7i,&	##C9$9&99 *  	CMA%% &	s   6AAc                      	 t        t        dgt        j                  t        j                  t              } | j                          | j                  dk(  S # t        $ r Y yw xY w)zBDetect whether the FFmpeg backend can be used on this system.
    z-version)stdoutstderrcreationflagsr   F)r=   COMMANDSr2   PIPE
PROC_FLAGScommunicate
returncoder4   )procs    r   	availablerH   c   s[    $L????$
 	!##	  s   5A 	A"!A"c                   L    e Zd ZdZefdZddZd Zd Zd Z	d Z
d Zd	 Zd
 Zy)FFmpegAudioFilez9An audio file decoded by the ffmpeg command-line utility.c                 j   t         j                  j                  d      }|rgt        j	                          d}dd l}|j                  j                  j                  |      }|j                  j                  j                  ||z         	 t        t        d|dddgt        j                  t        j                  t        j                  t              | _        	 |r>	 dd l}|j                  j                  j                         t        j%                          t'        | j                  j(                  |      | _        | j*                  j-                          | j/                          t'        | j                  j0                        | _        | j2                  j-                          y # t         $ r t#               w xY w# t        j%                          w xY w# |rX	 dd l}|j                  j                  j                         t        j%                          w # t        j%                          w xY ww xY w)	Nwin   r   z-iz-fs16le-)r?   r@   stdinrA   )sysplatform
startswithwindows_error_mode_lockacquirectypeswindllkernel32SetErrorModer=   rB   r2   rC   DEVNULLrD   rG   r4   r   releaser   r?   stdout_readerstart	_get_infor@   stderr_reader)r'   filename
block_sizewindowsSEM_NOGPFAULTERRORBOXrV   previous_error_modes          r   r    zFFmpegAudioFile.__init__|   s    ,,))%0#++-$*! &&334IJ  MM""//#&;;	6&xw4!! (((DI  6!MM**778KL+335 /tyy/?/?L  " 	
 /tyy/?/?@  "5  	&#%%	& ,335 6!MM**778KL+335+335 s=   
AF& )F= &F::G =GH2)HH2H..H2c              #     K   t        j                          }	 d}	 | j                  j                  j                  |      }|r| ny	 3# t        j                  $ rl t        j                          }|sS||z
  |k\  rGt        dj                  dj                  | j                  j                  j                                    |}Y Y w xY ww)z*Read blocks of raw PCM data from the file.N)timeoutzffmpeg output: {}r   )	timer\   r%   getEmptyr   formatjoinr_   )r'   rf   
start_timer-   end_times        r   	read_datazFFmpegAudioFile.read_data   s      YY[
D!))//33G3DJ    ;; !99;*,7 //B/I/IHHT%7%7%=%=%C%CD0   &.
  !s)   C-A CA:C
C	C

Cc                    g }	 | j                   j                  j                         }|st        d      t	        |t
              r|j                  dd      }|j                         j                         }d|v rt        d      d|v r
t               d|v r|j                  |       n6d|v r2|j                  |       | j                  d	j                  |             y
)zrReads the tool's output from its stderr stream, extracts the
        relevant information, and parses it.
        zstream info not foundutf8ignorezno such filezfile not foundzinvalid data foundz	duration:zaudio: N)rG   r@   readliner   
isinstancebytesdecodestriplowerr4   r   append_parse_infork   )r'   	out_partslines      r   r^   zFFmpegAudioFile._get_info   s     	99##,,.D()@AA $&{{684::<%%'D%.//%-&(($  &T!  &  !34+ r   c                    t        j                  d|      }|r t        |j                  d            | _        nd| _        t        j                  d|      }|rz|j                  d      }|dk(  rd| _        nct        j                  d|      }|r<t        t        t        |j                         j                  d                  | _        nd| _        nd| _        t        j                  d	|      }|r[t        t        t        |j                                     }|d   d
z  d
z  |d   d
z  z   |d   z   t        |d         dz  z   }|| _        yd| _        y)zgGiven relevant data from the ffmpeg output, set audio
        parameter fields on this object.
        z(\d+) hzr   r   zhz, ([^,]+),stereorM   z(\d+)\.?(\d)?.z duration: (\d+):(\d+):(\d+).(\d)<      
   N)researchintgroup
sampleratechannelsmatchsummapsplitlistgroupsfloatduration)r'   sr   modecmatchdurpartsr   s          r   rz   zFFmpegAudioFile._parse_info   s7   
 		+q)!%++a.1DODO 		/1-;;q>Dx !"2D9$'C1E1Ec1J(K$LDM$%DMDM 		/
 CU\\^45Hb 2%b ! hqk"R'(  %DM DMr   c                    t        | d      r| j                  j                          | j                  j                  4| j                  j	                          | j                  j                          t        | d      r| j                  j                          t        | d      r| j                  j                          | j                  j                  j                          | j                  j                  j                          yy)z6Close the ffmpeg process used to perform the decoding.rG   Nr_   r\   )hasattrrG   pollrF   killwaitr_   rk   r\   r?   closer@   r'   s    r   r   zFFmpegAudioFile.close  s    4 
 IINN yy##+		 		  t_-""'')t_-""'')
 II""$II""$/ !r   c                 $    | j                          y r*   r   r   s    r   __del__zFFmpegAudioFile.__del__3  s    

r   c                 "    | j                         S r*   )rn   r   s    r   __iter__zFFmpegAudioFile.__iter__7  s    ~~r   c                     | S r*   r   r   s    r   	__enter__zFFmpegAudioFile.__enter__;  s    r   c                 $    | j                          y)NFr   )r'   exc_typeexc_valexc_tbs       r   __exit__zFFmpegAudioFile.__exit__>  s    

r   N)g      $@)r   r   r   r   r   r    rn   r^   rz   r   r   r   r   r   r   r   r   rJ   rJ   z   s7    C,? 5#n!<8)V%6 r   rJ   )r   r%   r   r2   rQ   	threadingrg   ior   
exceptionsr   baser   rB   rR   rD   r   r   r   r   r   Threadr   r=   rH   LockrT   rJ   r   r   r   <module>r      s     	  
   " # <<7JJ	+ 	= =3{ 3, ,?{ ?	(( ,&$( ))..* Fi Fr   