Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Fixed rendering webm videos with sound when PyAV has no libvorbis (#5025)

Syed Muhammad Nafay Hassan Rizvicbaf84a5a01d
diff --git a/manim/scene/scene_file_writer.py b/manim/scene/scene_file_writer.py
index fb55c9620..6f08ca291 100644
--- a/manim/scene/scene_file_writer.py
+++ b/manim/scene/scene_file_writer.py
@@ -64,6 +64,20 @@ def convert_audio(
output_audio.mux(packet)
+def _webm_audio_codec() -> str:
+ """Return the audio codec used for sound in a VP9 (webm) video.
+
+ ``libvorbis`` is preferred, but PyAV wheels are not always built with it
+ (for example some Windows builds only ship the experimental native
+ ``vorbis`` encoder). Fall back to ``libopus``, which webm also supports.
+ """
+ try:
+ av.codec.Codec("libvorbis", "w")
+ except ValueError:
+ return "libopus"
+ return "libvorbis"
+
+
class _PartialMovieEncodeJob:
"""Run one segment encoder on a dedicated worker thread."""
@@ -892,7 +906,7 @@ class SceneFileWriter:
# manually.
if self.output_spec.segment_extension == ".webm":
ogg_sound_file_path = sound_file_path.with_suffix(".ogg")
- convert_audio(sound_file_path, ogg_sound_file_path, "libvorbis")
+ convert_audio(sound_file_path, ogg_sound_file_path, _webm_audio_codec())
sound_file_path = ogg_sound_file_path
elif self.output_spec.segment_extension == ".mp4":
# Similarly, pyav may reject wav audio in an .mp4 file;
diff --git a/tests/test_scene_rendering/test_file_writer.py b/tests/test_scene_rendering/test_file_writer.py
index 413f668a1..e82bce868 100644
--- a/tests/test_scene_rendering/test_file_writer.py
+++ b/tests/test_scene_rendering/test_file_writer.py
@@ -175,3 +175,30 @@ def test_unicode_partial_movie(config, tmpdir, simple_scenes_path):
_, err, exit_code = capture(command)
assert exit_code == 0, err
+
+
+ ("vorbis_modes", "expected"),
+ [({"r", "w"}, "libvorbis"), ({"r"}, "libopus"), (set(), "libopus")],
+ ids=["encoder-available", "decoder-only", "missing"],
+)
+def test_webm_audio_codec_checks_encoder_availability(
+ monkeypatch, vorbis_modes, expected
+):
+ from manim.scene import scene_file_writer
+
+ available = {"libopus", "vorbis", "aac"}
+ if vorbis_modes:
+ available.add("libvorbis")
+ monkeypatch.setattr(av, "codecs_available", available)
+ calls = []
+
+ def codec(name, mode):
+ calls.append((name, mode))
+ if mode not in vorbis_modes:
+ raise ValueError("libvorbis encoder is unavailable")
+ return object()
+
+ monkeypatch.setattr(av.codec, "Codec", codec)
+ assert scene_file_writer._webm_audio_codec() == expected
+ assert calls == [("libvorbis", "w")]