Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Made FFmpeg executable path configureable (#2667)

ad_chaosaad8c44ea965
diff --git a/manim/_config/default.cfg b/manim/_config/default.cfg
index ee382742e..651951cf6 100644
--- a/manim/_config/default.cfg
+++ b/manim/_config/default.cfg
@@ -224,6 +224,8 @@ repr_number = green
# Uncomment the following line to manually set the loglevel for ffmpeg. See
# ffmpeg manpage for accepted values
loglevel = ERROR
+# defaults to the one present in path
+ffmpeg_executable = ffmpeg
[jupyter]
media_embed =
diff --git a/manim/_config/utils.py b/manim/_config/utils.py
index 98bc88609..9b85e5723 100644
--- a/manim/_config/utils.py
+++ b/manim/_config/utils.py
@@ -252,6 +252,7 @@ class ManimConfig(MutableMapping):
"dry_run",
"enable_wireframe",
"ffmpeg_loglevel",
+ "ffmpeg_executable",
"format",
"flush_cache",
"frame_height",
@@ -633,6 +634,10 @@ class ManimConfig(MutableMapping):
if val:
self.ffmpeg_loglevel = val
+ # TODO: Fix the mess above and below
+ val = parser["ffmpeg"].get("ffmpeg_executable")
+ setattr(self, "ffmpeg_executable", val)
+
try:
val = parser["jupyter"].getboolean("media_embed")
except ValueError:
@@ -967,6 +972,12 @@ class ManimConfig(MutableMapping):
doc="Verbosity level of ffmpeg (no flag).",
)
+ ffmpeg_executable = property(
+ lambda self: self._d["ffmpeg_executable"],
+ lambda self, val: self._set_str("ffmpeg_executable", val),
+ doc="Manually specify the path to the ffmpeg executable",
+ )
+
media_embed = property(
lambda self: self._d["media_embed"],
lambda self, val: self._d.__setitem__("media_embed", val),
diff --git a/manim/constants.py b/manim/constants.py
index a32bf416b..2eb8b9147 100644
--- a/manim/constants.py
+++ b/manim/constants.py
@@ -64,7 +64,6 @@ __all__ = [
"PI",
"TAU",
"DEGREES",
- "FFMPEG_BIN",
"GIF_FILE_EXTENSION",
"FFMPEG_VERBOSITY_MAP",
"VERBOSITY_CHOICES",
@@ -201,9 +200,6 @@ TAU: float = 2 * PI
DEGREES: float = TAU / 360
"""The exchange rate between radians and degrees."""
-# ffmpeg stuff
-FFMPEG_BIN: str = "ffmpeg"
-
# gif stuff
GIF_FILE_EXTENSION: str = ".gif"
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index 2d66d62cf..25763268d 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -8,6 +8,7 @@ import json
import os
import shutil
import subprocess
+import sys
from pathlib import Path
from typing import Any
@@ -20,10 +21,11 @@ from manim import __version__
from .. import config, logger
from .._config.logger_utils import set_file_logger
-from ..constants import FFMPEG_BIN, GIF_FILE_EXTENSION
+from ..constants import GIF_FILE_EXTENSION
from ..utils.file_ops import (
add_extension_if_not_present,
add_version_before_extension,
+ ensure_executable,
guarantee_existence,
is_gif_format,
is_png_format,
@@ -79,6 +81,14 @@ class SceneFileWriter:
self.next_section(
name="autocreated", type=DefaultSectionType.NORMAL, skip_animations=False
)
+ # fail fast if ffmpeg is not found
+ if not ensure_executable(Path(config.ffmpeg_executable)):
+ raise RuntimeError(
+ "Manim could not find ffmpeg, which is required for generating video output.\n"
+ "For installing ffmpeg please consult https://docs.manim.community/en/stable/installation.html\n"
+ "Make sure to either add ffmpeg to the PATH environment variable\n"
+ "or set path to the ffmpeg executable under the ffmpeg header in Manim's configuration."
+ )
def init_output_directories(self, scene_name):
"""Initialise output directories.
@@ -464,7 +474,7 @@ class SceneFileWriter:
width = config["pixel_width"]
command = [
- FFMPEG_BIN,
+ config.ffmpeg_executable,
"-y", # overwrite output file if it exists
"-f",
"rawvideo",
@@ -547,7 +557,7 @@ class SceneFileWriter:
pf_path = pf_path.replace("\\", "/")
fp.write(f"file 'file:{pf_path}'\n")
commands = [
- FFMPEG_BIN,
+ config.ffmpeg_executable,
"-y", # overwrite output file if it exists
"-f",
"concat",
@@ -614,7 +624,7 @@ class SceneFileWriter:
)
temp_file_path = movie_file_path.replace(extension, f"_temp{extension}")
commands = [
- FFMPEG_BIN,
+ config.ffmpeg_executable,
"-i",
movie_file_path,
"-i",
diff --git a/manim/utils/file_ops.py b/manim/utils/file_ops.py
index e7b78d71a..1f41837d6 100644
--- a/manim/utils/file_ops.py
+++ b/manim/utils/file_ops.py
@@ -15,6 +15,7 @@ __all__ = [
"is_webm_format",
"is_mov_format",
"write_to_movie",
+ "ensure_executable",
]
import os
@@ -121,6 +122,16 @@ def write_to_movie() -> bool:
)
+def ensure_executable(path_to_exe: Path) -> bool:
+ if path_to_exe.parent == Path("."):
+ executable = shutil.which(path_to_exe.stem)
+ if executable is None:
+ return False
+ else:
+ executable = path_to_exe
+ return os.access(executable, os.X_OK)
+
+
def add_extension_if_not_present(file_name: Path, extension: str) -> Path:
if file_name.suffix != extension:
return file_name.with_suffix(extension)