main
John Lauer
Record native widget playback, captions, chapters and report-image verification
6d99137
"""Edited voiceover on authentic Pup window footage; captions are sentence-level estimates."""
from pathlib import Path
import json,subprocess,shutil,re,hashlib,math,textwrap
R=Path(__file__).resolve().parents[1];O=R/'videos/cooling-volume';B=R/'videos/full-edit-r10-tight';F=R/'videos/full-edit-with-thermal';F.mkdir(exist_ok=True)
def run(args):subprocess.run(args,check=True,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
def duration(p):return float(subprocess.check_output(['ffprobe','-v','error','-show_entries','format=duration','-of','csv=p=0',str(p)],text=True))
def stamp(t):
ms=round(t*1000);return f'{ms//3600000:02}:{ms//60000%60:02}:{ms//1000%60:02}.{ms%1000:03}'
receipts=json.loads((O/'native-recordings.json').read_text());pulled=json.loads((O/'pull-receipt.json').read_text())['files'];scenes=json.loads((O/'narration.json').read_text());t=0;cues=[];chapters=[];segments=[];audit=[]
for i,s in enumerate(scenes):
raw=O/(s['id']+'-raw.mp4');shutil.copyfile(pulled[i]['path'],raw);assert hashlib.sha256(raw.read_bytes()).hexdigest()==pulled[i]['sha256']
audio=O/(s['id']+'-tight.wav');run(['ffmpeg','-y','-i',str(O/(s['id']+'.mp3')),'-af','silenceremove=start_periods=1:start_threshold=-40dB:start_silence=0.04:stop_periods=-1:stop_duration=0.7:stop_threshold=-40dB:stop_silence=0.25','-ar','48000','-ac','2',str(audio)])
d=duration(audio);video=O/(s['id']+'-edited.mp4');available=duration(raw);speed=min(1.,d/available)
# Compress the full recorded action into the spoken duration; no repeated/frozen tail.
run(['ffmpeg','-y','-i',str(raw),'-i',str(audio),'-filter_complex',f'[0:v]setpts={speed}*(PTS-STARTPTS),scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,fps=30,setsar=1[v]','-map','[v]','-map','1:a','-t',str(d),'-c:v','libx264','-preset','fast','-crf','23','-g','30','-pix_fmt','yuv420p','-c:a','aac','-b:a','96k','-movflags','+faststart',str(video)])
actual=duration(video);segments.append(video);chapters.append({'t':t,'title':s['id'].replace('-',' ')})
sentences=re.split(r'(?<=[.!?])\s+',s['text']);weights=[len(x.split()) for x in sentences];total=sum(weights);offset=t
for sentence,w in zip(sentences,weights):
end=offset+d*w/total;cues.append({'start':offset,'end':end,'text':'\n'.join(textwrap.wrap(sentence,44))});offset=end
audit.append({'id':s['id'],'rawSeconds':available,'audioSeconds':d,'editedSeconds':actual,'visualSpeed':1/speed,'sha256':hashlib.sha256(raw.read_bytes()).hexdigest()});t+=actual
# Contact sheet source frames from each beat.
run(['ffmpeg','-y','-ss',str(min(3,d/3)),'-i',str(video),'-frames:v','1',str(O/(s['id']+'-frame.jpg'))])
(O/'concat.txt').write_text(''.join("file '"+str(p)+"'\n" for p in segments));addon=O/'thermal-addon.mp4';run(['ffmpeg','-y','-f','concat','-safe','0','-i',str(O/'concat.txt'),'-c','copy','-movflags','+faststart',str(addon)])
(O/'captions.vtt').write_text('WEBVTT\n\n'+'\n\n'.join(f"{stamp(c['start'])} --> {stamp(c['end'])}\n{c['text']}" for c in cues)+'\n');(O/'chapters.json').write_text(json.dumps(chapters,indent=2))
base=B/'electronic-load-r10-captioned-web.mp4';base_d=duration(base);(F/'concat.txt').write_text(f"file '{base}'\nfile '{addon}'\n");full=F/'electronic-load-with-thermal-web.mp4'
run(['ffmpeg','-y','-f','concat','-safe','0','-i',str(F/'concat.txt'),'-c:v','libx264','-preset','fast','-b:v','340k','-maxrate','380k','-bufsize','760k','-g','30','-keyint_min','30','-pix_fmt','yuv420p','-c:a','aac','-b:a','64k','-movflags','+faststart',str(full)])
old=json.loads((B/'review.video.json').read_text());vtt=(B/'captions.vtt').read_text().rstrip()+'\n\n'+'\n\n'.join(f"{stamp(base_d+c['start'])} --> {stamp(base_d+c['end'])}\n{c['text']}" for c in cues)+'\n';chs=old['chapters']+[{'t':base_d+x['t'],'title':'Thermal: '+x['title']} for x in chapters]
(F/'captions.vtt').write_text(vtt);(F/'chapters.json').write_text(json.dumps(chs,indent=2));manifest={'video':'/adom/adom-custom-electronic-load/render/videos/full-edit-with-thermal/'+full.name,'title':'Electronic load: build and compact cooling analysis','poster':old['poster'],'captions':vtt,'chapters':chs};(F/'review.video.json').write_text(json.dumps(manifest,indent=2))
report={'duration':duration(full),'bytes':full.stat().st_size,'sha256':hashlib.sha256(full.read_bytes()).hexdigest(),'baseSeconds':base_d,'addonSeconds':duration(addon),'captionTiming':'New segment: sentence-level duration weighted by word count, not forced-aligned; source audio trimmed of pauses >=0.7s to retained 0.25s. Existing captions retained.','segments':audit,'recording':'Native Pup window footage; separately synthesized Adom TTS voiceover added in edit.','sourceStatus':'Fields source PR pending owner review, not installed release','hardwareQualified':False};(F/'edit-report.json').write_text(json.dumps(report,indent=2));print(json.dumps(report),flush=True)