SceneFileWriter

Qualified name: manim.scene.scene\_file\_writer.SceneFileWriter

class SceneFileWriter(renderer, scene_name, **kwargs)[source]

Bases: object

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.

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.

Methods

add_audio_segment

This method adds an audio segment from an AudioSegment type object and suitable parameters.

add_partial_movie_file

Adds a new partial movie file path to scene.partial_movie_files from an hash.

add_sound

This method adds an audio segment from a sound file.

begin_animation

Used internally by manim to stream the animation to FFMPEG for displaying or writing to a file.

clean_cache

Will clean the cache by removing the oldest partial_movie_files.

close_movie_pipe

Used internally by Manim to gracefully stop writing to FFMPEG’s input buffer

combine_movie_files

Used internally by Manim to combine the separate partial movie files that make up a Scene into a single video file for that Scene.

create_audio_segment

Creates an empty, silent, Audio Segment.

end_animation

Internally used by Manim to stop streaming to FFMPEG gracefully.

finish

Finishes writing to the FFMPEG buffer or writing images to output directory.

flush_cache_directory

Delete all the cached partial movie files

get_resolution_directory

Get the name of the resolution directory directly containing the video file.

idle_stream

Doesn’t write anything to the FFMPEG frame buffer.

init_audio

Preps the writer for adding audio to the movie.

init_output_directories

Initialise output directories.

is_already_cached

Will check if a file named with hash_invocation exists.

open_movie_pipe

Used internally by Manim to initialise FFMPEG and begin writing to FFMPEG’s input buffer.

output_image

output_image_from_array

print_file_ready_message

Prints the “File Ready” message to STDOUT.

save_final_image

The name is a misnomer.

write_frame

Used internally by Manim to write a frame to the FFMPEG input buffer.

write_opengl_frame

Attributes

force_output_as_scene_name

add_audio_segment(new_segment, time=None, gain_to_background=None)[source]

This method adds an audio segment from an AudioSegment type object and suitable parameters.

Parameters
  • new_segment (AudioSegment) – The audio segment to add

  • time (int, float, optional) – the timestamp at which the sound should be added.

  • gain_to_background (optional) – The gain of the segment from the background.

add_partial_movie_file(hash_animation)[source]

Adds a new partial movie file path to scene.partial_movie_files from an hash. This method will compute the path from the hash.

Parameters

hash_animation (str) – Hash of the animation.

add_sound(sound_file, time=None, gain=None, **kwargs)[source]

This method adds an audio segment from a sound file.

Parameters
  • sound_file (str) – The path to the sound file.

  • time (float or int, optional) – The timestamp at which the audio should be added.

  • gain (optional) – The gain of the given audio segment.

  • **kwargs – This method uses add_audio_segment, so any keyword arguments used there can be referenced here.

begin_animation(allow_write=False, file_path=None)[source]

Used internally by manim to stream the animation to FFMPEG for displaying or writing to a file.

Parameters

allow_write (bool, optional) – Whether or not to write to a video file.

clean_cache()[source]

Will clean the cache by removing the oldest partial_movie_files.

close_movie_pipe()[source]

Used internally by Manim to gracefully stop writing to FFMPEG’s input buffer

combine_movie_files(partial_movie_files=None)[source]

Used internally by Manim to combine the separate partial movie files that make up a Scene into a single video file for that Scene.

create_audio_segment()[source]

Creates an empty, silent, Audio Segment.

end_animation(allow_write=False)[source]

Internally used by Manim to stop streaming to FFMPEG gracefully.

Parameters

allow_write (bool, optional) – Whether or not to write to a video file.

finish(partial_movie_files=None)[source]

Finishes writing to the FFMPEG buffer or writing images to output directory. Combines the partial movie files into the whole scene. If save_last_frame is True, saves the last frame in the default image directory.

flush_cache_directory()[source]

Delete all the cached partial movie files

get_resolution_directory()[source]

Get the name of the resolution directory directly containing the video file.

This method gets the name of the directory that immediately contains the video file. This name is <height_in_pixels_of_video>p<frame_rate>. For example, if you are rendering an 854x480 px animation at 15fps, the name of the directory that immediately contains the video, file will be 480p15.

The file structure should look something like:

MEDIA_DIR
    |--Tex
    |--texts
    |--videos
    |--<name_of_file_containing_scene>
        |--<height_in_pixels_of_video>p<frame_rate>
            |--<scene_name>.mp4
Returns

The name of the directory.

Return type

str

idle_stream()[source]

Doesn’t write anything to the FFMPEG frame buffer.

init_audio()[source]

Preps the writer for adding audio to the movie.

init_output_directories(scene_name)[source]

Initialise output directories.

Notes

The directories are read from config, for example config['media_dir']. If the target directories don’t already exist, they will be created.

is_already_cached(hash_invocation)[source]

Will check if a file named with hash_invocation exists.

Parameters

hash_invocation (str) – The hash corresponding to an invocation to either scene.play or scene.wait.

Returns

Whether the file exists.

Return type

bool

open_movie_pipe(file_path=None)[source]

Used internally by Manim to initialise FFMPEG and begin writing to FFMPEG’s input buffer.

print_file_ready_message(file_path)[source]

Prints the “File Ready” message to STDOUT.

save_final_image(image)[source]

The name is a misnomer. This method saves the image passed to it as an in the default image directory.

Parameters

image (np.array) – The pixel array of the image to save.

write_frame(frame_or_renderer)[source]

Used internally by Manim to write a frame to the FFMPEG input buffer.

Parameters

frame (np.array) – Pixel array of the frame.