Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Add support for webm and jupyter transparency (#1660)

Ryan McCauley7d5c5d3de191
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
index 0fac7f941..ce3e3c0f0 100644
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -864,7 +864,7 @@ class ManimConfig(MutableMapping):
@property
def format(self):
- """File format; "png", "gif", "mp4", or "mov"."""
+ """File format; "png", "gif", "mp4", "webm" or "mov"."""
return self._d["format"]
@format.setter
@@ -873,8 +873,12 @@ class ManimConfig(MutableMapping):
self._set_from_list(
"format",
val,
- [None, "png", "gif", "mp4", "mov"],
+ [None, "png", "gif", "mp4", "mov", "webm"],
)
+ if self.format == "webm":
+ logging.getLogger("manim").warning(
+ "Output format set as webm, this can be slower than other formats"
+ )
ffmpeg_loglevel = property(
lambda self: self._d["ffmpeg_loglevel"],
@@ -1008,9 +1012,9 @@ class ManimConfig(MutableMapping):
movie_file_extension = property(
lambda self: self._d["movie_file_extension"],
lambda self, val: self._set_from_list(
- "movie_file_extension", val, [".mp4", ".mov"]
+ "movie_file_extension", val, [".mp4", ".mov", ".webm"]
),
- doc="Either .mp4 or .mov (no flag).",
+ doc="Either .mp4, .webm or .mov.",
)
background_opacity = property(
@@ -1056,12 +1060,11 @@ class ManimConfig(MutableMapping):
def transparent(self, val: bool) -> None:
if val:
self.png_mode = "RGBA"
- self.movie_file_extension = ".mov"
self.background_opacity = 0.0
else:
self.png_mode = "RGB"
- self.movie_file_extension = ".mp4"
self.background_opacity = 1.0
+ self.resolve_movie_file_extension(val)
@property
def dry_run(self):
@@ -1147,6 +1150,16 @@ class ManimConfig(MutableMapping):
doc="Main output directory. See :meth:`ManimConfig.get_dir`.",
)
+ def resolve_movie_file_extension(self, is_transparent):
+ if is_transparent:
+ self.movie_file_extension = ".webm" if self.format == "webm" else ".mov"
+ elif self.format == "webm":
+ self.movie_file_extension = ".webm"
+ elif self.format == "mov":
+ self.movie_file_extension = ".mov"
+ else:
+ self.movie_file_extension = ".mp4"
+
enable_gui = property(
lambda self: self._d["enable_gui"],
lambda self, val: self._set_boolean("enable_gui", val),
diff --git a/manim/cli/render/render_options.py b/manim/cli/render/render_options.py
index 991ec94f7..d473fd057 100644
--- a/manim/cli/render/render_options.py
+++ b/manim/cli/render/render_options.py
@@ -49,7 +49,7 @@ render_options = option_group(
),
option(
"--format",
- type=click.Choice(["png", "gif", "mp4"], case_sensitive=False),
+ type=click.Choice(["png", "gif", "mp4", "webm", "mov"], case_sensitive=False),
),
option("-s", "--save_last_frame", is_flag=True),
option(
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index e395ae5bf..d79534fd6 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -23,6 +23,7 @@ from ..utils.file_ops import (
guarantee_existence,
is_gif_format,
is_png_format,
+ is_webm_format,
modify_atime,
write_to_movie,
)
@@ -392,7 +393,10 @@ class SceneFileWriter(object):
]
if config.renderer == "opengl":
command += ["-vf", "vflip"]
- if config["transparent"]:
+ if is_webm_format():
+ command += ["-vcodec", "libvpx-vp9", "-auto-alt-ref", "0"]
+ # .mov format
+ elif config["transparent"]:
command += ["-vcodec", "qtrle"]
else:
command += ["-vcodec", "libx264", "-pix_fmt", "yuv420p"]
diff --git a/manim/utils/file_ops.py b/manim/utils/file_ops.py
index 1f1cf0358..bf204ad4d 100644
--- a/manim/utils/file_ops.py
+++ b/manim/utils/file_ops.py
@@ -9,6 +9,8 @@ __all__ = [
"is_mp4_format",
"is_gif_format",
"is_png_format",
+ "is_webm_format",
+ "is_mov_format",
"write_to_movie",
]
@@ -50,6 +52,32 @@ def is_gif_format() -> bool:
return config["format"] == "gif"
+def is_webm_format() -> bool:
+ """
+ Determines if output format is .webm
+
+ Returns
+ -------
+ class:`bool`
+ ``True`` if format is set as webm
+
+ """
+ return config["format"] == "webm"
+
+
+def is_mov_format() -> bool:
+ """
+ Determines if output format is .mov
+
+ Returns
+ -------
+ class:`bool`
+ ``True`` if format is set as mov
+
+ """
+ return config["format"] == "mov"
+
+
def is_png_format() -> bool:
"""
Determines if output format is .png
@@ -76,7 +104,13 @@ def write_to_movie() -> bool:
"""
if is_png_format():
return False
- return config["write_to_movie"] or is_mp4_format() or is_gif_format()
+ return (
+ config["write_to_movie"]
+ or is_mp4_format()
+ or is_gif_format()
+ or is_webm_format()
+ or is_mov_format()
+ )
def add_extension_if_not_present(file_name, extension):
diff --git a/manim/utils/ipython_magic.py b/manim/utils/ipython_magic.py
index add4d5435..e0862f0e5 100644
--- a/manim/utils/ipython_magic.py
+++ b/manim/utils/ipython_magic.py
@@ -102,7 +102,7 @@ else:
if not len(args) or "-h" in args or "--help" in args or "--version" in args:
main(args, standalone_mode=False, prog_name="manim")
return
- modified_args = ["--jupyter"] + args[:-1] + [""] + [args[-1]]
+ modified_args = self.add_additional_args(args)
args = main(modified_args, standalone_mode=False, prog_name="manim")
with tempconfig(local_ns.get("config", {})):
config.digest_args(args)
@@ -136,6 +136,13 @@ else:
)
)
+ def add_additional_args(self, args):
+ additional_args = ["--jupyter"]
+ # Use webm to support transparency
+ if "-t" in args and "--format" not in args:
+ additional_args += ["--format", "webm"]
+ return additional_args + args[:-1] + [""] + [args[-1]]
+
def _generate_file_name():
return config["scene_names"][0] + "@" + datetime.now().strftime("%Y-%m-%d@%H-%M-%S")
diff --git a/tests/test_scene_rendering/test_cli_flags.py b/tests/test_scene_rendering/test_cli_flags.py
index 3f7c78718..5ef39d0ba 100644
--- a/tests/test_scene_rendering/test_cli_flags.py
+++ b/tests/test_scene_rendering/test_cli_flags.py
@@ -363,3 +363,106 @@ def test_images_are_created_when_png_format_set(
expected_png_path = tmp_path / "images" / "simple_scenes" / "SquareToCircle0.png"
assert expected_png_path.exists(), "png file not found at " + str(expected_png_path)
+
+
+def test_webm_format_output(tmp_path, manim_cfg_file, simple_scenes_path):
+ """Test only webm created when --format webm is set"""
+ scene_name = "SquareToCircle"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ "--format",
+ "webm",
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ unexpected_mp4_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mp4"
+ )
+ assert not unexpected_mp4_path.exists(), "unexpected mp4 file found at " + str(
+ unexpected_mp4_path
+ )
+
+ expected_webm_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm"
+ )
+ assert expected_webm_path.exists(), "expected webm file not found at " + str(
+ expected_webm_path
+ )
+
+
+def test_default_format_output_for_transparent_flag(
+ tmp_path, manim_cfg_file, simple_scenes_path
+):
+ """Test .mov is created by default when transparent flag is set"""
+ scene_name = "SquareToCircle"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ "-t",
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ unexpected_webm_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm"
+ )
+ assert not unexpected_webm_path.exists(), "unexpected webm file found at " + str(
+ unexpected_webm_path
+ )
+
+ expected_mov_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mov"
+ )
+ assert expected_mov_path.exists(), "expected .mov file not found at " + str(
+ expected_mov_path
+ )
+
+
+def test_mov_can_be_set_as_output_format(tmp_path, manim_cfg_file, simple_scenes_path):
+ """Test .mov is created by when set using --format mov arg"""
+ scene_name = "SquareToCircle"
+ command = [
+ sys.executable,
+ "-m",
+ "manim",
+ "-ql",
+ "--media_dir",
+ str(tmp_path),
+ "--format",
+ "mov",
+ simple_scenes_path,
+ scene_name,
+ ]
+ out, err, exit_code = capture(command)
+ assert exit_code == 0, err
+
+ unexpected_webm_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.webm"
+ )
+ assert not unexpected_webm_path.exists(), "unexpected webm file found at " + str(
+ unexpected_webm_path
+ )
+
+ expected_mov_path = (
+ tmp_path / "videos" / "simple_scenes" / "480p15" / "SquareToCircle.mov"
+ )
+ assert expected_mov_path.exists(), "expected .mov file not found at " + str(
+ expected_mov_path
+ )