U
    a+dh#                  
   @   sj   d Z ddlZddlZddlZddlmZ ddlmZm	Z	 ddl
mZ G dd dZdddZdddZdS )zW
On the long term this will implement several methods to make videos
out of VideoClips
    N)proglog)DEVNULLPY3)get_settingc                   @   s:   e Zd ZdZdddZdd	 Zd
d Zdd Zdd ZdS )FFMPEG_VideoWritera0   A class for FFMPEG-based video writing.

    A class to write videos using ffmpeg. ffmpeg will write in a large
    choice of formats.

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

    filename
      Any filename like 'video.mp4' etc. but if you want to avoid
      complications it is recommended to use the generic extension
      '.avi' for all your videos.

    size
      Size (width,height) of the output video in pixels.

    fps
      Frames per second in the output video file.

    codec
      FFMPEG codec. It seems that in terms of quality the hierarchy is
      'rawvideo' = 'png' > 'mpeg4' > 'libx264'
      'png' manages the same lossless quality as 'rawvideo' but yields
      smaller files. Type ``ffmpeg -codecs`` in a terminal to get a list
      of accepted codecs.

      Note for default 'libx264': by default the pixel format yuv420p
      is used. If the video dimensions are not both even (e.g. 720x405)
      another pixel format is used, and this can cause problem in some
      video readers.

    audiofile
      Optional: The name of an audio file that will be incorporated
      to the video.

    preset
      Sets the time that FFMPEG will take to compress the video. The slower,
      the better the compression rate. Possibilities are: ultrafast,superfast,
      veryfast, faster, fast, medium (default), slow, slower, veryslow,
      placebo.

    bitrate
      Only relevant for codecs which accept a bitrate. "5000k" offers
      nice results in general.

    withmask
      Boolean. Set to ``True`` if there is a mask in the video to be
      encoded.

    libx264NmediumFc                 C   sl  |	d krt j}	|| _|| _| jdd | _tddd|	t jkrDdnddd	d
d	dd|d |d f d|rldnddd| dddg}|d k	r|d|ddg |d
|d|g |d k	r|| |d k	r|d|g |
d k	r|dt|
g |dkr*|d d dkr*|d d dkr*|ddg ||g t	|	t jd}t
jd krXd!|d"< t j|f|| _d S )#N.FFMPEG_BINARY-yz	-loglevelerrorinfo-frawvideoz-vcodec-s%dx%dr      -pix_fmtrgbargb24z-rz%.02fz-an-i-z-acodeccopyz-presetz-bz-threadsr      Zyuv420pstdoutstderrstdinnt   creationflags)spPIPEfilenamecodecsplitextr   extendstrr   osnamePopenproc)selfr$   sizefpsr%   	audiofilepresetbitratewithmasklogfilethreadsffmpeg_paramscmdpopen_params r:   B/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/video/io/ffmpeg_writer.py__init__D   s         
       
 
 zFFMPEG_VideoWriter.__init__c              
   C   s   z.t r| jj|  n| jj|  W n tk
r } z| j \}}t|d| j	t|f  }d|kr|d| j
  }n@d|kr|d| j
| jf  }n"d|kr|d }nd|kr|d	 }t|W 5 d
}~X Y nX d
S )z Writes one frame in the file.zS

MoviePy error: FFMPEG encountered the following error while writing file %s:

 %ss   Unknown encoderz

The video export failed because FFMPEG didn't find the specified codec for video encoding (%s). Please install this codec or change the codec when calling write_videofile. For instance:
  >>> clip.write_videofile('myvid.webm', codec='libvpx')s   incorrect codec parameters ?a  

The video export failed, possibly because the codec specified for the video (%s) is not compatible with the given extension (%s). Please specify a valid 'codec' argument in write_videofile. This would be 'libx264' or 'mpeg4' for mp4, 'libtheora' for ogv, 'libvpx for webm. Another possible reason is that the audio codec was not compatible with the video codec. For instance the video extensions 'ogv' and 'webm' only allow 'libvorbis' (default) as avideo codec.s   encoder setup failedzr

The video export failed, possibly because the bitrate you specified was too high or too low for the video codec.s   Invalid encoder typezY

The video export failed because the codec or file extension you provided is not a videoN)r   r-   r   writetobytestostringIOErrorcommunicater)   r$   r%   r'   )r.   Z	img_arrayerr_Zffmpeg_errorr   r:   r:   r;   write_frame   s,    


zFFMPEG_VideoWriter.write_framec                 C   s>   | j r4| j j  | j jd k	r*| j j  | j   d | _ d S N)r-   r   closer   waitr.   r:   r:   r;   rF      s    
zFFMPEG_VideoWriter.closec                 C   s   | S rE   r:   rH   r:   r:   r;   	__enter__   s    zFFMPEG_VideoWriter.__enter__c                 C   s   |    d S rE   )rF   )r.   exc_type	exc_value	tracebackr:   r:   r;   __exit__   s    zFFMPEG_VideoWriter.__exit__)r   Nr   NFNNN)	__name__
__module____qualname____doc__r<   rD   rF   rI   rM   r:   r:   r:   r;   r      s   3           
@2r   r   r   FTbarc                 C   s   t |}|rt|d d}nd}|d| d t|| j|||||||
|d
p}t| j| }| j|d|dd	D ]H\}}|rd
| j	| }|j
dkr|d}t||g}|| qpW 5 Q R X |r|  |dd dS )ze Write the clip to a videofile. See VideoClip.write_videofile for details
    on the parameters.
    .logw+NzMoviepy - Writing video %s
)message)r%   r2   r3   r5   r1   r6   r7   Tuint8)loggerZ
with_timesr0   dtype   zMoviepy - Done !)r   Zdefault_bar_loggeropenr   r/   intdurationZiter_framesmaskZ	get_framerX   astypenpZdstackrD   rF   )Zclipr$   r0   r%   r3   r2   r4   Zwrite_logfiler1   verboser6   r7   rW   r5   writerZnframestframer]   r:   r:   r;   ffmpeg_write_video   s8    
    

rd   c           	      C   s   |j dkr|d}tdddd|jdd ddd  d	d
d|jd dkrPdnddd| g}|rpt| d d}ntj}t|tjd}tj	dkrd|d< tj
|f|}|| \}}|jrdd| d|dg}t|~dS )zO Writes an image (HxWx3 or HxWx4 numpy array) to a file, using
        ffmpeg. rV   r   r   r   r   Nr   r
   r   r   r      r   r   r   r   rS   rT   r   r   r    r!   
z[MoviePy] Running : %s
z(WARNING: this command returned an error:utf8)rX   r^   r   shaperZ   r"   r#   r   r*   r+   r,   rA   r?   
returncodejoindecoder@   )	r$   imager5   r8   Zlog_filer9   r-   outrB   r:   r:   r;   ffmpeg_write_image   s>    

     

rn   )
r   Nr   FFNTNNrR   )F)rQ   r*   
subprocessr"   Znumpyr_   r   Zmoviepy.compatr   r   Zmoviepy.configr   r   rd   rn   r:   r:   r:   r;   <module>   s&    8              
$