#!/usr/bin/env python3 """WER del texto EN VIVO de un canal (tarea 081): guarda los .ts que va cerrando la ingesta durante N segundos y, al final, compara las palabras que publicó el canal (cues.jsonl, por PTS) contra una referencia de ese mismo audio en pedazos de 30 s. Los pedidos de la referencia van de a uno (gpu02 es compartido). wer_vivo.py [--url U] """ import argparse, json, os, shutil, subprocess, sys, time sys.path.insert(0, "/opt/orbisubs") import numpy as np from orbisubs import SR, MotorHTTP, leer_lista, pts_inicio from bench import wer def main(): ap = argparse.ArgumentParser() ap.add_argument("canal"); ap.add_argument("segundos", type=float); ap.add_argument("out") ap.add_argument("--url", default="http://192.168.2.10:8004") a = ap.parse_args() d = f"/var/www/hls/{a.canal}" cap = a.out + ".ts" os.makedirs(cap, exist_ok=True) vistos, t0 = [], time.time() while time.time() - t0 < a.segundos: segs, _ = leer_lista(f"{d}/src/index.m3u8") for seq, uri, dur, pdt in segs[:-1]: # el último puede estar escribiéndose if seq not in vistos and (not vistos or seq == vistos[-1] + 1): try: shutil.copy(f"{d}/src/{uri}", f"{cap}/{seq}.ts") vistos.append(seq) except FileNotFoundError: pass time.sleep(1) time.sleep(30) # que el canal termine de confirmar el final p0 = pts_inicio(f"{cap}/{vistos[0]}.ts") p1 = pts_inicio(f"{cap}/{vistos[-1]}.ts") + 6.0 lista = f"{cap}/lista.txt" open(lista, "w").write("".join(f"file {cap}/{s}.ts\n" for s in vistos)) pcm = subprocess.run(["ffmpeg", "-v", "error", "-f", "concat", "-safe", "0", "-i", lista, "-vn", "-ac", "1", "-ar", str(SR), "-f", "f32le", "-"], capture_output=True).stdout audio = np.frombuffer(pcm, dtype=np.float32) motor = MotorHTTP(a.url, timeout=60) ref = [] for i in range(0, len(audio), 30 * SR): ref += [(p0 + i / SR + w0, txt) for w0, w1, txt in motor(audio[i:i + 30 * SR], None)] # Se comparan solo las palabras del medio (sin los bordes, donde corta la captura). lo, hi = p0 + 15, p1 - 15 vivo = [] for l in open(f"{d}/cues.jsonl"): try: x = json.loads(l) except ValueError: continue for c0, c1, txt in x.get("cues") or []: if lo <= c0 < hi: vivo.append((c0, txt)) vivo.sort() r_txt = "".join(t for t0_, t in ref if lo <= t0_ < hi) v_txt = " ".join(t for _, t in vivo) r = {"canal": a.canal, "segmentos": len(vistos), "audio_s": round(hi - lo, 1), "wer": round(wer(r_txt, v_txt), 3), "referencia": r_txt.strip(), "vivo": v_txt.strip()} json.dump(r, open(a.out, "w"), ensure_ascii=False, indent=1) shutil.rmtree(cap, ignore_errors=True) print(json.dumps({k: v for k, v in r.items() if k not in ("referencia", "vivo")})) if __name__ == "__main__": main()