调版式的时候,一帧截图比一段视频有用得多。-s(--save_last_frame)让 Manim 跳过视频编码,只把最后一帧写成 PNG,文件名和目录由场景自动决定。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from manim import *
class SaveLastFrameDemo(Scene):
"""-s / --save_last_frame:跳过视频编码,只导出最后一帧"""
def construct(self):
title = Text("-s 只导出最后一帧,不编码视频", font_size=32).to_edge(UP, buff=0.4)
self.play(Write(title))
# 左:不加 -s,输出 mp4(多帧)
left = VGroup(*[Rectangle(width=1.15, height=0.78, stroke_color=BLUE,
stroke_width=2, fill_color=BLUE, fill_opacity=0.15)
for _ in range(3)]).arrange(RIGHT, buff=0.12)
left_label = Text("默认:逐帧 → mp4", font_size=21, color=BLUE_B)
left_tag = Text("480p15/xxx.mp4", font="Consolas", font_size=17, color=GREY_B)
g_left = VGroup(left, left_label, left_tag).arrange(DOWN, buff=0.22)
plus = Text("-s", font="Consolas", font_size=30, color=YELLOW)
# 右:加 -s,只输出 png(单帧)
right = Rectangle(width=1.15, height=0.78, stroke_color=GREEN,
stroke_width=2, fill_color=GREEN, fill_opacity=0.35)
right_label = Text("-s:只存最后一帧 → png", font_size=21, color=GREEN_B)
right_tag = Text("images/xxx_ManimCE_v0.20.1.png", font="Consolas",
font_size=17, color=GREY_B)
g_right = VGroup(right, right_label, right_tag).arrange(DOWN, buff=0.22)
row = VGroup(g_left, plus, g_right).arrange(RIGHT, buff=0.85)
row.next_to(title, DOWN, buff=0.85)
self.play(FadeIn(g_left, shift=UP * 0.2))
self.play(FadeIn(plus, scale=0.6))
self.play(FadeIn(g_right, shift=UP * 0.2))
self.wait(0.4)
# 演示:本体动画
shapes = VGroup(Circle(radius=0.55, color=BLUE), Square(side_length=0.95, color=YELLOW),
Triangle(color=RED).scale(0.75))
shapes.arrange(RIGHT, buff=0.45)
stage = VGroup(shapes).next_to(row, DOWN, buff=0.72)
self.play(LaggedStart(*[Create(s, run_time=1.1) for s in shapes], lag_ratio=0.35))
self.play(shapes.animate.set_fill(opacity=0.5).scale(1.12))
flash = Flash(shapes.get_center(), color=WHITE, line_length=0.35)
stamp = Text("导出这一帧", font_size=22, color=WHITE)
stamp.next_to(stage, DOWN, buff=0.3)
self.play(flash, FadeIn(stamp, shift=UP * 0.15))
self.wait(1.4)

演示视频

下载 Python 脚本

要点

  • 产物是 media/images/{脚本名}/{场景名}_ManimCE_v0.20.1.png,版本号直接写在文件名里
  • 前面的动画依然会执行一遍,所以耗时不等于零,省掉的是编码与合成环节
  • 调构图用 -ql -s、要高清图再换 -qh -s,比每次都渲一遍视频快得多

代码来源:Manim Community 官方文档 Tutorials / Manim’s Output Settings(MIT),示例代码按 Manim CE 0.20.1 编写并实跑验证。