"""The Director's and the assistant's talking loops (ArtSource/Director/video/*_src.webm, 768x1168, 24 fps, figure on black) -> Assets/StreamingAssets/Talk/.webm, VP8 with alpha, which the game's VideoPlayer plays behind the call and the hint. The black is cut away by flooding in from the frame's edge over near-black pixels, so dark trousers and shoes inside the figure's outline stay solid; the edge is softened by a pixel. Both are cut to head-to-hips (the user: no need to show them full length), a bust that fills the phone and her small window like a video call. python -I Tools/director/talk_videos.py all of them python -I Tools/director/talk_videos.py boss_calm one """ import os, subprocess, sys import numpy as np from scipy import ndimage HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.normpath(os.path.join(HERE, "..", "..")) SRC = os.path.join(ROOT, "ArtSource", "Director", "video") OUT = os.path.join(ROOT, "Assets", "StreamingAssets", "Talk") W, H = 768, 1168 # name: (crop height from the top, output width) JOBS = { "boss_calm": (780, 640), "boss_stern": (780, 640), "boss_angry": (780, 640), "asst_happy": (780, 400), "asst_stern": (780, 400), "asst_fed": (780, 400), } FLOOD, SOLID = 9, 24 # the background is at most this bright; above SOLID a pixel at the edge is fully kept def frames(path): p = subprocess.Popen(["ffmpeg", "-v", "error", "-i", path, "-f", "rawvideo", "-pix_fmt", "rgb24", "-"], stdout=subprocess.PIPE) n = W * H * 3 while True: b = p.stdout.read(n) if len(b) < n: break yield np.frombuffer(b, np.uint8).reshape(H, W, 3) p.wait() def key(rgb): peak = rgb.max(axis=2) dark = peak <= FLOOD lab, _ = ndimage.label(dark) edge = np.unique(np.concatenate([lab[0], lab[-1], lab[:, 0], lab[:, -1]])) bg = np.isin(lab, edge[edge > 0]) # a soft rim: pixels just off the background fade by brightness, then a one-pixel blur near = ndimage.binary_dilation(bg, iterations=2) & ~bg a = np.where(bg, 0.0, 1.0) a[near] = np.clip((peak[near] - FLOOD) / float(SOLID - FLOOD), 0.0, 1.0) a = ndimage.uniform_filter(a, 3) a[~ndimage.binary_dilation(bg, iterations=3)] = 1.0 return (a * 255).astype(np.uint8) def run(name): crop, ow = JOBS[name] oh = int(round(crop * ow / W / 2)) * 2 src = os.path.join(SRC, name + "_src.webm") os.makedirs(OUT, exist_ok=True) dst = os.path.join(OUT, name + ".webm") enc = subprocess.Popen(["ffmpeg", "-v", "error", "-y", "-f", "rawvideo", "-pix_fmt", "rgba", "-s", f"{W}x{crop}", "-r", "24", "-i", "-", "-vf", f"scale={ow}:{oh}:flags=lanczos", "-c:v", "libvpx", "-pix_fmt", "yuva420p", "-auto-alt-ref", "0", "-b:v", "1800k", "-crf", "10", "-deadline", "good", "-an", dst], stdin=subprocess.PIPE) n = 0 for rgb in frames(src): # keyed whole, then cut: the cut edge must not count as frame edge the black floods in from rgba = np.dstack([rgb, key(rgb)])[:crop] enc.stdin.write(rgba.tobytes()) n += 1 enc.stdin.close() enc.wait() print(name, n, "frames ->", dst, os.path.getsize(dst) // 1024, "KB") if __name__ == "__main__": for name in (sys.argv[1:] or list(JOBS)): run(name)