123 lines
5.3 KiB
Python
123 lines
5.3 KiB
Python
|
|
"""Trailer (MP4, 1920x1080) from screenshots: slow Ken-Burns move per image, soft cross-fades,
|
||
|
|
music, end card with title + tagline + footer. Generalised from GladiatorIdle/docs/itchio/make_trailer.py.
|
||
|
|
Upload to YouTube, put the link into itch.io "Gameplay video or trailer". Needs ffmpeg in PATH.
|
||
|
|
|
||
|
|
py -3.12 itch_trailer.py --shots docs/itchio/screenshots --music game/assets/Audio/music_game.mp3 \
|
||
|
|
--title "PIPEFIX" --tagline "Lay the pipes. Beat the flow." --footer "sn09.itch.io/pipefix" \
|
||
|
|
--out docs/itchio/trailer.mp4 [--order 01_menu,03_play,...] [--secs 3.2] [--card-bg background_1920x1200.jpg]
|
||
|
|
[--font C:/Windows/Fonts/seguibl.ttf] [--color "#4fd8ff"]
|
||
|
|
Without --order all PNG/JPG in --shots are used in name order.
|
||
|
|
"""
|
||
|
|
import argparse
|
||
|
|
import os
|
||
|
|
import subprocess
|
||
|
|
import tempfile
|
||
|
|
|
||
|
|
from PIL import Image, ImageDraw, ImageFont, ImageFilter
|
||
|
|
|
||
|
|
FADE, FPS = 0.6, 30
|
||
|
|
|
||
|
|
|
||
|
|
def hex_rgb(s):
|
||
|
|
s = s.lstrip("#")
|
||
|
|
return tuple(int(s[i:i + 2], 16) for i in (0, 2, 4))
|
||
|
|
|
||
|
|
|
||
|
|
def fit(path, text, size, max_w):
|
||
|
|
while size > 12:
|
||
|
|
f = ImageFont.truetype(path, size)
|
||
|
|
if f.getbbox(text)[2] <= max_w:
|
||
|
|
return f
|
||
|
|
size -= 4
|
||
|
|
return ImageFont.truetype(path, size)
|
||
|
|
|
||
|
|
|
||
|
|
def end_card(path, a, first_shot):
|
||
|
|
src = a.card_bg or first_shot
|
||
|
|
im = Image.open(src).convert("RGB")
|
||
|
|
k = max(1920 / im.width, 1080 / im.height)
|
||
|
|
im = im.resize((round(im.width * k), round(im.height * k)), Image.LANCZOS)
|
||
|
|
im = im.crop(((im.width - 1920) // 2, (im.height - 1080) // 2, (im.width - 1920) // 2 + 1920, (im.height - 1080) // 2 + 1080))
|
||
|
|
im = Image.blend(im.filter(ImageFilter.GaussianBlur(4)), Image.new("RGB", im.size, (0, 0, 0)), 0.6)
|
||
|
|
d = ImageDraw.Draw(im)
|
||
|
|
bold = a.font if a.font and os.path.exists(a.font) else r"C:\Windows\Fonts\seguibl.ttf"
|
||
|
|
regular = r"C:\Windows\Fonts\segoeui.ttf"
|
||
|
|
rows = [(a.title, fit(bold, a.title, 150, 1700), 400, hex_rgb(a.color)),
|
||
|
|
(a.tagline, fit(bold, a.tagline, 56, 1700), 560, (240, 236, 228)),
|
||
|
|
(a.footer, fit(regular, a.footer, 40, 1700), 780, (200, 196, 188))]
|
||
|
|
for txt, f, y, col in rows:
|
||
|
|
if not txt:
|
||
|
|
continue
|
||
|
|
d.text((964, y + 4), txt, font=f, fill=(0, 0, 0), anchor="mm")
|
||
|
|
d.text((960, y), txt, font=f, fill=col, anchor="mm")
|
||
|
|
im.save(path)
|
||
|
|
|
||
|
|
|
||
|
|
def main():
|
||
|
|
ap = argparse.ArgumentParser()
|
||
|
|
ap.add_argument("--shots", required=True)
|
||
|
|
ap.add_argument("--music")
|
||
|
|
ap.add_argument("--title", required=True)
|
||
|
|
ap.add_argument("--tagline", default="")
|
||
|
|
ap.add_argument("--footer", default="")
|
||
|
|
ap.add_argument("--out", required=True)
|
||
|
|
ap.add_argument("--order")
|
||
|
|
ap.add_argument("--secs", type=float, default=3.2)
|
||
|
|
ap.add_argument("--card-bg")
|
||
|
|
ap.add_argument("--font")
|
||
|
|
ap.add_argument("--color", default="#4fd8ff")
|
||
|
|
a = ap.parse_args()
|
||
|
|
|
||
|
|
if a.order:
|
||
|
|
names = [n.strip() for n in a.order.split(",") if n.strip()]
|
||
|
|
imgs = []
|
||
|
|
for n in names:
|
||
|
|
p = next((os.path.join(a.shots, n + e) for e in ("", ".png", ".jpg") if os.path.isfile(os.path.join(a.shots, n + e))), None)
|
||
|
|
if not p:
|
||
|
|
raise SystemExit(f"fehlt: {n}")
|
||
|
|
imgs.append(p)
|
||
|
|
else:
|
||
|
|
imgs = sorted(os.path.join(a.shots, f) for f in os.listdir(a.shots) if f.lower().endswith((".png", ".jpg")))
|
||
|
|
if not imgs:
|
||
|
|
raise SystemExit("keine Screenshots")
|
||
|
|
|
||
|
|
dur = a.secs
|
||
|
|
with tempfile.TemporaryDirectory() as t:
|
||
|
|
card = os.path.join(t, "end.png")
|
||
|
|
end_card(card, a, imgs[0])
|
||
|
|
clips = []
|
||
|
|
for i, p in enumerate(imgs + [card]):
|
||
|
|
c = os.path.join(t, f"c{i:02d}.mp4")
|
||
|
|
frames = int((dur + FADE) * FPS)
|
||
|
|
z = "if(eq(on,0),1.0,min(zoom+0.0009,1.12))" if i % 2 == 0 else "if(eq(on,0),1.12,max(zoom-0.0009,1.0))"
|
||
|
|
vf = (f"scale=3840:2160:force_original_aspect_ratio=increase,crop=3840:2160,"
|
||
|
|
f"zoompan=z='{z}':x='iw/2-(iw/zoom/2)':y='ih/2-(ih/zoom/2)':d={frames}:s=1920x1080:fps={FPS},format=yuv420p")
|
||
|
|
subprocess.run(["ffmpeg", "-y", "-loglevel", "error", "-loop", "1", "-i", p, "-vf", vf, "-frames:v", str(frames),
|
||
|
|
"-c:v", "libx264", "-preset", "medium", "-crf", "18", c], check=True)
|
||
|
|
clips.append(c)
|
||
|
|
args = ["ffmpeg", "-y", "-loglevel", "error"]
|
||
|
|
for c in clips:
|
||
|
|
args += ["-i", c]
|
||
|
|
filt, last, off = [], "[0:v]", dur
|
||
|
|
for i in range(1, len(clips)):
|
||
|
|
out = f"[v{i}]"
|
||
|
|
filt.append(f"{last}[{i}:v]xfade=transition=fade:duration={FADE}:offset={off:.2f}{out}")
|
||
|
|
last, off = out, off + dur
|
||
|
|
total = off + FADE
|
||
|
|
if len(clips) == 1:
|
||
|
|
filt, last = ["[0:v]null[v0]"], "[v0]"
|
||
|
|
if a.music:
|
||
|
|
args += ["-i", a.music, "-filter_complex",
|
||
|
|
";".join(filt) + f";[{len(clips)}:a]atrim=0:{total:.2f},afade=t=in:d=1,afade=t=out:st={max(0, total - 2.5):.2f}:d=2.5[a]",
|
||
|
|
"-map", last, "-map", "[a]", "-c:a", "aac", "-b:a", "192k", "-shortest"]
|
||
|
|
else:
|
||
|
|
args += ["-filter_complex", ";".join(filt), "-map", last]
|
||
|
|
args += ["-c:v", "libx264", "-preset", "medium", "-crf", "18", "-pix_fmt", "yuv420p", a.out]
|
||
|
|
os.makedirs(os.path.dirname(os.path.abspath(a.out)), exist_ok=True)
|
||
|
|
subprocess.run(args, check=True)
|
||
|
|
print("ok", a.out, f"{total:.0f} s")
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|