Skip to content

Commit 9d2c0a7

Browse files
committed
Render to mp4
1 parent e6d7e76 commit 9d2c0a7

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

manim/renderer/opengl_renderer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def __init__(self):
186186
self.num_plays = 0
187187
self.skip_animations = False
188188

189-
self.window = Window(size=(1280, 720))
189+
self.window = Window()
190190

191191
self.camera = OpenGLCamera()
192192

@@ -207,6 +207,8 @@ def __init__(self):
207207
# Initialize texture map.
208208
self.path_to_texture_id = {}
209209

210+
self.partial_movie_files = []
211+
210212
def update_depth_test(self, context, shader_wrapper):
211213
if shader_wrapper.depth_test:
212214
self.context.enable(moderngl.DEPTH_TEST)
@@ -354,6 +356,7 @@ def play(self, scene, *args, **kwargs):
354356
self.animation_elapsed_time = 0
355357

356358
temp_name = f"media/temp_{self.num_plays}.mp4"
359+
self.partial_movie_files.append(temp_name)
357360
self.file_writer.begin_animation(
358361
not self.skip_animations, file_path=temp_name
359362
)
@@ -379,7 +382,7 @@ def update_frame():
379382
update_frame()
380383

381384
def scene_finished(self, scene):
382-
pass
385+
self.file_writer.finish(self.partial_movie_files)
383386

384387
def save_static_frame_data(self, scene, static_mobjects):
385388
pass

manim/renderer/opengl_renderer_window.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class Window(PygletWindow):
1111
vsync = True
1212
cursor = True
1313

14-
def __init__(self, size=(config["pixel_width"], config["pixel_height"]), **kwargs):
15-
super().__init__()
14+
def __init__(self, size=None, **kwargs):
15+
if size is None:
16+
size = (config["pixel_width"], config["pixel_height"])
17+
super().__init__(size=size)
1618

1719
self.pressed_keys = set()
1820

@@ -23,3 +25,5 @@ def __init__(self, size=(config["pixel_width"], config["pixel_height"]), **kwarg
2325
self.timer = Timer()
2426
self.config = mglw.WindowConfig(ctx=self.ctx, wnd=self, timer=self.timer)
2527
self.timer.start()
28+
29+
self.swap_buffers()

manim/scene/scene.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def get_time_progression(
741741
if self.renderer.skip_animations and not override_skip_animations:
742742
times = [run_time]
743743
else:
744-
step = 1 / self.renderer.camera.frame_rate
744+
step = 1 / config["frame_rate"]
745745
times = np.arange(0, run_time, step)
746746
time_progression = tqdm(
747747
times,

manim/scene/scene_file_writer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def idle_stream(self):
323323
if time_diff < frame_duration:
324324
sleep(frame_duration - time_diff)
325325

326-
def finish(self):
326+
def finish(self, partial_movie_files=None):
327327
"""
328328
Finishes writing to the FFMPEG buffer.
329329
Combines the partial movie files into the
@@ -334,7 +334,7 @@ def finish(self):
334334
if config["write_to_movie"]:
335335
if hasattr(self, "writing_process"):
336336
self.writing_process.terminate()
337-
self.combine_movie_files()
337+
self.combine_movie_files(partial_movie_files=partial_movie_files)
338338
if config["flush_cache"]:
339339
self.flush_cache_directory()
340340
else:
@@ -376,6 +376,8 @@ def open_movie_pipe(self, file_path=None):
376376
"-metadata",
377377
f"comment=Rendered with Manim Community v{__version__}",
378378
]
379+
if config["use_opengl_renderer"]:
380+
command += ["-vf", "vflip"]
379381
if config["transparent"]:
380382
command += ["-vcodec", "qtrle"]
381383
else:
@@ -416,7 +418,7 @@ def is_already_cached(self, hash_invocation):
416418
)
417419
return os.path.exists(path)
418420

419-
def combine_movie_files(self):
421+
def combine_movie_files(self, partial_movie_files=None):
420422
"""
421423
Used internally by Manim to combine the separate
422424
partial movie files that make up a Scene into a single
@@ -428,7 +430,10 @@ def combine_movie_files(self):
428430
# which effectively has cuts at all the places you might want. But for
429431
# viewing the scene as a whole, one of course wants to see it as a
430432
# single piece.
431-
partial_movie_files = [el for el in self.partial_movie_files if el is not None]
433+
if not config["use_opengl_renderer"]:
434+
partial_movie_files = [
435+
el for el in self.partial_movie_files if el is not None
436+
]
432437
# NOTE : Here we should do a check and raise an exception if partial
433438
# movie file is empty. We can't, as a lot of stuff (in particular, in
434439
# tests) use scene initialization, and this error would be raised as

0 commit comments

Comments
 (0)