
    W|h                     n    S SK r S SKrS SKrSSKJr   " S S\R
                  5      rS rS
S jrS r	S	 r
g)    N   )loggerc                   D    \ rS rSrSrS rS r\S 5       rS
S jr	S r
Srg	)
LogCatcher   zThread to keep reading from stderr so that the buffer does not
fill up and stalls the ffmpeg process. On stderr a message is send
on every few frames with some meta information. We only keep the
last ones.
c                     Xl         SU l        / U l        SU l        [        R
                  R                  U 5        SU l        SU l        U R                  5         g )N     TF)
_file_header_lines
_remainder	threadingThread__init__daemon_should_stopstart)selffiles     Q/home/james-whalen/.local/lib/python3.13/site-packages/imageio_ffmpeg/_parsing.pyr   LogCatcher.__init__   sH    
!!$'!

r
   c                     SU l         g )NT)r   r   s    r   stop_meLogCatcher.stop_me   s
     r
   c                     U R                   $ )z>Get header text. Empty string if the header is not yet parsed.)r   r   s    r   headerLogCatcher.header   s     ||r
   c                    US:  a  [         R                   " 5       U-   nU R                  5       (       a_  [         R                   " 5       U:  aF  [         R                  " S5        U R                  5       (       a  [         R                   " 5       U:  a  MF  SR                  U R                  5      nU R
                  S-   UR                  SS5      -   $ )a  Get the whole text written to stderr so far. To preserve
memory, only the last 50 to 100 frames are kept.

If a timeout is given, wait for this thread to finish. When
something goes wrong, we stop ffmpeg and want a full report of
stderr, but this thread might need a tiny bit more time.
r   g{Gz?   

utf-8ignore)timeis_alivesleepjoinr   r   decode)r   timeoutetimeliness       r   get_textLogCatcher.get_text!   s     Q;IIK')E--//diikE&9

4  --//diikE&9 

4;;'||d"U\\'8%DDDr
   c                 d   [         nU R                  (       Gd\  [        R                  " S5         U R                  R                  S5      nU(       d  GO!UR                  SS5      R                  SS5      nUR                  S5      nU R                  US   -   US'   UR                  S5      U l	        U R                  R                  U5        U R                  (       d[  [        U R                  5      (       a@  SR                  U R                  5      nU =R                  UR!                  SS5      -  sl        O(U R                  (       a  U" U R                  5      U l        U R                  (       d  GM\   U R                  R#                  5         g ! [         a     M)  f = f! [$         a     g f = f)	Nr         r!   s   

r#   r$   )limit_linesr   r%   r'   r   read
ValueErrorreplacesplitr   popr   extendr   get_output_video_liner(   r)   close	Exception)r   limit_lines_localliner,   r   s        r   runLogCatcher.run3   sC   '###JJqMzzr* <<u-55guEDJJu%Eq1E!H#iimDOKKu%<<(55"ZZ4FLLFMM'8$DDL/<+ ###2	JJ+  ,  		s#   F 6F" 
FF"
F/.F/)r   r   r   r   r   r   N)r   )__name__
__module____qualname____firstlineno____doc__r   r   propertyr   r-   r?   __static_attributes__ r
   r   r   r      s0    !  E$ r
   r   c                     SnU  HX  nUR                  5       nUR                  S5      (       a  SnM-  U(       d  M6  UR                  S5      (       d  MN  SU;   d  MV  Us  $    g)zSGet the line that defines the video stream that ffmpeg outputs,
and which we read.
Fs   Output Ts   Stream s    Video:N)lstrip
startswith)r,   	in_outputr>   slines       r   r:   r:   V   sV     IJ''IY
++
e0C r
   c                 <    [        U 5      SU-  :  a	  S/X* S -   n U $ )z(When number of lines > 2*N, reduce to N.   s#   ... showing only last few lines ...N)len)r,   Ns     r   r3   r3   d   s*    
5zAE785:ELr
   c                     [        U 5      S:X  a  [        U S   5      $ [        U 5      S:X  a   S[        U S   5      -  [        U S   5      -   $ [        U 5      S:X  a2  S[        U S   5      -  S[        U S   5      -  -   [        U S   5      -   $ g)zRconverts a time to second. Either cvsecs(min, secs) or
cvsecs(hours, mins, secs).
r   r   rO   <      i  N)rP   float)argss    r   cvsecsrW   k   s     4yA~T!W~	TaE$q'N"U47^33	TaeDGn$rE$q'N'::U47^KK 
r
   c                    U R                  5       n0 nUS   R                  SS5      S   R                  S5      S   nUR                  5       S-   US   R                  5       -   US'   U Vs/ s H2  oDR                  5       R	                  S5      (       d  M(  S	U;   d  M0  UPM4     nnUS   nUR                  S
S5      S   R                  5       R                  SS5      S   R                  5       US'   [
        R                  " SUR                  S
S5      S   5      S   R                  5       US'   U Vs/ s H2  oDR                  5       R	                  S5      (       d  M(  SU;   d  M0  UPM4     nn[        U5      S:  aL  US   nUR                  SS5      S   R                  5       R                  SS5      S   R                  5       US'   Sn	US   4 HB  n[
        R                  " SU5      n
U
(       d  M#  [        U
S   S   R                  5       5      n	MD     XS'   US   n[
        R                  " SU5      nXkR                  5       UR                  5       S-
   R                  S5      n[        [        [        U5      5      US'   US   n[
        R                  " SU5      nXkR                  5       UR                  5       S-
   R                  S5      n[        [        [        U5      5      US'   US   US   :w  a,  [        R                   " SR#                  US   US   5      5        [
        R$                  " S5      nUR                  U 5      nSnUb  UR'                  5       S   n[        U5      US'   U Vs/ s H  nSU;   d  M  UPM     snS   n[
        R                  " SU5      nSnUb:  XkR                  5       S-   UR                  5        R                  S5      n[)        U6 nXS'   U$ s  snf s  snf s  snf )Nr   versionr   r2   	Copyright ffmpeg_versionzStream z Video: zVideo: codecz,\s*(?![^()]*\))pix_fmtz Audio: zAudio: audio_codecz ([0-9]+\.?[0-9]*) (fps)fpsz [0-9]*x[0-9]*(,| )xsource_sizesizezIThe frame size for reading {} is different from the source frame size {}.zrotate\s+:\s([0-9]+)rotatez
Duration: z, [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]:duration)
splitlinesr7   striprJ   rK   rerP   findallrU   searchr   endtuplemapintr   warningformatcompilegroupsrW   )textr,   metaverl
videolinesr>   
audiolines
audio_liner`   matchesmatchparts
reo_rotaterd   rf   hmss                    r   parse_ffmpeg_headerr   w   s   OOED
 (..A
&r
*
0
0
=a
@C YY[3.q1AAD	 aHHJ11)<q5  
 a=DJJy!,R0779??QGJPPRDMhh 	

9a $	 		 
 	O aHHJ11)<q5   :]
Y*2.557==c1EaHNNP 	] CA**8$?7
1++-.C   K a=DII+T2Eq177<EC0D b>DII+T2Eq177<ES%)DL Md6l*77=vVd=18	
 34Jd#EF"[DN 2u! 1Au215DIIDdKEH;;=1$uyy{399#>3<K]&f 3s0   +'O/O/O/4'O4O4'O49
O9O9)    )ri   r   r%   _utilsr   r   r   r:   r3   rW   r   rH   r
   r   <module>r      s<    	   K!! K\	LYr
   