db/report/server/UI updates

This commit is contained in:
Stefano Zini
2026-11-04 14:14:26 +01:00
parent a741859140
commit c1fbcc1688
6 changed files with 52 additions and 17 deletions
+27 -8
View File
@@ -29,7 +29,7 @@ NOPROXY = urllib.request.build_opener(urllib.request.ProxyHandler({}))
EDGE = Path(os.path.expandvars(r"%ProgramFiles(x86)%\Microsoft\Edge\Application\msedge.exe"))
PRINT_CSS = "@media print{body{max-width:none;padding:0}img,tr{break-inside:avoid}h2,h3{break-after:avoid}}"
LOCK = threading.Lock()
STEPS = ["audio", "transcribe", "diarize", "speakers", "shots", "docs", "report", "memory", "render"]
STEPS = ["audio", "transcribe", "diarize", "speakers", "shots", "docs", "report", "memory", "render", "title"]
QUEUE = [] # (name, start_from, speakers) waiting for the single runner, oldest first
BATCH = None # runner thread, None when idle
QLOCK = threading.Lock()
@@ -466,6 +466,14 @@ class H(BaseHTTPRequestHandler):
else: self._send(b"not found", 404)
def video(self, name):
a, v = db.media(name); f = v or a
if f and f.suffix == ".webm" and name not in R:
# MediaRecorder webm has no duration/cues: the player treats it as an endless stream. Cache a stream-copy remux (header with length + seek index).
pv = f.with_name("preview.webm")
if not pv.exists() or pv.stat().st_mtime < f.stat().st_mtime:
t = pv.with_suffix(".part.webm")
if subprocess.run(["ffmpeg", "-y", "-v", "error", "-i", str(f), "-c", "copy", str(t)], capture_output=True).returncode == 0: t.replace(pv)
else: t.unlink(missing_ok=True)
if pv.exists(): f = pv
if not f: return self._send(b"not found", 404)
size = f.stat().st_size; lo, hi = 0, size - 1
m = re.match(r"bytes=(\d*)-(\d*)$", self.headers.get("Range", ""))
@@ -491,7 +499,10 @@ class H(BaseHTTPRequestHandler):
try: s, e = float(q["start"]), float(q["end"])
except (KeyError, ValueError): return self._send(b"bad times", 400)
if not 0 <= s < e: return self._send("intervallo non valido".encode(), 400)
new = "meet_" + time.strftime("%Y-%m-%dT%H-%M-%S"); ext = ".mp4" if v else ".m4a"
inplace = q.get("inplace") == "1" # replace this meeting's source (and shift its call events) instead of creating a new meeting
j = JOBS.get(name)
if inplace and ((j and j.poll() is None) or any(x[0] == name for x in QUEUE)): return self._send("elaborazione in corso: ferma prima".encode(), 409)
new = name if inplace else "meet_" + time.strftime("%Y-%m-%dT%H-%M-%S"); ext = ".mp4" if v else ".m4a"
dst = db.out_dir(new) / f"source{ext}"; tmp = dst.with_name(f"source{ext}.part")
enc = ["-vf", "scale=trunc(iw/2)*2:trunc(ih/2)*2", "-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-pix_fmt", "yuv420p"] if v else ["-vn"]
CROP[name] = 0
@@ -501,13 +512,21 @@ class H(BaseHTTPRequestHandler):
if ln.startswith("out_time_us=") and ln[12:].strip().lstrip("-").isdigit(): CROP[name] = min(99, max(0, int(ln[12:]) / 1e4 / (e - s)))
err = p.stderr.read(); p.wait(); CROP.pop(name, None)
r = type("R", (), {"returncode": p.returncode, "stderr": err})
if r.returncode or not tmp.exists() or not tmp.stat().st_size:
tmp.unlink(missing_ok=True); dst.parent.rmdir(); return self._send(f"ffmpeg: {r.stderr[-300:]}".encode(), 500)
fail = lambda msg: (tmp.unlink(missing_ok=True), None if inplace else dst.parent.rmdir(), self._send(msg.encode(), 500))
if r.returncode or not tmp.exists() or not tmp.stat().st_size: return fail(f"ffmpeg: {r.stderr[-300:]}")
try: dur = db.duration(tmp)
except ValueError as x: return fail(f"ritaglio non leggibile: {x}")
if inplace:
for f in (a, v, dst.with_name("preview.webm")):
if f and f != dst: f.unlink(missing_ok=True)
c = db.connect()
c.execute("DELETE FROM call_events WHERE name=? AND (t<? OR t>?)", (name, s, e))
c.execute("UPDATE call_events SET t=t-? WHERE name=?", (s, name))
tmp.replace(dst)
try: dur = db.duration(dst)
except ValueError as x: dst.unlink(); dst.parent.rmdir(); return self._send(f"ritaglio non leggibile: {x}".encode(), 500)
db.set_meta(new, upload=f"{title(name) or name} [{int(s)}s-{int(e)}s]")
db.upsert_recording(new, source="upload", started_at=now(), status="queued", bytes=dst.stat().st_size, duration_s=dur)
if inplace: db.upsert_recording(name, bytes=dst.stat().st_size, duration_s=dur, status="queued")
else:
db.set_meta(new, upload=f"{title(name) or name} [{int(s)}s-{int(e)}s]")
db.upsert_recording(new, source="upload", started_at=now(), status="queued", bytes=dst.stat().st_size, duration_s=dur)
launch(new, "audio")
self._json({"name": new, "duration_s": dur})
def upload(self, q):