Jotley / website / scripts / video-refresh / finalize-neutral-audio.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Apply one verified final mix to all four v10 exports, then refresh receipts.""" |
| 3 | import hashlib |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | import subprocess |
| 7 | |
| 8 | BASE = Path(__file__).resolve().parents[2] / 'output/video-refresh-20261023/workspace/neutral-refresh-20261023' |
| 9 | |
| 10 | |
| 11 | def audio_hash(path): |
| 12 | return hashlib.sha256(subprocess.check_output(['ffmpeg', '-v', 'error', '-i', str(path), '-map', '0:a:0', '-c:a', 'copy', '-f', 'adts', '-'])).hexdigest() |
| 13 | |
| 14 | |
| 15 | def main(): |
| 16 | audio = BASE / 'marketing-v10-final-audio.m4a' |
| 17 | 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 |
| 18 | levels = json.loads(level_output[level_output.rfind('{'):level_output.rfind('}') + 1]) |
| 19 | assert float(levels['input_tp']) < -.5, levels |
| 20 | assert abs(float(levels['input_i']) + 16) < .75, levels |
| 21 | digest = audio_hash(audio) |
| 22 | files = sorted((BASE / 'export').rglob('*.mp4')) |
| 23 | assert len(files) == 4 |
| 24 | for path in files: |
| 25 | temporary = path.with_name(path.stem + '.final-audio.tmp.mp4') |
| 26 | 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) |
| 27 | temporary.replace(path) |
| 28 | result = subprocess.run(['ffmpeg', '-v', 'error', '-i', str(path), '-f', 'null', '-'], capture_output=True, text=True, check=True) |
| 29 | assert not result.stderr, (path, result.stderr) |
| 30 | probe = json.loads(subprocess.check_output(['ffprobe', '-v', 'error', '-show_streams', '-show_format', '-of', 'json', str(path)])) |
| 31 | video = next(s for s in probe['streams'] if s['codec_type'] == 'video') |
| 32 | assert video['avg_frame_rate'] == '60/1' |
| 33 | assert abs(float(probe['format']['duration']) - 5554 / 60) < .15 |
| 34 | assert audio_hash(path) == digest |
| 35 | probe.update(full_decode=True, final_limited_audio=True, audio_sha256=digest, sha256=hashlib.file_digest(path.open('rb'), 'sha256').hexdigest()) |
| 36 | path.with_suffix('.verification.json').write_text(json.dumps(probe, indent=2) + '\n') |
| 37 | print('Final mix and full decode verified:', path.name, flush=True) |
| 38 | (BASE / 'final-audio-verification.json').write_text(json.dumps({'audio_sha256': digest, 'levels': levels, 'all_four_exports_match': True}, indent=2) + '\n') |
| 39 | receipt = BASE / 'narration-receipt.json' |
| 40 | data = json.loads(receipt.read_text()) |
| 41 | data['final_audio_replacement_pending'] = False |
| 42 | receipt.write_text(json.dumps(data, indent=2) + '\n') |
| 43 | |
| 44 | |
| 45 | if __name__ == '__main__': |
| 46 | main() |