KAZIK/tools/previews/export_studio_v5.py

130 lines
4.3 KiB
Python

"""Export a studio review MP4 or a reusable ProRes master with stereo sound."""
import argparse
import struct
import subprocess
import sys
from pathlib import Path
from PIL import Image, ImageEnhance
ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "tools/generators"))
from pack_studio_hd import VERSIONS, frame_gain
def game_frames(movie_path, count, duration):
with movie_path.open("rb") as source:
header = struct.unpack("<4s7I", source.read(32))
if header != (b"HFM4", 4, 1920, 1080, 24, count, 2 * 1024 * 1024, duration * 1000):
raise ValueError(f"Invalid HFM header in {movie_path}")
index = [struct.unpack("<2I", source.read(8)) for _ in range(count)]
for offset, size in index:
source.seek(offset)
frame = source.read(size)
if len(frame) != size:
raise ValueError(f"Incomplete frame at offset {offset}")
yield frame
def production_frames(directory, duration, fade_in, fade_out):
for i in range(duration * 24):
with Image.open(directory / f"frame_{i + 1:04d}.png") as source:
if source.size != (1920, 1080):
raise ValueError(f"Invalid dimensions on frame {i + 1}")
frame = source.convert("RGB")
gain = frame_gain(i, duration, fade_in, fade_out)
if gain < 1:
frame = ImageEnhance.Brightness(frame).enhance(gain)
yield frame.tobytes()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--ffmpeg", required=True)
parser.add_argument("--version", type=int, choices=(5, 6, 7, 8), default=8)
parser.add_argument(
"--master", action="store_true", help="Encode ProRes 422 HQ directly from Blender renders"
)
args = parser.parse_args()
version = args.version
duration, fade_in, fade_out, _ = VERSIONS[version]
work = ROOT / f"build/cinematic-v{version}"
audio = ROOT / f"assets/audio/startup/studio-workshop-v{version}.wav"
if args.master:
out = ROOT / f"assets/cinematics/masters/Eblan-Games-master-v{version}.mov"
frames = production_frames(work / "frames", duration, fade_in, fade_out)
decoder = ["-f", "rawvideo", "-pixel_format", "rgb24", "-video_size", "1920x1080"]
codec = ["-c:v", "prores_ks", "-profile:v", "3", "-pix_fmt", "yuv422p10le", "-c:a", "pcm_s24le"]
else:
out = (
ROOT / "assets/cinematics/masters/Eblan-Games-v8.mp4"
if version == 8
else work / f"eblan-studio-v{version}-preview.mp4"
)
frames = game_frames(
ROOT / f"assets/animations/intro/eblan-studio-v{version}.hfm", duration * 24, duration
)
decoder = ["-f", "image2pipe", "-c:v", "mjpeg"]
codec = [
"-c:v",
"libx264",
"-preset",
"slow",
"-crf",
"17",
"-pix_fmt",
"yuv420p",
"-c:a",
"aac",
"-b:a",
"192k",
]
out.parent.mkdir(parents=True, exist_ok=True)
temporary = out.with_name(out.stem + ".partial" + out.suffix)
command = [
args.ffmpeg,
"-y",
"-hide_banner",
"-loglevel",
"error",
"-xerror",
*decoder,
"-framerate",
"24",
"-i",
"pipe:0",
"-i",
str(audio),
"-t",
str(duration),
"-vf",
"scale=out_color_matrix=bt709:out_range=tv",
"-color_primaries",
"bt709",
"-color_trc",
"bt709",
"-colorspace",
"bt709",
*codec,
"-movflags",
"+faststart",
str(temporary),
]
try:
with subprocess.Popen(command, stdin=subprocess.PIPE) as encoder:
try:
for frame in frames:
encoder.stdin.write(frame)
finally:
encoder.stdin.close()
if encoder.wait():
raise RuntimeError("Studio encode failed")
temporary.replace(out)
finally:
temporary.unlink(missing_ok=True)
print(out)
if __name__ == "__main__":
main()