Skip to content

Made FFmpeg executable path configurable #2667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions manim/_config/default.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
11 changes: 11 additions & 0 deletions manim/_config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class MyScene(Scene):
"dry_run",
"enable_wireframe",
"ffmpeg_loglevel",
"ffmpeg_executable",
"format",
"flush_cache",
"frame_height",
Expand Down Expand Up @@ -633,6 +634,10 @@ def digest_parser(self, parser: configparser.ConfigParser) -> ManimConfig:
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:
Expand Down Expand Up @@ -967,6 +972,12 @@ def format(self, val: str) -> None:
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),
Expand Down
4 changes: 0 additions & 4 deletions manim/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
"PI",
"TAU",
"DEGREES",
"FFMPEG_BIN",
"GIF_FILE_EXTENSION",
"FFMPEG_VERBOSITY_MAP",
"VERBOSITY_CHOICES",
Expand Down Expand Up @@ -201,9 +200,6 @@
DEGREES: float = TAU / 360
"""The exchange rate between radians and degrees."""

# ffmpeg stuff
FFMPEG_BIN: str = "ffmpeg"

# gif stuff
GIF_FILE_EXTENSION: str = ".gif"

Expand Down
18 changes: 14 additions & 4 deletions manim/scene/scene_file_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import shutil
import subprocess
import sys
from pathlib import Path
from typing import Any

Expand All @@ -20,10 +21,11 @@

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,
Expand Down Expand Up @@ -79,6 +81,14 @@ def __init__(self, renderer, scene_name, **kwargs):
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.
Expand Down Expand Up @@ -464,7 +474,7 @@ def open_movie_pipe(self, file_path=None):
width = config["pixel_width"]

command = [
FFMPEG_BIN,
config.ffmpeg_executable,
"-y", # overwrite output file if it exists
"-f",
"rawvideo",
Expand Down Expand Up @@ -547,7 +557,7 @@ def combine_files(
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",
Expand Down Expand Up @@ -614,7 +624,7 @@ def combine_to_movie(self):
)
temp_file_path = movie_file_path.replace(extension, f"_temp{extension}")
commands = [
FFMPEG_BIN,
config.ffmpeg_executable,
"-i",
movie_file_path,
"-i",
Expand Down
11 changes: 11 additions & 0 deletions manim/utils/file_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"is_webm_format",
"is_mov_format",
"write_to_movie",
"ensure_executable",
]

import os
Expand Down Expand Up @@ -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)
Expand Down