Dup Ver Goto 📝

ExampleSvgFfmpeg

PT2/lang/python/animation does not exist
To
35 lines, 113 words, 922 chars Page 'ExampleSvgFfmpeg' does not exist.

The idea is that we have a function or a callable that takes the frame number as its parameter and generates SVG. Then we pass that to resvg, and then we have a bunch of PNG's that we then give to ffmpeg. This example just gets a simple moving circle.

#!/usr/bin/env python3
from subprocess import run

def main():
  for i in range(24):
    frame = make_frame(i)
    frame_svg = f"frame_{i:04d}.svg"
    frame_png = f"frame_{i:04d}.png"
    with open(frame_svg,"wt") as f:
      f.write(frame)
    run(["resvg",frame_svg,frame_png])
  run(["ffmpeg","-i",frame_png,"-r","24","out.mp4"])

def svg_wrap(w,h,x):
  return f"""<svg xmlns="http://www.w3.org/2000/svg" width="{w}" height="{h}">
  {x}
  </svg>"""


def make_frame(i):
  x = 100
  svg = f"""
  <circle cx="{i*10+x}" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
  """
  return svg_wrap(640,480,svg)

if __name__ == "__main__":
  main()