o
    +i                     @   s\   d dl Z d dlZd dlZddlmZ G dd dejZdd Zdd	d
Zdd Z	dd Z
dS )    N   )loggerc                   @   s>   e Zd ZdZdd Zdd Zedd Zdd	d
Zdd Z	dS )
LogCatcherzThread 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                 C   s<   || _ d| _g | _d| _tj|  d| _d| _| 	  d S )N     TF)
_file_header_lines
_remainder	threadingThread__init__daemon_should_stopstart)selffile r   J/var/www/html/venv/lib/python3.10/site-packages/imageio_ffmpeg/_parsing.pyr      s   zLogCatcher.__init__c                 C   s
   d| _ d S )NT)r   r   r   r   r   stop_me   s   
zLogCatcher.stop_mec                 C   s   | j S )z>Get header text. Empty string if the header is not yet parsed.)r   r   r   r   r   header   s   zLogCatcher.headerr   c                 C   sh   |dkr#t   | }|  r#t   |k r#t d |  r#t   |k sd| j}| jd |dd S )aG  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linesr   r   r   get_text!   s   

zLogCatcher.get_textc                 C   s   t }| jsitd z| jd}W n	 ty   Y nNw |snJ|dddd}|d}| j	|d  |d< |
d| _	| j| | js]t| jr\d| j}|  j|dd7  _n	| jrf|| j| _| jrz| j  W d S  ty{   Y d S w )	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   r   r   r   run3   s8   


zLogCatcher.runN)r   )
__name__
__module____qualname____doc__r   r   propertyr   r$   r4   r   r   r   r   r      s    


r   c                 C   sH   d}| D ]}|  }|drd}q|r!|dr!d|v r!|  S qdS )z[Get 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_outputr3   sliner   r   r   r/   V   s   
r/       c                 C   s(   t | d| krdg| | d  } | S )z(When number of lines > 2*N, reduce to N.   s#   ... showing only last few lines ...N)len)r#   Nr   r   r   r(   d   s   r(   c                  G   s|   t | dkrt| d S t | dkr dt| d  t| d  S t | dkr<dt| d  dt| d   t| d  S dS )zZconverts a time to second. Either cvsecs(min, secs) or
    cvsecs(hours, mins, secs).
    r   r   r@   <      i  N)rA   float)argsr   r   r   cvsecsk   s   ,rG   c                 C   s|  |   }i }|d ddd dd }| d |d   |d< dd	 |D }|d }|d
dd  ddd  |d< td|d
dd d  |d< dd	 |D }t|dkry|d }|ddd  ddd  |d< d}|d fD ]}td|}	|	rt|	d d  }q||d< |d }td|}
||
	 |

 d  d}ttt||d< |d }td|}
||
	 |

 d  d}ttt||d< |d |d krtd|d |d  td}|| }
d}|
d ur	|
 d }t||d< dd	 |D d }td|}
d}|
d ur8||
	 d |

  d}t| }||d< |S )Nr   versionr   r'   	Copyright ffmpeg_versionc                 S   &   g | ]}|  d rd|v r|qS )Stream z Video: r:   .0lr   r   r   
<listcomp>       z'parse_ffmpeg_header.<locals>.<listcomp>zVideo: codecz,\s*(?![^()]*\))pix_fmtc                 S   rL   )rM   z Audio: r:   rN   r   r   r   rQ      rR   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]+)rotatec                 S   s   g | ]}d |v r|qS )z
Duration: r   rN   r   r   r   rQ      s    z, [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]:duration)
splitlinesr,   stripr;   rerA   findallrE   searchr   endtuplemapintr   warningformatcompilegroupsrG   )textr#   metaver
videolinesr3   
audiolines
audio_linerV   matchesmatchparts
reo_rotaterZ   r\   hmsr   r   r   parse_ffmpeg_headerw   sx   (		"



ru   )r?   )r_   r   r   _utilsr   r   r   r/   r(   rG   ru   r   r   r   r   <module>   s    N
