INNER CODE UNIT · Python
generate
unitedrhino/things · scripts/generate-energy-demo-svg.py:119
def generate(demo: Demo, output_dir: Path) -> None:
"""generate 校验输入后生成一份完整时长的 SVG 动图。"""
if not demo.source.is_file():
raise FileNotFoundError(demo.source)
duration, source_width, source_height = probe_video(demo.source)
if abs(duration - demo.duration) > 0.1:
raise RuntimeError(f"{demo.source.name} 时长应为 {demo.duration}s,实际为 {duration:.3f}s")
output_height = round(FRAME_WIDTH * source_height / source_width)
output_height += output_height % 2
with tempfile.TemporaryDirectory(prefix="energy-demo-svg-") as temporary:
frames = extract_frames(demo, Path(temporary))
output = output_dir / demo.output_name
build_svg(demo, frames, FRAME_WIDTH, output_height, output)
print(f"生成 {output}({output.stat().st_size / 1024 / 1024:.2f} MB)")
def main() -> None: