U
    a+d                     @   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   ;/tmp/pip-unpacked-wheel-hya5gyls/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   sV   |dkr4t   | }|  r4t   |k r4t d q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   timeoutZetimelinesr   r   r   get_text!   s    
zLogCatcher.get_textc                 C   s   t }| jstd z| jd}W n tk
r<   Y qY nX |sDq|dddd}|d}| j	|d  |d< |
d| _	| j| | jst| jrd| j}|  j|dd7  _q| jr|| j| _qz| j  W n tk
r   Y nX d S )	Nr         r   s   

r   r   )limit_linesr   r   r   r   read
ValueErrorreplacesplitr
   popr	   extendr   get_output_video_liner   r    close	Exception)r   Zlimit_lines_localliner"   r   r   r   r   run3   s0    



zLogCatcher.runN)r   )
__name__
__module____qualname____doc__r   r   propertyr   r#   r2   r   r   r   r   r      s   


r   c                 C   sH   d}| D ]:}|  }|dr$d}q|r|drd|k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"   Z	in_outputr1   sliner   r   r   r.   W   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'   e   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rxd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)r>   float)argsr   r   r   cvsecsl   s    rD   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r|d }|ddd  ddd  |d< d}|d |d fD ]>}td|}	|	jdd dd |	rt|	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rtd|d |d  td}|	| }
d}|
d k	r0|
 d }t||d< dd	 |D d }t	d|}
d}|
d k	r||

 d |
  d }t| }||d!< |S )"Nr   versionr   r&   Z	Copyright Zffmpeg_versionc                 S   s&   g | ]}|  d rd|kr|qS )Stream z Video: r8   .0lr   r   r   
<listcomp>   s      z'parse_ffmpeg_header.<locals>.<listcomp>zVideo: codecz,\s*(?![^()]*\))Zpix_fmtc                 S   s&   g | ]}|  d rd|kr|qS )rG   z Audio: r8   rH   r   r   r   rK      s      zAudio: Zaudio_codecz ([0-9]+\.?[0-9]*) (tbr|fps)c                 S   s   | d dkS )Nr   Ztbrr   )xr   r   r   <lambda>   r   z%parse_ffmpeg_header.<locals>.<lambda>T)keyreversefpsz [0-9]*x[0-9]*(,| )rM   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 |kr|qS )z
Duration: r   rH   r   r   r   rK      s      z, [0-9][0-9]:[0-9][0-9]:[0-9][0-9].[0-9][0-9]:duration)
splitlinesr+   stripr9   rer>   findallsortrB   searchr   endtuplemapintr   warningformatcompilegroupsrD   )textr"   metaverZ
videolinesr1   Z
audiolinesZ
audio_linerQ   matchesmatchpartsZ
reo_rotaterT   rV   Zhmsr   r   r   parse_ffmpeg_headerx   st    (	" 



rk   )r<   )rY   r   r   _utilsr   r   r   r.   r'   rD   rk   r   r   r   r   <module>   s   O
