KAZIK/tools/previews/export_startup_film.py

55 lines
2.2 KiB
Python

"""Export captured production MASM frames and original audio for review only."""
import argparse
from pathlib import Path
import shutil
import subprocess
import sys
import wave
ROOT = Path(__file__).resolve().parents[2]
OUT = ROOT/'build/scratch'
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--ffmpeg')
args = parser.parse_args()
encoder = args.ffmpeg or shutil.which('ffmpeg')
if not encoder:
sys.path.insert(0,str(OUT/'video-tools'))
try:
from imageio_ffmpeg import get_ffmpeg_exe
encoder = get_ffmpeg_exe()
except ImportError:
raise SystemExit('Optional export needs ffmpeg; pass --ffmpeg path. The game does not need it.')
audio = OUT/'startup-film-preview.wav'
with wave.open(str(audio),'wb') as target:
target.setparams((2,2,22050,0,'NONE','not compressed'))
for name in ['studio-ident.wav','casino-title.wav']:
with wave.open(str(ROOT/'assets/audio/startup'/name),'rb') as source:
assert (source.getnchannels(),source.getsampwidth(),source.getframerate()) == (2,2,22050)
target.writeframes(source.readframes(source.getnframes()))
video = OUT/'startup-film-preview.mp4'
command = [encoder,'-hide_banner','-loglevel','error','-y',
'-f','rawvideo','-pixel_format','bgra','-video_size','640x480',
'-framerate','20','-i','pipe:0','-i',str(audio),
'-c:v','libx264','-preset','slow','-crf','18','-pix_fmt','yuv420p',
'-c:a','aac','-b:a','160k','-movflags','+faststart','-shortest',str(video)]
process = subprocess.Popen(command,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
try:
for index in range(400):
raw = (OUT/f'boot-motion-{index:03}.bgra').read_bytes()
assert len(raw) == 640*480*4
process.stdin.write(raw)
process.stdin.close()
error = process.stderr.read().decode(errors='replace')
assert process.wait(timeout=120) == 0,error
except BaseException:
process.kill()
process.wait()
raise
print(f'Exported 20 seconds / 400 production frames with stereo audio: {video}')
if __name__ == '__main__':
main()