Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103"""Cut an animatic (one labelled card per shot) to the song, to review pacing before anything costs money.
uv run --with pillow python previz/animatic.py --song song.m4a [--anim old_video.mp4] # animatic uv run --with pillow python previz/animatic.py --song song.m4a [--anim old_video.mp4] --live # storyboard
Reads previz/shots.json (retime.py) and previz/keyframes.json (the prompt generator; for camera-move labels).Cards show id, time range, duration, lyric (or title) and chapter/move. With --anim (a previous animation or storyboardvideo), stills are pulled from it at each shot's o_still time (previz/stills/<id>_{first,mid,last}.jpg); without it,cards show the shot description on a dark background.Outputs: previz/cards/<id>.png + previz/animatic.mp4. With --live, each shot uses previz/keyframes/<id>.png (and<id>_end.png) when they exist, the old still shrinks to a corner inset for comparison: previz/cards_live/,previz/storyboard.mp4. A start/end pair splits the shot's time in half."""import argparse, json, pathlib, subprocessfrom PIL import Image, ImageDraw, ImageFont
OUT = pathlib.Path(__file__).resolve().parentap = argparse.ArgumentParser()ap.add_argument("--song", required=True)ap.add_argument("--anim", help="previous animation/storyboard video to pull reference stills from")ap.add_argument("--live", action="store_true", help="use generated keyframes where they exist")a = ap.parse_args()CARDS, VIDEO = (OUT / "cards_live", OUT / "storyboard.mp4") if a.live else (OUT / "cards", OUT / "animatic.mp4")shots = json.load(open(OUT / "shots.json"))(OUT / "stills").mkdir(exist_ok=True); CARDS.mkdir(exist_ok=True)
def font(size): for f in ("/System/Library/Fonts/Supplemental/Arial Bold.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "C:/Windows/Fonts/arialbd.ttf"): if pathlib.Path(f).exists(): return ImageFont.truetype(f, size) return ImageFont.load_default(size)big, small = font(34), font(22)
def grab(t, path): if a.anim and t is not None and not path.exists(): subprocess.run(["ffmpeg", "-v", "error", "-y", "-ss", f"{max(t, 0):.3f}", "-i", a.anim, "-frames:v", "1", "-q:v", "3", str(path)], check=True)
def wrap(draw, text, font, width): words, lines, cur = text.split(), [], "" for w in words: if draw.textlength(cur + " " + w, font=font) > width and cur: lines.append(cur); cur = w else: cur = (cur + " " + w).strip() return lines + [cur]
def label(img, s, note): d = ImageDraw.Draw(img, "RGBA") d.rectangle([0, 0, 1280, 52], fill=(0, 0, 0, 170)) d.text((16, 9), f'{s["id"]}{s.get("part", "")} {s["t0"]:.2f}–{s["t1"]:.2f}s ({s["dur"]:.2f}s)', font=big, fill="white") d.text((1264, 14), (note or s.get("chapter") or "")[:48], font=small, fill=(255, 220, 120), anchor="ra") lines = wrap(d, s["lyric"] if s.get("lyric") not in ("—", "", None) else s.get("title") or "(instrumental)", big, 1200) h = 16 + 44 * len(lines) d.rectangle([0, 720 - h, 1280, 720], fill=(0, 0, 0, 170)) for i, ln in enumerate(lines): d.text((640, 720 - h + 8 + 44 * i), ln, font=big, fill="white", anchor="ma")
def blank(s): img = Image.new("RGB", (1280, 720), (28, 28, 34)) d = ImageDraw.Draw(img) for i, ln in enumerate(wrap(d, s.get("shot", ""), small, 1100)[:12]): d.text((90, 120 + 32 * i), ln, font=small, fill=(210, 210, 210)) return img
kf = OUT / "keyframes.json"MOVES = {k["shot"]: k.get("move", "") for k in json.load(open(kf))} if kf.exists() else {}for s in shots: sid = s["id"] s["move"] = MOVES.get(sid, "") mid = OUT / "stills" / f"{sid}_mid.jpg" grab(s.get("o_still"), mid) if "o0" in s: # first/last frames of the source shot (or of this part of a split shot) span = (s["o1"] - s["o0"]) / (2 if "part" not in s else 2 * len([x for x in shots if x.get("o0") == s["o0"]])) grab(s["o_still"] - span + .1, OUT / "stills" / f"{sid}_first.jpg") grab(s["o_still"] + span - .1, OUT / "stills" / f"{sid}_last.jpg")
ref = Image.open(mid).convert("RGB").resize((1280, 720)) if mid.exists() else blank(s) live, live_end = OUT / "keyframes" / f"{sid}.png", OUT / "keyframes" / f"{sid}_end.png" frames = [p for p in (live, live_end) if a.live and p.exists()] or [None] s["cards"] = [] for k, src in enumerate(frames): img = ref.copy() if src: img = Image.open(src).convert("RGB").resize((1280, 720), Image.LANCZOS) img.paste(ref.resize((256, 144)), (1280 - 256 - 12, 60)) name = f"{sid}{'_end' if k else ''}.png" label(img, s, ("END · " if k else "") + (s.get("move", "") if len(frames) > 1 else "")) img.save(CARDS / name); s["cards"].append(name)
# concat demuxer: each card held for its exact shot durationlst = CARDS / "list.txt"with open(lst, "w") as f: for s in shots: # a start/end pair splits the shot's time in half for name in s["cards"]: f.write(f"file '{name}'\nduration {s['dur'] / len(s['cards']):.3f}\n") f.write(f"file '{shots[-1]['cards'][-1]}'\n")subprocess.run(["ffmpeg", "-v", "error", "-y", "-f", "concat", "-safe", "0", "-i", str(lst), "-i", a.song, "-map", "0:v", "-map", "1:a", "-vf", "fps=30,format=yuv420p", "-c:v", "libx264", "-crf", "23", "-tune", "stillimage", "-c:a", "aac", "-b:a", "192k", "-shortest", str(VIDEO)], check=True)print("wrote", VIDEO, "| live frames:", sum((OUT / "keyframes" / f"{s['id']}.png").exists() for s in shots) if a.live else "-")