Repository navigation
06 Recording
English | 中文
| 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.
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.
- 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; -
ffmpegmust be onPATH, 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.
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_atrenders a single frame and does not depend on the previous one; -
save_frame_asyncuses a bounded queue plus a thread pool, so the main thread only does GPU readback.
_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.
_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:
- imports the scene in its own process;
- creates its own hidden
MLWindow; - runs
FrameServer(scene)toconstruct()again; - renders only its own frame range;
- writes
seg_XXX.mp4; - the parent joins the segments losslessly with ffmpeg concat.
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_DIRcan point at a persistent cache.
Workers write add_sound cues and play(subcaption=...) entries to .sounds.json / .subcaptions.json; the parent muxes audio and generates the SRT after concatenation.
- one
MLWindowper process; parallelism means multiple processes, not multiple windows. - workers with
jobs=Ndo not run each animation'sfinish()/clean_up_from_scene(); if your scene depends on those side effects, usejobs=None. - fast record is fixed at 60 fps and always 1920×1080.
-
cleanup=Trueremoves only this run's temporary output, never themedia/Tex/media/textscaches.
-
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