#!/usr/bin/env python3 """Derive a bounded poster/bootstrap from the exact user-authorized immutable pack. CPU decode only. Portrait outputs stay ignored; the metadata records exact provenance. """ import argparse, hashlib, io, json, subprocess from pathlib import Path from PIL import Image ROOT=Path(__file__).resolve().parents[1] MEDIA=json.loads((ROOT/'manifests/character-build-profiles.json').read_bytes())['mediaBundles']['black-correct03'] PIN=MEDIA['indexSha256'] ASSET_ROOT=MEDIA['assetRoot'] BASIC_ROOT=MEDIA['bootstrapRoot'] sha=lambda data:hashlib.sha256(data).hexdigest() def derive(source,media_bundle='black-correct03'): media=json.loads((ROOT/'manifests/character-build-profiles.json').read_bytes())['mediaBundles'][media_bundle] pin=media['indexSha256'];asset_root=media['assetRoot'] assert media.get('userAuthorizedDebug') is True,'authorized debug bundle required' index=source/'manifest.json';assert sha(index.read_bytes())==pin,'exact index drift' m=json.loads(index.read_bytes());row=next(r for r in m['conversationPackages'] if r['id']==m['fallbackPackageId']) runtime=source/row['runtimeManifest']['path'];assert sha(runtime.read_bytes())==row['runtimeManifest']['sha256'] body=json.loads(runtime.read_bytes())['body'];name=str(Path(row['runtimeManifest']['path']).parent/body['path']) path=source/name;assert sha(path.read_bytes())==body['sha256'] assert (body['width'],body['height'])==(720,1280) and body['ptsUs'][0]==0 and body['hasAudio'] is False native=subprocess.check_output(['/home/ubuntu/gori-bin/ffmpeg','-v','error','-i',str(path),'-frames:v','1','-f','image2pipe','-vcodec','png','-'],timeout=30) image=Image.open(io.BytesIO(native)).convert('RGB').resize((180,320),Image.Resampling.LANCZOS) out=io.BytesIO();image.save(out,format='PNG',optimize=True);poster=out.getvalue();assert len(poster)<=100_000 meta={'schemaVersion':1,'sourceIndexSha256':pin,'posterSha256':sha(poster),'posterWidth':180,'posterHeight':320, 'provenance':'Exact decoded neutral source frame0; CPU downsample, no generated media; user-authorized debug integration, not Pi certification', 'body':{**body,'path':asset_root+name}} return (json.dumps(meta,indent=2)+'\n').encode(),poster def prepare(source,verify_only=False,media_bundle='black-correct03'): media=json.loads((ROOT/'manifests/character-build-profiles.json').read_bytes())['mediaBundles'][media_bundle] raw,poster=derive(source,media_bundle) outputs={ROOT/media.get('bootstrapMetadata','manifests/startup-black-tulle-correct03.json'):raw, ROOT/'app/src/main/assets'/media['bootstrapRoot']/'manifest.json':raw, ROOT/'app/src/main/assets'/media['bootstrapRoot']/'poster.png':poster, ROOT/media.get('resourceDirectory','app/src/main/res')/'drawable-nodpi'/(media['posterResource']+'.png'):poster} for path,data in outputs.items(): if path.exists():assert path.read_bytes()==data,'Unknown bootstrap bytes preserved: '+str(path) else: assert not verify_only,'missing bootstrap: '+str(path) path.parent.mkdir(parents=True,exist_ok=True);path.write_bytes(data) return {'status':'passed','index_sha256':media['indexSha256'],'bootstrap_sha256':sha(raw),'poster_sha256':sha(poster),'poster_bytes':len(poster),'paid_calls':0} if __name__=='__main__': parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('--source',type=Path,required=True);parser.add_argument('--verify-only',action='store_true') parser.add_argument('--media-bundle',default='black-correct03') args=parser.parse_args();print(json.dumps(prepare(args.source,args.verify_only,args.media_bundle),indent=2))