U
    a+d                     @   s   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	m
Z
 edZdd Ze d	d
 ZdddZdd Zdd ZdS )    N)	lru_cache)resource_filename   )get_platformFNAME_PER_PLATFORMimageio_ffmpegc                  C   s.   t dd} | r| S t } | r"| S tddS )a=  
    Get the ffmpeg executable file. This can be the binary defined by
    the IMAGEIO_FFMPEG_EXE environment variable, the binary distributed
    with imageio-ffmpeg, an ffmpeg binary installed with conda, or the
    system ffmpeg (in that order). A RuntimeError is raised if no valid
    ffmpeg could be found.
    ZIMAGEIO_FFMPEG_EXENzpNo ffmpeg exe could be found. Install ffmpeg on your system, or set the IMAGEIO_FFMPEG_EXE environment variable.)osgetenv_get_ffmpeg_exeRuntimeError)exe r   9/tmp/pip-unpacked-wheel-hya5gyls/imageio_ffmpeg/_utils.pyget_ffmpeg_exe   s    
r   c                  C   s   t  } tdd}tj|t| d}|rBtj|rBt|rB|S | 	drbtjt
jddd}ntjt
jdd}|rtj|rt|r|S d}t|r|S d S )	Nr   Zbinaries winLibrarybinz
ffmpeg.exeZffmpeg)r   r   r   pathjoinr   getisfile_is_valid_exe
startswithsysprefix)platbin_dirr   r   r   r   r
   '   s    

r
   Fc                 C   st   d }d }d}t jdr0t }| jtjO  _| rLt jdrFd}ntj}d}t	dd
 |krhd }|||dS )Nr   r   i   )r   0falsenoZ IMAGEIO_FFMPEG_NO_PREVENT_SIGINTr   )startupinfocreationflags
preexec_fn)r   platformr   
subprocessZSTARTUPINFOZdwFlagsZSTARTF_USESHOWWINDOWr   setpgrpr	   lower)Zprevent_sigintr!   r#   r"   Zfalsyr   r   r   _popen_kwargsB   s"    r(   c              
   C   sf   | dg}z:t tjd"}tj|f|tjdt  W 5 Q R X W dS  tttj	fk
r`   Y dS X d S )N-versionw)stdoutstderrTF)
openr   devnullr%   
check_callSTDOUTr(   OSError
ValueErrorCalledProcessError)r   cmdnullr   r   r   r   ^   s     r   c                  C   s^   t  } tj| dgft ddd }|jdd }|ddd  d	dd  }|S )
zF
    Get the version of the used ffmpeg executable (as a string).
    r)      
r   r   ignore)errorsversion )r   r%   check_outputr(   splitdecodestriplstrip)r   liner9   r   r   r   get_ffmpeg_versionj   s     $rB   )F)r   r   loggingr%   	functoolsr   pkg_resourcesr   Z_definitionsr   r   	getLoggerloggerr   r
   r(   r   rB   r   r   r   r   <module>   s   


