Skip to content

06 Recording

Tagin T edited this page Nov 24, 2026 · 2 revisions

English | 中文

06 Recording & Parallelism

Three recording paths

Entry point Window Frame source Use case
fast_record_scene hidden per-frame GPU readback → ffmpeg pipe fastest, batch renders
record_scene visible render thread samples the presented frame watching / interactive
FrameServer + save_frame hidden render_at(t) on demand + PNG stills, thumbnails, storyboards

All three share sync → paint → tick, so the output is identical.

The fast_record_scene flow

fast_record_scene(SceneCls, out.mp4)
  ├─ scene_lifecycle(scene)        # setup/construct/tear_down
  │    └─ construct() creates the MLWindow
  ├─ _AutoRecord wraps close()
  ├─ enable_fast_record(out, fps, hidden)
  ├─ _run_timeline(...)            # per frame evaluate + sync + tick
  │    └─ readback each frame → ffmpeg stdin
  └─ close() → _finish_fast_record() → ffmpeg finalises

fast_record_scene takes the frame count from Timeline.frame_times(fps), not round(duration*fps), so play boundaries match single-frame playback.

count_only=True builds and times the scene without writing a file; segment=(start, end) exports a BMP directory instead of an MP4.

The ffmpeg pipe

  • recording opens an ffmpeg subprocess and writes pixels from the staging buffer straight to stdin;
  • the default format is BGRA (Windows) or BGR24 (the older native fallback);
  • encoder arguments live in window/recording.py::fast_pipe_ffmpeg_args;
  • ffmpeg must be on PATH, otherwise you get an explicit error.

_finish_fast_record closes stdin and waits for the encoder to exit; close() ordering guarantees encoding finishes before Vulkan is torn down.

FrameServer

fs = FrameServer(MyScene)       # one construct + build_timeline
fs.render_frame(t)              # O(1)
fs.grab(t)                      # render + read back
fs.save_frame(t, "f.png")
fs.save_frame_async(t, "f.png") # PNG encode/write on a thread pool
fs.flush()
fs.close()
  • the window is hidden by default;
  • render_at renders a single frame and does not depend on the previous one;
  • save_frame_async uses a bounded queue plus a thread pool, so the main thread only does GPU readback.

jobs=N parallelism

Planning

_parallel_plan(scene, jobs)
  ├─ _scene_token()              # only importable Scene classes qualify
  ├─ FrameServer(scene) probe    # one construct, read n_frames
  ├─ check only one window exists
  └─ return {jobs, scene_ref, n_frames}

jobs only applies to hidden windows without count_only or segment; instances, lambdas and classes defined inside functions fall back to a single process.

Chunking

_record_worker.frame_block(n_frames, jobs, index) cuts the frame range into contiguous blocks: the first n_frames % jobs workers get one extra frame, so concatenation is exactly the full sequence.

Each worker:

  1. imports the scene in its own process;
  2. creates its own hidden MLWindow;
  3. runs FrameServer(scene) to construct() again;
  4. renders only its own frame range;
  5. writes seg_XXX.mp4;
  6. the parent joins the segments losslessly with ffmpeg concat.

Shared prepare

Each worker still has to rebuild mobjects (they cannot be pickled), but expensive pure data can be computed once:

class Film(Scene):
    @classmethod
    def prepare(cls):
        return {"verts": build_terrain()}   # picklable

    def construct(self):
        data = prepared()                   # computed once in the parent, loaded here
        grid = shared_prepare("grid-v2", build_grid)
  • the parent runs prepare() once and writes a pickle;
  • workers load it through prepared();
  • shared_prepare(key, builder, version=...) is a general on-disk cache with a cross-process lock;
  • RTM_PREPARE_DIR can point at a persistent cache.

Audio and subtitles

Workers write add_sound cues and play(subcaption=...) entries to .sounds.json / .subcaptions.json; the parent muxes audio and generates the SRT after concatenation.

Recording constraints

  • one MLWindow per process; parallelism means multiple processes, not multiple windows.
  • workers with jobs=N do not run each animation's finish() / clean_up_from_scene(); if your scene depends on those side effects, use jobs=None.
  • fast record is fixed at 60 fps and always 1920×1080.
  • cleanup=True removes only this run's temporary output, never the media/Tex / media/texts caches.

Key files

  • real_time_manim/record.py: fast_record_scene, record_scene, _parallel_plan, _fast_record_parallel
  • real_time_manim/_record_worker.py: per-worker frame blocks and encoding
  • real_time_manim/prepare.py: prepared, shared_prepare
  • real_time_manim/window/recording.py: ffmpeg pipe, _AutoRecord

Clone this wiki locally