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 d dlmZ zd dlZdZW n ek
r|   dZY nX eedddZeedddZdddZdS )    N)DEVNULL)get_setting)requires_durationuse_clip_fps_by_default)subprocess_callTFImageMagickOptimizeTransparency   barc                 C   s  t |
}
tj|\}}td| jd| }g }|
d| d |
dd |
jt	t
|dD ]2\}}d||d f }|| | j||d	d
 q^td| }|dkr|
dd tddd| dd|rdnd dd| d| ddd| d dd| g|	dk	rdd|	 gng  |g }n4|dkrNtddd d!d"t|d#|d$ d"t||g}zt||
d% |
d&| d W nR ttfk
r } z.d'|t|f }|dkr|d( }t|W 5 d}~X Y nX |D ]}t| qdS ))a*   Write the VideoClip to a GIF file.


    Converts a VideoClip into an animated GIF using ImageMagick
    or ffmpeg. Does the same as write_gif (see this one for more
    docstring), but writes every frame to a file instead of passing
    them in the RAM. Useful on computers with little RAM.

    r         ?zMoviePy - Building file %s
messagez!MoviePy - - Generating GIF frames)tz%s_GIFTEMP%04d.pngr	   T)withmask      Y@r   z.MoviePy - - Optimizing GIF with ImageMagick...IMAGEMAGICK_BINARY-delay%d-dispose   -loopz%s_GIFTEMP*.png	-coalesce-fuzzz%02d%-layersz%sN-colorsffmpegFFMPEG_BINARY-y-fZimage2-r-iz_GIFTEMP%04d.png)loggerzMoviePy - GIF ready: %s.zLMoviePy Error: creation of %s failed because of the following error:

%s.

.zThis error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file config_defaults.py.)proglogdefault_bar_loggerospathsplitextnpZarangedurationZiter_barlist	enumerateappendZ
save_frameintr   strr   IOErrorOSErrorremove)clipfilenamefpsprogramoptfuzzverboseloopdisposecolorsr"   fileNameextttZ	tempfilesir   namedelaycmderrerrorf rF   @/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/video/io/gif_writers.pywrite_gif_with_tempfiles   sp    




    
 	
     

rH   c                 C   s  d| }t |}| jdkr d}tdddddd	d
d	dd| dd| j| jf d|rVdndddg}tttd}tjdkr~d|d< |dkrt	j
|d< t|d< t	j|d|rdnddd| |g f|}n0t	j
|d< t	j
|d< t	j|ddd
ddg f|}|dkrtddd| d d!|	rd"nd# d$d!| dd%g	}|d&kr^|j|d< t|d< t	j||g f|}n(|j|d< t	j
|d< t	j|d'g f|}|rtddd(d!| d) d*|g|
dk	rd+d!|
 gng  |g }|j|d< t|d< t	j|f|}|d,| d- |d.d- z\| j||d/d0d1D ]D\}}|rFd2| j| }t||gd0}|j|  qW nN tk
r } z.d3|t|f }|dkr|d4 }t|W 5 d}~X Y nX |dkr|d5d- |j  |  |dkr|  |r|  |d6| d- dS )7aC   Write the VideoClip to a GIF file, without temporary files.

    Converts a VideoClip into an animated GIF using ImageMagick
    or ffmpeg.


    Parameters
    -----------

    filename
      Name of the resulting gif file.

    fps
      Number of frames per second (see note below). If it
        isn't provided, then the function will look for the clip's
        ``fps`` attribute (VideoFileClip, for instance, have one).

    program
      Software to use for the conversion, either 'ImageMagick' or
      'ffmpeg'.

    opt
      (ImageMagick only) optimalization to apply, either
      'optimizeplus' or 'OptimizeTransparency'.

    fuzz
      (ImageMagick only) Compresses the GIF by considering that
      the colors that are less than fuzz% different are in fact
      the same.


    Notes
    -----

    The gif will be playing the clip in real time (you can
    only change the frame rate). If you want the gif to be played
    slower than the clip you will use ::

        >>> # slow down clip 50% and make it a gif
        >>> myClip.speedx(0.5).write_gif('myClip.gif')

    r   NFr   r   z	-loglevelrD   r   Zrawvideoz-vcodecr    z%.02fz-sz%dx%dz-pix_fmtZrgbaZrgb24r!   -)stdoutstderrstdinnti   creationflagsr   rL   rJ   Z
image2pipeZbmpr   r   r   r   r   r   r	   r   r   )FNzgif:-r   r   r   r   zMoviePy - Building file  %sr   z"MoviePy - - Generating GIF frames.Tuint8)r4   r"   Z
with_timesdtype   zN[MoviePy] Error: creation of %s failed because of the following error:

%s.

.zThis can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file config_defaults.py.z,MoviePy - - Optimizing GIF with ImageMagick.zMoviePy - - File ready: %s.)r#   r$   maskr   whr   r%   r@   spPIPEPopenrJ   iter_framesZ	get_framer(   ZdstackZastyperL   writetostringr/   r.   closewait)r2   r3   r4   r5   r6   r7   r8   r   r9   r:   r;   r"   rA   Zcmd1Zpopen_paramsZproc1Zcmd2Zproc2Zcmd3Zproc3r   framerR   rC   rD   rF   rF   rG   	write_gif^   s    >

      
 

  


    



 
  

 





r^   c                 C   s   |dkrd}t |}ts"td|dkr0| j}|dkr<dnd}tj|d| |||d}	|d| d	 | j||d
dD ]}
|	|
 qvdS )zt
    Writes the gif with the Python library ImageIO (calls FreeImage).

    Parameters
    -----------
    opt

    N   zVWriting a gif with imageio requires ImageIO installed, with e.g. 'pip install imageio'r   Znqr   )r)   	quantizerZpalettesizer9   z(MoviePy - Building file %s with imageio.r   rO   )r4   r"   rP   )	r#   r$   IMAGEIO_FOUNDImportErrorr4   imageiosaverX   Zappend_data)r2   r3   r4   r6   r9   r;   r8   r"   r`   writerr]   rF   rF   rG   write_gif_with_image_io   s$    
rf   )	Nr   r   r	   Tr   TNr
   )
Nr   r   r	   TTr   TNr
   )Nr   r   NTr
   )r%   
subprocessrU   Znumpyr(   r#   Zmoviepy.compatr   Zmoviepy.configr   Zmoviepy.decoratorsr   r   Zmoviepy.toolsr   rc   ra   rb   rH   r^   rf   rF   rF   rF   rG   <module>   sR   
             I                      