U
    a+d                     @   s   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mZ dd Z	dd Z
d'ddZdd Zdd Zd(ddZddddgdddgdddgdddiddiddgdddgddddd gddd!gdd"	Zd#D ]Zdd$iee< qd%d& ZdS ))zH
Misc. useful functions that can be used at many places in the program.
    N   )DEVNULLc                 C   s   t j|  t j  dS )z8 Writes and flushes without delay a text in the console N)sysstdoutwriteflush)s r	   1/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/tools.pysys_write_flush   s    r   c                 C   s   | rt | dS )z9 Only prints s (with sys_write_flush) if verbose is True.N)r   )verboser   r	   r	   r
   verbose_print   s    r   barTc                 C   s   t |}|dd ttjtd}tjdkr4d|d< tj| f|}| \}}|j	
  |jr||rl|dd t|dn
|d	d ~d
S )zw Executes the given subprocess command.
    
    Set logger to None or a custom Proglog logger to avoid printings.
    z'Moviepy - Running:
>>> "+ " ".join(cmd))message)r   stderrstdinnti   creationflagsz#Moviepy - Command returned an errorutf8zMoviepy - Command successfulN)proglogZdefault_bar_loggerr   spPIPEosnamePopencommunicater   close
returncodeIOErrordecode)cmdloggerZ
errorprintZpopen_paramsprocouterrr	   r	   r
   subprocess_call   s"    





r%   c                 C   s0   zt | tW S  tk
r*   t | t Y S X dS )z^ Returns true if s is string or string-like object,
    compatible with Python 2 and Python 3.N)
isinstance
basestring	NameErrorstr)objr	   r	   r
   	is_string<   s    r+   c                 C   sN   d}t | r dd | dD } t| ttfs2| S tdd t|t| D S )a   Will convert any time into seconds. 
    
    If the type of `time` is not valid, 
    it's returned as is. 

    Here are the accepted formats::

    >>> cvsecs(15.4)   # seconds 
    15.4 
    >>> cvsecs((1, 21.5))   # (min,sec) 
    81.5 
    >>> cvsecs((1, 1, 2))   # (hr, min, sec)  
    3662  
    >>> cvsecs('01:01:33.045') 
    3693.045
    >>> cvsecs('01:01:33,5')    # coma works too
    3693.5
    >>> cvsecs('1:33,5')    # only minutes and secs
    99.5
    >>> cvsecs('33.5')      # only secs
    33.5
    )r   <   i  c                 S   s   g | ]}t |d dqS ),.)floatreplace).0fr	   r	   r
   
<listcomp>_   s     zcvsecs.<locals>.<listcomp>:c                 s   s   | ]\}}|| V  qd S )Nr	   )r1   Zmultpartr	   r	   r
   	<genexpr>d   s     zcvsecs.<locals>.<genexpr>)r+   splitr&   tuplelistsumzipreversed)timeZfactorsr	   r	   r
   cvsecsE   s    r>   c                    s2   |dkr j }d||f  fdd}|_|S )a   Indicates that a function is deprecated and has a new name.

    `f` is the new function, `oldname` the name of the deprecated
    function, `newname` the name of `f`, which can be automatically
    found.

    Returns
    ========

    f_deprecated
      A function that does the same thing as f, but with a docstring
      and a printed message on call which say that the function is
      deprecated and that you should use f instead.

    Examples
    =========

    >>> # The badly named method 'to_file' is replaced by 'write_file'
    >>> class Clip:
    >>>    def write_file(self, some args):
    >>>        # blablabla
    >>>
    >>> Clip.to_file = deprecated_version_of(Clip.write_file, 'to_file')
    NzThe function ``%s`` is deprecated and is kept temporarily for backwards compatibility.
Please use the new name, ``%s``, instead.c                     s   t d t  | |S )Nz	MoviePy: )warningswarnPendingDeprecationWarning)akwr2   warningr	   r
   fdepr   s    z$deprecated_version_of.<locals>.fdepr)__name____doc__)r2   ZoldnameZnewnamerF   r	   rD   r
   deprecated_version_ofg   s     rI   videoZlibx264Zlibmpeg4Zaac)typecodecZ	libtheoraZlibvpxrK   audioZ	libvorbisZ
libmp3lameZ	pcm_s16leZ	pcm_s24leZ	pcm_s32leZ
libfdk_aac)	Zmp4ZogvZwebmZaviZmovZoggZmp3ZwavZm4a)ZjpgZjpegZpngZbmpZtiffimagec                 C   sB   | t kr| S t  D ] \}}| |dg kr|  S qtdd S )NrL   zThe audio_codec you chose is unknown by MoviePy. You should report this. In the meantime, you can specify a temp_audiofile with the right extension in write_videofile.)extensions_dictitemsget
ValueError)rL   extZinfosr	   r	   r
   find_extension   s    
rT   )r   T)N)rH   r   
subprocessr   r   r?   r   compatr   r   r   r%   r+   r>   rI   rO   rS   rT   r	   r	   r	   r
   <module>   s2   

	"
-




