main
John Lauer
Record six R11 clip approvals and assemble caption-clean review chapter
5962882
"""Assemble only explicitly approved, hash-verified R11 clips; no caption overlay."""
from pathlib import Path
import json, hashlib, subprocess, re
p=Path(__file__).resolve().parents[1]/'videos/compact-r11/chapter'
rows=json.loads((p/'publication-review.json').read_text())
story={r['id']:r for r in json.loads((p/'storyboard.json').read_text())['clips']}
out=p/'approved';out.mkdir(exist_ok=True)
inputs=[]; filters=[]; chapters=[]; cues=[]; offset=0
def seconds(s):
h,m,v=s.split(':');return int(h)*3600+int(m)*60+float(v)
def stamp(t):
n=round(t*1000);return f'{n//3600000:02}:{n//60000%60:02}:{n//1000%60:02}.{n%1000:03}'
for i,r in enumerate(rows):
f=p/(r['id']+'-review.mp4')
assert r['userApproved'] and hashlib.sha256(f.read_bytes()).hexdigest()==r['sha256']
duration=float(subprocess.check_output(['ffprobe','-v','error','-show_entries','format=duration','-of','csv=p=0',str(f)]))
inputs+=['-i',str(f)]
filters += [f'[{i}:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fps=30,trim=duration={duration},setpts=PTS-STARTPTS[v{i}]',f'[{i}:a]aresample=48000,apad,atrim=duration={duration},asetpts=PTS-STARTPTS[a{i}]']
chapters.append({'t':round(offset,3),'title':story[r['id']]['title']})
for block in (p/(r['id']+'.vtt')).read_text().strip().split('\n\n'):
if '-->' not in block:continue
lines=block.splitlines(); timing=next(j for j,line in enumerate(lines) if '-->' in line)
a,b=lines[timing].split(' --> ')
cues.append(f'{stamp(offset+seconds(a))} --> {stamp(offset+seconds(b))}\n'+ '\n'.join(lines[timing+1:]))
offset+=duration
filters.append(''.join(f'[v{i}][a{i}]' for i in range(len(rows)))+f'concat=n={len(rows)}:v=1:a=1[v][a]')
subprocess.run(['ffmpeg','-y',*inputs,'-filter_complex',';'.join(filters),'-map','[v]','-map','[a]','-c:v','libx264','-preset','fast','-crf','25','-g','30','-pix_fmt','yuv420p','-c:a','aac','-b:a','128k','-movflags','+faststart',str(out/'r11-approved-chapter.mp4')],check=True)
vtt='WEBVTT\n\n'+'\n\n'.join(cues)+'\n';(out/'r11-approved-chapter.vtt').write_text(vtt)
(out/'r11-approved-chapter.srt').write_text('\n\n'.join(str(i+1)+'\n'+c.replace('.',',',2) for i,c in enumerate(cues))+'\n')
(out/'review.video.json').write_text(json.dumps({'video':'/adom/adom-custom-electronic-load/render/videos/compact-r11/chapter/approved/r11-approved-chapter.mp4','title':'R11 approved design-progression chapter','captions':vtt,'chapters':chapters},indent=2)+'\n')
(out/'assembly.json').write_text(json.dumps({'status':'Assembled approved R11 chapter; not full historical process film','sourceApprovals':rows,'durationSeconds':offset,'burnedCaptions':False,'newAssemblyFootageIncluded':False,'sha256':hashlib.sha256((out/'r11-approved-chapter.mp4').read_bytes()).hexdigest()},indent=2)+'\n')
print(json.dumps({'durationSeconds':offset,'output':str(out)}))