#!/usr/bin/env python3 """Apply one verified final mix to all four v10 exports, then refresh receipts.""" import hashlib import json from pathlib import Path import subprocess BASE = Path(__file__).resolve().parents[2] / 'output/video-refresh-20261023/workspace/neutral-refresh-20261023' def audio_hash(path): return hashlib.sha256(subprocess.check_output(['ffmpeg', '-v', 'error', '-i', str(path), '-map', '0:a:0', '-c:a', 'copy', '-f', 'adts', '-'])).hexdigest() def main(): audio = BASE / 'marketing-v10-final-audio.m4a' level_output = subprocess.run(['ffmpeg', '-hide_banner', '-i', str(audio), '-af', 'loudnorm=I=-16:TP=-1.5:LRA=11:print_format=json', '-f', 'null', '-'], capture_output=True, text=True, check=True).stderr levels = json.loads(level_output[level_output.rfind('{'):level_output.rfind('}') + 1]) assert float(levels['input_tp']) < -.5, levels assert abs(float(levels['input_i']) + 16) < .75, levels digest = audio_hash(audio) files = sorted((BASE / 'export').rglob('*.mp4')) assert len(files) == 4 for path in files: temporary = path.with_name(path.stem + '.final-audio.tmp.mp4') subprocess.run(['ffmpeg', '-v', 'error', '-y', '-i', str(path), '-i', str(audio), '-map', '0:v:0', '-map', '1:a:0', '-c', 'copy', '-movflags', '+faststart', str(temporary)], check=True) temporary.replace(path) result = subprocess.run(['ffmpeg', '-v', 'error', '-i', str(path), '-f', 'null', '-'], capture_output=True, text=True, check=True) assert not result.stderr, (path, result.stderr) probe = json.loads(subprocess.check_output(['ffprobe', '-v', 'error', '-show_streams', '-show_format', '-of', 'json', str(path)])) video = next(s for s in probe['streams'] if s['codec_type'] == 'video') assert video['avg_frame_rate'] == '60/1' assert abs(float(probe['format']['duration']) - 5554 / 60) < .15 assert audio_hash(path) == digest probe.update(full_decode=True, final_limited_audio=True, audio_sha256=digest, sha256=hashlib.file_digest(path.open('rb'), 'sha256').hexdigest()) path.with_suffix('.verification.json').write_text(json.dumps(probe, indent=2) + '\n') print('Final mix and full decode verified:', path.name, flush=True) (BASE / 'final-audio-verification.json').write_text(json.dumps({'audio_sha256': digest, 'levels': levels, 'all_four_exports_match': True}, indent=2) + '\n') receipt = BASE / 'narration-receipt.json' data = json.loads(receipt.read_text()) data['final_audio_replacement_pending'] = False receipt.write_text(json.dumps(data, indent=2) + '\n') if __name__ == '__main__': main()