KAZIK/tools/generators/pack_studio_hd.py

49 lines
2.6 KiB
Python

"""Package independent Full HD JPEG frames for the assembly GDI+ presenter."""
from pathlib import Path
import argparse,struct,json,hashlib,io
from PIL import Image,ImageEnhance
ROOT=Path(__file__).resolve().parents[2]
VERSIONS = {4: (8,.3,.5,1), 5: (24,.45,.8,4), 6: (12,.16,.46,4), 7: (12,.12,.44,6), 8: (16,.12,.42,8)}
def frame_gain(index, duration, fade_in, fade_out):
age = index / 24
return min(1, age / fade_in, max(0, (duration - age) / fade_out))
def main():
parser=argparse.ArgumentParser()
parser.add_argument('--version',type=int,choices=VERSIONS,default=8)
parser.add_argument('--frames',type=Path)
args=parser.parse_args()
duration, fade_in, fade_out, shots = VERSIONS[args.version]
count=duration*24
default_folder = 'build/cinematic-v4/workshop-clutter' if args.version==4 else f'build/cinematic-v{args.version}/frames'
folder=args.frames or ROOT/default_folder
files=sorted(folder.glob('frame_*.png'));assert len(files)==count,len(files)
assert [p.name for p in files]==[f'frame_{i:04d}.png' for i in range(1,count+1)]
out=ROOT/f'assets/animations/intro/eblan-studio-v{args.version}.hfm';temp=out.with_suffix('.hfm.tmp')
header=struct.pack('<4s7I',b'HFM4',4,1920,1080,24,count,2*1024*1024,duration*1000)
entries=[];offset=32+count*8
with temp.open('wb') as target:
target.write(header+b'\0'*(count*8))
for i,file in enumerate(files):
im=Image.open(file).convert('RGB');assert im.size==(1920,1080),im.size
# Baked photographic fade keeps the full-resolution path independent of
# the 640x480 gameplay HUD and its old software fade.
gain=frame_gain(i,duration,fade_in,fade_out)
if gain<1:im=ImageEnhance.Brightness(im).enhance(gain)
buf=io.BytesIO();im.save(buf,format='JPEG',quality=94,subsampling=0)
raw=buf.getvalue();assert 0<len(raw)<=2*1024*1024
entries.append((offset,len(raw)));offset+=len(raw);target.write(raw)
target.seek(32);target.write(b''.join(struct.pack('<2I',*e) for e in entries))
temp.replace(out)
with out.open('rb') as file:digest=hashlib.file_digest(file,'sha256').hexdigest()
out.with_suffix('.json').write_bytes((json.dumps(dict(format='HFM4 / independent JPEG',size=[1920,1080],frames=count,fps=24,duration=duration,bytes=offset,sha256=digest,source=f'assets/cinematics/blender/eblan-workshop-v{args.version}.blend',shots=shots),indent=2)+'\n').encode())
print('Packed Full HD studio movie:',round(offset/1024/1024,1),'MiB')
if __name__=='__main__':main()