not sure about this commit

This commit is contained in:
2026-04-03 08:21:07 -03:00
parent 51c0bdd2da
commit 908231f98a
5 changed files with 122 additions and 109 deletions

View File

@@ -5,7 +5,7 @@ from threading import Thread
from gi.repository import GLib
from cht.config import TRANSCRIBE_MIN_CHUNK_S
from cht.config import TRANSCRIBE_MIN_CHUNK_S, SEGMENT_DURATION
from cht.session import rebuild_manifest
from cht.stream.manager import StreamManager
from cht.stream.tracker import RecordingTracker
@@ -191,18 +191,66 @@ class StreamLifecycle:
Thread(target=_transcribe, daemon=True, name="transcriber").start()
def _flush_pending_transcript(self):
"""Force-transcribe any buffered audio (called before segment rotation)."""
if not self._pending_transcript_audio or not self._stream_mgr:
return
first = self._pending_transcript_audio[0]
first_global = first["global_start"]
first_local = first["local_start"]
seg_path = first["segment_path"]
total_dur = self._pending_transcript_duration
self._pending_transcript_audio.clear()
self._pending_transcript_duration = 0.0
mgr = self._stream_mgr
chunk_wav = mgr.audio_dir / f"transcript_{int(first_global):06d}.wav"
def _transcribe():
from cht.stream import ffmpeg as ff
try:
ff.extract_audio_chunk(
seg_path, chunk_wav,
start_time=first_local, duration=total_dur,
)
except Exception as e:
log.error("Flush transcript failed: %s", e)
return
if not chunk_wav.exists():
return
new_segs = self._transcriber.transcribe_chunk(chunk_wav, time_offset=first_global)
self._transcriber.save_index(mgr.transcript_dir / "index.json")
if new_segs:
GLib.idle_add(self._on_transcript_ready, new_segs)
Thread(target=_transcribe, daemon=True, name="transcriber_flush").start()
def _check_recorder(self):
if not self._streaming or not self._stream_mgr:
return False
if not self._stream_mgr.recorder_alive():
log.warning("Recorder died — restarting into new segment")
self._stream_mgr.restart_recorder()
self._on_recorder_restarted(self._stream_mgr.recording_path)
# Rebuild manifest with the newly completed segment
try:
rebuild_manifest(self._stream_mgr.session_dir)
except Exception as e:
log.error("Manifest rebuild failed: %s", e)
if self._on_manifest_updated:
GLib.idle_add(self._on_manifest_updated)
self._rotate_segment()
return True
# Proactive rotation: cut segment when it exceeds SEGMENT_DURATION
if SEGMENT_DURATION > 0:
dur = self._stream_mgr._estimate_safe_duration()
if dur and dur >= SEGMENT_DURATION:
log.info("Segment reached %.0fs — rotating", dur)
self._rotate_segment()
return True
def _rotate_segment(self):
"""Restart recorder into a new segment and update manifest."""
# Flush pending transcript buffer from the old segment
if self._pending_transcript_audio and self._pending_transcript_duration > 0:
self._flush_pending_transcript()
self._stream_mgr.restart_recorder()
self._on_recorder_restarted(self._stream_mgr.recording_path)
try:
rebuild_manifest(self._stream_mgr.session_dir)
except Exception as e:
log.error("Manifest rebuild failed: %s", e)
if self._on_manifest_updated:
GLib.idle_add(self._on_manifest_updated)