Repository: ManimCommunity/manim
File: manim/scene/scene_file_writer.py (Python)

Code before:

def convert_audio(input_path: Path, output_path: Path, codec_name: str):
    with (
        av.open(input_path) as input_audio,
        av.open(output_path, "w") as output_audio,
    ):
        input_audio_stream = input_audio.streams.audio[0]
        output_audio_stream = output_audio.add_stream(codec_name)
        for frame in input_audio.decode(input_audio_stream):
            for packet in output_audio_stream.encode(frame):
                output_audio.mux(packet)

        for packet in output_audio_stream.encode():
            output_audio.mux(packet)


def get_out_format(path: Path | str, check_config=False) -> str:
    if isinstance(path, str):
        path = Path(path)

    suffix = path.suffix.lower()
    if not suffix.startswith("."):
        raise ValueError(f"{path}: no suffix")

    out_format = suffix[1:]
    if check_config and config.format is not None and config.format != out_format:
        raise Exception(
            f"config.format ({config.format}) and suffix of output ({suffix}) do not match"
        )

    return out_format


class SceneFileWriter:
    """
    SceneFileWriter is the object that actually writes the animations
    played, into video files, using FFMPEG.
    This is mostly for Manim's internal use. You will rarely, if ever,
    have to use the methods for this class, unless tinkering with the very
    fabric of Manim's reality.

    Attributes
    ----------
        sections : list of :class:`.Section`
            used to segment scene

        sections_output_dir : :class:`pathlib.Path`
            where are section videos stored

        output_name : str
            name of movie without extension and basis for section video names

Review comment (suggestion):
I think the best way to get the out format is to just use `config.movie_file_extension.removeprefix(".")` - and then you can also avoid the checks.

Code after:


def convert_audio(input_path: Path, output_path: Path, codec_name: str):
    with (
        av.open(input_path) as input_audio,
        av.open(output_path, "w") as output_audio,
    ):
        input_audio_stream = input_audio.streams.audio[0]
        output_audio_stream = output_audio.add_stream(codec_name)
        for frame in input_audio.decode(input_audio_stream):
            for packet in output_audio_stream.encode(frame):
                output_audio.mux(packet)

        for packet in output_audio_stream.encode():
            output_audio.mux(packet)


class SceneFileWriter:
    """
    SceneFileWriter is the object that actually writes the animations
    played, into video files, using FFMPEG.
    This is mostly for Manim's internal use. You will rarely, if ever,
    have to use the methods for this class, unless tinkering with the very
    fabric of Manim's reality.

    Attributes
    ----------
        sections : list of :class:`.Section`
            used to segment scene

        sections_output_dir : :class:`pathlib.Path`
            where are section videos stored

        output_name : str
            name of movie without extension and basis for section video names

    Some useful attributes are:
        "write_to_movie" (bool=False)
            Whether or not to write the animations into a video file.
        "movie_file_extension" (str=".mp4")
            The file-type extension of the outputted video.
        "partial_movie_files"
            List of all the partial-movie files.

    """

    force_output_as_scene_name = False

    def __init__(self, renderer, scene_name, **kwargs):
        self.renderer = renderer
        self.init_output_directories(scene_name)