# For every cue of an SRT, measures the mean volume of scene.wav inside the cue window (ffmpeg volumedetect), plus control windows.
import re, subprocess, sys
def secs(t): h,m,s=t.replace(",",".").split(":"); return int(h)*3600+int(m)*60+float(s)
def mean_db(a,b):
    r=subprocess.run(["ffmpeg","-v","info","-ss",f"{a}","-t",f"{b-a}","-i","scene.wav","-af","volumedetect","-f","null","-"],capture_output=True,text=True).stderr
    m=re.search(r"mean_volume: (\S+) dB",r); return float(m.group(1)) if m else None
text=open(sys.argv[1]).read()
print(f"{'window':<28}{'mean volume':>12}")
for blk in re.split(r"\n\s*\n",text.strip()):
    rows=blk.split("\n"); m=re.match(r"(\S+) --> (\S+)",rows[1]); a,b=secs(m[1]),secs(m[2])
    print(f"cue {rows[0]} {a:6.2f}-{b:6.2f} s {mean_db(a,b):>10.1f} dB  {rows[2][:30]}")
for a,b,lab in ((11.0,11.9,"control: gap, nothing placed"),(3.85,4.15,"control: gap between cues 2, 3")):
    print(f"{lab:<28}{a:5.1f}-{b:5.1f} s {mean_db(a,b):>5.1f} dB")
