|
| 1 | +"""Motion Graphics — Basic HTML/GSAP → MP4 render (no LLM required). |
| 2 | +
|
| 3 | +This example drives the `HtmlRenderBackend` directly with a hand-authored |
| 4 | +HTML/GSAP composition. It renders to a real MP4 via headless Chromium + |
| 5 | +bundled ffmpeg, with no API keys needed. |
| 6 | +
|
| 7 | +Requirements: |
| 8 | + pip install praisonai-tools[video-motion] |
| 9 | + playwright install chromium |
| 10 | +
|
| 11 | +Verified end-to-end: produces 1920x1080 @ 30fps H.264 MP4 in ~5–10 seconds. |
| 12 | +""" |
| 13 | + |
| 14 | +import asyncio |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from praisonai_tools.video.motion_graphics import HtmlRenderBackend, RenderOpts |
| 18 | + |
| 19 | + |
| 20 | +HTML = """ |
| 21 | +<!DOCTYPE html> |
| 22 | +<html> |
| 23 | +<head> |
| 24 | + <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.12.2/gsap.min.js"></script> |
| 25 | + <style> |
| 26 | + body { margin:0; padding:0; background:#0f172a; overflow:hidden; |
| 27 | + font-family: -apple-system, Arial, sans-serif; } |
| 28 | + #stage { width:1920px; height:1080px; position:relative; } |
| 29 | + .title { font-size:96px; font-weight:800; color:#f8fafc; |
| 30 | + position:absolute; top:50%; left:50%; |
| 31 | + transform:translate(-50%,-50%); opacity:0; } |
| 32 | + .subtitle { font-size:44px; color:#94a3b8; |
| 33 | + position:absolute; top:62%; left:50%; |
| 34 | + transform:translate(-50%,-50%); opacity:0; } |
| 35 | + .dot { width:32px; height:32px; border-radius:50%; background:#38bdf8; |
| 36 | + position:absolute; top:78%; left:50%; |
| 37 | + transform:translate(-50%,-50%); opacity:0; } |
| 38 | + </style> |
| 39 | +</head> |
| 40 | +<body> |
| 41 | + <div id="stage" data-duration="4.0"> |
| 42 | + <div class="title">PraisonAI</div> |
| 43 | + <div class="subtitle">Motion Graphics Pipeline</div> |
| 44 | + <div class="dot"></div> |
| 45 | + </div> |
| 46 | + <script> |
| 47 | + const tl = gsap.timeline({ paused: true }); |
| 48 | + tl.to(".title", { duration: 0.8, opacity: 1, y: -30, ease: "power3.out" }) |
| 49 | + .to(".subtitle", { duration: 0.7, opacity: 1, y: -20, ease: "power3.out" }, "-=0.3") |
| 50 | + .to(".dot", { duration: 0.5, opacity: 1, scale: 2, ease: "back.out(2)" }, "-=0.2") |
| 51 | + .to(".dot", { duration: 0.4, x: 200, ease: "power2.inOut" }) |
| 52 | + .to(".dot", { duration: 0.4, x: -200, ease: "power2.inOut" }) |
| 53 | + .to(".dot", { duration: 0.4, x: 0, ease: "power2.inOut" }) |
| 54 | + .to([".title",".subtitle",".dot"], |
| 55 | + { duration: 0.5, opacity: 0, ease: "power2.in" }); |
| 56 | + // Required: export timeline(s) for the render backend to drive. |
| 57 | + window.__timelines = [tl]; |
| 58 | + </script> |
| 59 | +</body> |
| 60 | +</html> |
| 61 | +""" |
| 62 | + |
| 63 | + |
| 64 | +async def main() -> None: |
| 65 | + out = Path("/tmp/motion_graphics_demo") |
| 66 | + out.mkdir(exist_ok=True) |
| 67 | + (out / "index.html").write_text(HTML) |
| 68 | + |
| 69 | + backend = HtmlRenderBackend(base_dir=out) |
| 70 | + |
| 71 | + lint = await backend.lint(out) |
| 72 | + print(f"Lint: ok={lint.ok} messages={lint.messages}") |
| 73 | + |
| 74 | + opts = RenderOpts(output_name="praisonai_demo.mp4", fps=30, quality="standard") |
| 75 | + print("Render: ...", flush=True) |
| 76 | + result = await backend.render(out, opts) |
| 77 | + |
| 78 | + if result.ok: |
| 79 | + print(f"Render: OK path={result.output_path} size={result.size_kb} KB") |
| 80 | + else: |
| 81 | + print(f"Render: FAIL stderr={result.stderr}") |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + asyncio.run(main()) |
0 commit comments