U
    a+d                     @   s   d Z ddlZddlm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mZ dZG dd deZW n ek
r   dZY nX dZde d dde d dZdddZdddZdS )zh
This module implements ipython_display
A function to embed images/videos/audio in the IPython Notebook
    N)	b64encode)	AudioClip)extensions_dict   )	ImageClip	VideoClip   )ffmpeg_parse_infos)HTMLTc                   @   s   e Zd Zdd ZdS )HTML2c                 C   s   t | j|j S )N)r   data)selfother r   ?/tmp/pip-unpacked-wheel-0yp4gafk/moviepy/video/io/html_tools.py__add__   s    zHTML2.__add__N)__name__
__module____qualname__r   r   r   r   r   r      s   r   Fz@Sorry, seems like your browser doesn't support HTML5 audio/videozN<audio controls><source %(options)s  src='data:audio/%(ext)s;base64,%(data)s'>z</audio>z:<img %(options)s src='data:image/%(ext)s;base64,%(data)s'>zD<video %(options)ssrc='data:video/%(ext)s;base64,%(data)s' controls>z</video>)audioimagevideo<   c              	   K   s0  |dkri }dt | jkrd}t| trR|d }|dd}|| | jf | nrt| tr|d }|dd	d
}|| | jf | n<t| tr|d }|dd}|| | j	f | nt
dt|f|||d|S | }ddd | D }	tj|\}
}|dd }|dkr`|dd  }|dkr@d}n |tkrXt| d }nt
d|dkrdddd}d| }z|| }W n   t
d| Y nX |dkrt|d  }||krt
d!||f d" t|d#}t| d$}W 5 Q R X t| }|||	|d% }|r,d&| }|S )'a   Returns HTML5 code embedding the clip
    
    clip
      Either a file name, or a clip to preview.
      Either an image, a sound or a video. Clips will actually be
      written to a file and embedded as if a filename was provided.


    filetype
      One of 'video','image','audio'. If None is given, it is determined
      based on the extension of ``filename``, but this can bug.
    
    rd_kwargs
      keyword arguments for the rendering, like {'fps':15, 'bitrate':'50k'}
    

    **html_kwargs
      Allow you to give some options, like width=260, autoplay=True,
      loop=1 etc.

    Examples
    =========

    >>> import moviepy.editor as mpy
    >>> # later ...
    >>> clip.write_videofile("test.mp4")
    >>> mpy.ipython_display("test.mp4", width=360)

    >>> clip.audio.write_audiofile('test.ogg') # Sound !
    >>> mpy.ipython_display('test.ogg')

    >>> clip.write_gif("test.gif")
    >>> mpy.ipython_display('test.gif')

    >>> clip.save_frame("first_frame.jpeg")
    >>> mpy.ipython_display("first_frame.jpeg")

    NZClipZ__temp__z.pngT)filenameZwithmaskz.mp4FZ	ultrafast)r   verbosepresetz.mp3)r   r   z5Unknown class for the clip. Cannot embed and preview.)maxduration	rd_kwargscenter c                 S   s$   g | ]\}}d t |t |f qS )z%s='%s')str).0kvr   r   r   
<listcomp>o   s     zhtml_embed.<locals>.<listcomp>r   .Zgifr   typezNo file type is known for the provided file. Please provide argument `filetype` (one of 'image', 'video', 'sound') to the ipython display function.r   mp4webmZogg)r(   r)   ZogvzVThis video extension cannot be displayed in the IPython Notebook. Allowed extensions: )r   r   durationz:The duration of video %s (%.1f) exceeds the 'maxduration' zattribute. You can increase 'maxduration', by passing 'maxduration' parameterto ipython_display function.But note that embedding large videos may take all the memory away !rbzutf-8)r   optionsextz<div align=middle>%s</div>)r    	__class__
isinstancer   updateZ
save_framer   Zwrite_videofiler   Zwrite_audiofile
ValueError
html_embedjoinitemsospathsplitextsplitlowerr   keysr	   openr   readdecode	templates)clipfiletyper   r   r   html_kwargsZTEMP_PREFIXr   kwargsr,   namer-   Zexts_htmltypeZallowed_extsr*   fr   templateresultr   r   r   r2   +   st    )














r2   c                 K   sX   t std|dkri }|dk	r(||d< |dk	r:| |} tt| f||||d|S )a  
    clip
      Either the name of a file, or a clip to preview. The clip will
      actually be written to a file and embedded as if a filename was
      provided.

    filetype:
      One of 'video','image','audio'. If None is given, it is determined
      based on the extension of ``filename``, but this can bug.

    maxduration
      An error will be raised if the clip's duration is more than the indicated
      value (in seconds), to avoid spoiling the  browser's cache and the RAM.

    t
      If not None, only the frame at time t will be displayed in the notebook,
      instead of a video of the clip

    fps
      Enables to specify an fps, as required for clips whose fps is unknown.
    
    **kwargs:
      Allow you to give some options, like width=260, etc. When editing
      looping gifs, a good choice is loop=1, autoplay=1.
    
    Remarks: If your browser doesn't support HTML5, this should warn you.
    If nothing is displayed, maybe your file or filename is wrong.
    Important: The media will be physically embedded in the notebook.

    Examples
    =========

    >>> import moviepy.editor as mpy
    >>> # later ...
    >>> clip.write_videofile("test.mp4")
    >>> mpy.ipython_display("test.mp4", width=360)

    >>> clip.audio.write_audiofile('test.ogg') # Sound !
    >>> mpy.ipython_display('test.ogg')

    >>> clip.write_gif("test.gif")
    >>> mpy.ipython_display('test.gif')

    >>> clip.save_frame("first_frame.jpeg")
    >>> mpy.ipython_display("first_frame.jpeg")
    z%Only works inside an IPython NotebookNfps)r@   r   r   r   )ipython_availableImportErrorZto_ImageClipr   r2   )r?   r@   r   trG   r   r   rA   r   r   r   ipython_display   s    1
 rK   )Nr   NT)Nr   NNNT)__doc__r5   base64r   Zmoviepy.audio.AudioClipr   Zmoviepy.toolsr   r   r   Zffmpeg_readerr	   ZIPython.displayr
   rH   r   rI   Zsorryr>   r2   rK   r   r   r   r   <module>   s@   

  
t    