Skip to content

Commit

Permalink
Add tests for HTML tools (Zulko#1592)
Browse files Browse the repository at this point in the history
* Add tests for HTML tools
  • Loading branch information
mondeja authored May 27, 2021
1 parent 68818f6 commit 9b09ada
Show file tree
Hide file tree
Showing 2 changed files with 295 additions and 31 deletions.
98 changes: 70 additions & 28 deletions moviepy/video/io/html_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# That is because most browsers use a cache system and they won't
# properly refresh the media when the original files are changed.

import inspect
import os
from base64 import b64encode

Expand All @@ -17,7 +18,7 @@
from moviepy.video.VideoClip import ImageClip, VideoClip


try:
try: # pragma: no cover
from IPython.display import HTML

ipython_available = True
Expand All @@ -28,6 +29,10 @@ def __add__(self, other):


except ImportError:

def HTML2(content): # noqa D103
return content

ipython_available = False


Expand All @@ -50,39 +55,64 @@ def __add__(self, other):
def html_embed(
clip, filetype=None, maxduration=60, rd_kwargs=None, center=True, **html_kwargs
):
"""Returns HTML5 code embedding the clip
"""Returns HTML5 code embedding the clip.
clip
Parameters
----------
clip : moviepy.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
filetype : str, optional
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'}``
maxduration : float, optional
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.
rd_kwargs : dict, optional
Keyword arguments for the rendering, like ``dict(fps=15, bitrate="50k")``.
Allow you to give some options to the render process. You can, for
example, disable the logger bar passing ``dict(logger=None)``.
center : bool, optional
If true (default), the content will be wrapped in a
``<div align=middle>`` HTML container, so the content will be displayed
at the center.
html_kwargs
Allow you to give some options, like ``width=260``, ``autoplay=True``,
``loop=1`` etc.
Examples
--------
TODO Create example based on ipython_display examples
>>> from moviepy.editor import *
>>> # later ...
>>> html_embed(clip, width=360)
>>> html_embed(clip.audio)
>>> clip.write_gif("test.gif")
>>> html_embed('test.gif')
>>> clip.save_frame("first_frame.jpeg")
>>> html_embed("first_frame.jpeg")
"""
if rd_kwargs is None:
if rd_kwargs is None: # pragma: no cover
rd_kwargs = {}

if "Clip" in str(clip.__class__):
TEMP_PREFIX = "__temp__"
if isinstance(clip, ImageClip):
filename = TEMP_PREFIX + ".png"
kwargs = {"filename": filename, "with_mask": True}
kwargs.update(rd_kwargs)
argnames = inspect.getfullargspec(clip.save_frame).args
kwargs.update(
{key: value for key, value in rd_kwargs.items() if key in argnames}
)
clip.save_frame(**kwargs)
elif isinstance(clip, VideoClip):
filename = TEMP_PREFIX + ".mp4"
Expand Down Expand Up @@ -174,33 +204,45 @@ def ipython_display(
):
"""Displays clip content in an IPython Notebook.
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.
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.
filetype:
One of 'video','image','audio'. If None is given, it is determined
based on the extension of ``filename``, but this can bug.
Parameters
----------
clip : moviepy.Clip.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.
maxduration
filetype : str, optional
One of ``"video"``, ``"image"`` or ``"audio"``. If None is given, it is
determined based on the extension of ``filename``, but this can bug.
maxduration : float, optional
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.
value (in seconds), to avoid spoiling the browser's cache and the RAM.
t
t : float, optional
If not None, only the frame at time t will be displayed in the notebook,
instead of a video of the clip
instead of a video of the clip.
fps
fps : int, optional
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.
rd_kwargs : dict, optional
Keyword arguments for the rendering, like ``dict(fps=15, bitrate="50k")``.
Allow you to give some options to the render process. You can, for
example, disable the logger bar passing ``dict(logger=None)``.
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.
center : bool, optional
If true (default), the content will be wrapped in a
``<div align=middle>`` HTML container, so the content will be displayed
at the center.
kwargs
Allow you to give some options, like ``width=260``, etc. When editing
looping gifs, a good choice is ``loop=1, autoplay=1``.
Examples
--------
Expand Down
Loading

0 comments on commit 9b09ada

Please sign in to comment.