Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Make manim crash when ffmpeg crashes (#546)

Leo Torres8b9dba2edb6c
diff --git a/docs/source/examples/3d.rst b/docs/source/examples/3d.rst
index 5ffddca16..e68ae17af 100644
--- a/docs/source/examples/3d.rst
+++ b/docs/source/examples/3d.rst
@@ -78,4 +78,3 @@
self.add(axes, curve1)
self.set_camera_orientation(phi=80 * DEGREES, theta=-60 * DEGREES)
self.wait()
-
diff --git a/docs/source/installation/linux.rst b/docs/source/installation/linux.rst
index d3b7c791f..532cb3937 100644
--- a/docs/source/installation/linux.rst
+++ b/docs/source/installation/linux.rst
@@ -11,7 +11,7 @@ The two necessary dependencies are cairo and ffmpeg. LaTeX is strongly
recommended, as it is necessary to have access to the ``Tex`` and ``MathTex`` classes.
Ubuntu/Mint/Debian
-*************
+******************
Before installing anything, make sure that your system is up to date.
@@ -42,15 +42,15 @@ To install LaTeX:
If you don't have python3-pip installed, install it:
.. code-block:: bash
-
+
sudo apt install python3-pip
-
+
.. note:: These instructions are also valid for other Debian-based
distributions or distributions that use the ``apt`` package manager.
Fedora/CentOS/RHEL
-*************
+******************
To install cairo:
@@ -116,7 +116,7 @@ To install LaTeX:
If you don't have python-pip installed, install it:
.. code-block:: bash
-
+
sudo pacman -S python-pip
diff --git a/docs/source/manim_directive.py b/docs/source/manim_directive.py
index 7e0fb3cdb..7f66a374f 100644
--- a/docs/source/manim_directive.py
+++ b/docs/source/manim_directive.py
@@ -168,6 +168,14 @@ class ManimDirective(Directive):
video_dir = os.path.join(media_dir, "videos")
output_file = f"{clsname}-{classnamedict[clsname]}"
+ # Important: note that all scenes are being rendered on the same python
+ # interpreter. That means that each time we change the config, that
+ # same config will be used for the next scene. For this reason, we
+ # have to make sure to restore the original config after each scene.
+ save_config_code = [
+ "original_config = copy.deepcopy(config)",
+ "original_fw_config = copy.deepcopy(file_writer_config)",
+ ]
file_writer_config_code = [
f'config["frame_rate"] = {frame_rate}',
f'config["pixel_height"] = {pixel_height}',
@@ -181,6 +189,14 @@ class ManimDirective(Directive):
f'file_writer_config["save_as_gif"] = {save_as_gif}',
f'file_writer_config["output_file"] = "{output_file}"',
]
+ file_writer_config_code.append(
+ 'file_writer_config["write_to_movie"] = '
+ + ("False" if save_last_frame else "True")
+ )
+ restore_config_code = [
+ "config = original_config",
+ "file_writer_config = original_fw_config",
+ ]
user_code = self.content
if user_code[0].startswith(">>> "): # check whether block comes from doctest
@@ -190,9 +206,12 @@ class ManimDirective(Directive):
code = [
"from manim import *",
+ f"logger.info('rendering {clsname}')",
+ *save_config_code,
*file_writer_config_code,
*user_code,
f"{clsname}().render()",
+ *restore_config_code,
]
exec("\n".join(code), globals())
diff --git a/manim/constants.py b/manim/constants.py
index 2f13f226e..c83b06821 100644
--- a/manim/constants.py
+++ b/manim/constants.py
@@ -1,6 +1,4 @@
-"""
-Constant definitions.
-"""
+"""Constant definitions."""
import numpy as np
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index e11647715..528791c19 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -544,8 +544,11 @@ class SceneFileWriter(object):
if not self.includes_sound:
commands.insert(-1, "-an")
- combine_process = subprocess.Popen(commands)
- combine_process.wait()
+ try:
+ subprocess.check_call(commands)
+ except subprocess.CalledProcessError as exc:
+ logger.error(f"FFMPEG returned with code {exc.returncode}")
+ raise exc
if self.includes_sound:
sound_file_path = movie_file_path.replace(
@@ -582,7 +585,12 @@ class SceneFileWriter(object):
# "-shortest",
temp_file_path,
]
- subprocess.call(commands)
+ try:
+ subprocess.check_call(commands)
+ except subprocess.CalledProcessError as exc:
+ logger.error(f"FFMPEG returned with code {exc.returncode}")
+ raise exc
+
shutil.move(temp_file_path, movie_file_path)
os.remove(sound_file_path)
diff --git a/manim/utils/caching.py b/manim/utils/caching.py
index 6b611b96c..9afed9433 100644
--- a/manim/utils/caching.py
+++ b/manim/utils/caching.py
@@ -36,16 +36,21 @@ def handle_caching_play(func):
hash_play = get_hash_from_play_call(
self, self.camera, animations, mobjects_on_scene
)
- if self.file_writer.is_already_cached(hash_play):
- logger.info(
- f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",
- {"hash_play": hash_play},
- )
- file_writer_config["skip_animations"] = True
+ if file_writer_config["write_to_movie"]:
+ if self.file_writer.is_already_cached(hash_play):
+ logger.info(
+ f"Animation {self.num_plays} : Using cached data (hash : %(hash_play)s)",
+ {"hash_play": hash_play},
+ )
+ file_writer_config["skip_animations"] = True
else:
hash_play = "uncached_{:05}".format(self.num_plays)
self.animations_hashes.append(hash_play)
- self.file_writer.add_partial_movie_file(hash_play)
+ if (
+ not file_writer_config["disable_caching"]
+ and file_writer_config["write_to_movie"]
+ ):
+ self.file_writer.add_partial_movie_file(hash_play)
logger.debug(
"List of the first few animation hashes of the scene: %(h)s",
{"h": str(self.animations_hashes[:5])},
@@ -84,15 +89,20 @@ def handle_caching_wait(func):
hash_wait = get_hash_from_wait_call(
self, self.camera, duration, stop_condition, scene.get_mobjects()
)
- if self.file_writer.is_already_cached(hash_wait):
- logger.info(
- f"Wait {self.num_plays} : Using cached data (hash : {hash_wait})"
- )
- file_writer_config["skip_animations"] = True
+ if file_writer_config["write_to_movie"]:
+ if self.file_writer.is_already_cached(hash_wait):
+ logger.info(
+ f"Wait {self.num_plays} : Using cached data (hash : {hash_wait})"
+ )
+ file_writer_config["skip_animations"] = True
else:
hash_wait = "uncached_{:05}".format(self.num_plays)
self.animations_hashes.append(hash_wait)
- self.file_writer.add_partial_movie_file(hash_wait)
+ if (
+ not file_writer_config["disable_caching"]
+ and file_writer_config["write_to_movie"]
+ ):
+ self.file_writer.add_partial_movie_file(hash_wait)
logger.debug(
"List of the first few animation hashes of the scene: %(h)s",
{"h": str(self.animations_hashes[:5])},