main
John Lauer
Publish 4 minute R10-to-R11 progression film with player captions
80d34ca
"""Combine historical clean R10 footage and approved R11 scenes; no subtitle burn-in."""
from pathlib import Path
import json,subprocess,hashlib,re
R=Path(__file__).resolve().parents[1];O=R/'videos/r11-progression';O.mkdir(exist_ok=True)
old=R/'videos/full-edit-r10-tight/electronic-load-r10-tight-web.mp4';new=R/'videos/compact-r11/chapter/approved/r11-approved-chapter.mp4';edl=[(old,0,27.833,R/'videos/full-edit-r10-tight/captions.vtt'),(old,36.333,161.533,R/'videos/full-edit-r10-tight/captions.vtt'),(new,0,96.41,R/'videos/compact-r11/chapter/approved/r11-approved-chapter.vtt')]
def sec(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}'
args=[];fs=[];offset=0;cues=[];records=[]
for i,(p,a,b,vtt) in enumerate(edl):
args+=['-i',str(p)];fs+=[f'[{i}:v]trim=start={a}:end={b},setpts=PTS-STARTPTS,scale=1280:720,setsar=1,fps=30[v{i}]',f'[{i}:a]atrim=start={a}:end={b},asetpts=PTS-STARTPTS,aresample=48000,apad,atrim=duration={b-a}[a{i}]']
for block in vtt.read_text().strip().split('\n\n'):
if '-->' not in block:continue
lines=block.splitlines();j=next(k for k,l in enumerate(lines) if '-->' in l);lo,hi=map(sec,lines[j].split(' --> '))
if hi<=a or lo>=b:continue
assert lo>=a-.002 and hi<=b+.002,('sentence crosses edit',lo,hi,a,b)
cues.append(f'{stamp(offset+max(lo,a)-a)} --> {stamp(offset+min(hi,b)-a)}\n'+'\n'.join(lines[j+1:]))
records.append({'source':str(p.relative_to(R)),'sha256':hashlib.sha256(p.read_bytes()).hexdigest(),'start':a,'end':b,'outputStart':offset});offset+=b-a
fs+=['[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[v][a]']
video=O/'electronic-load-r11-progression.mp4';subprocess.run(['ffmpeg','-y',*args,'-filter_complex',';'.join(fs),'-map','[v]','-map','[a]','-c:v','libx264','-preset','fast','-b:v','340k','-maxrate','600k','-bufsize','1200k','-g','30','-pix_fmt','yuv420p','-c:a','aac','-b:a','96k','-movflags','+faststart',str(video)],check=True)
vtt='WEBVTT\n\n'+'\n\n'.join(cues)+'\n';(O/'captions.vtt').write_text(vtt);(O/'captions.srt').write_text('\n\n'.join(f'{i+1}\n'+c.replace('.',',',2) for i,c in enumerate(cues))+'\n')
chapters=[{'t':0,'title':'Earlier R10 design: sourcing and development'},{'t':27.833,'title':'Earlier R10 layout, checks and protection'}]+[{'t':153.033+c['t'],'title':'R11: '+c['title']} for c in json.loads((new.parent/'review.video.json').read_text())['chapters']]
(O/'review.video.json').write_text(json.dumps({'video':'/adom/adom-custom-electronic-load/render/videos/r11-progression/'+video.name,'title':'Electronic load: from R10 development to compact R11','captions':vtt,'chapters':chapters},indent=2)+'\n')
(O/'edit-report.json').write_text(json.dumps({'durationPlannedSeconds':offset,'edl':records,'sha256':hashlib.sha256(video.read_bytes()).hexdigest(),'bytes':video.stat().st_size,'burnedSubtitles':False,'newUnapprovedAssemblyFootageIncluded':False,'historicalFootage':'Retained source R10/Board34 labels distinguish the earlier design; engineering statements refer to that revision.','status':'Combined process edit; not completed board or thermal qualification'},indent=2)+'\n')
print(offset,video)