scrub optimization

This commit is contained in:
2026-04-03 06:40:08 -03:00
parent 9dfa252727
commit 84dc1405dc
13 changed files with 813 additions and 68 deletions

View File

@@ -6,6 +6,7 @@ from threading import Thread
from gi.repository import GLib
from cht.config import TRANSCRIBE_MIN_CHUNK_S
from cht.session import rebuild_manifest
from cht.stream.manager import StreamManager
from cht.stream.tracker import RecordingTracker
@@ -84,11 +85,19 @@ class StreamLifecycle:
self._tracker = None
readonly = self._stream_mgr.readonly if self._stream_mgr else True
session_dir = self._stream_mgr.session_dir if self._stream_mgr else None
if self._stream_mgr:
if not readonly:
self._stream_mgr.stop_all()
self._stream_mgr = None
# Rebuild manifest now that all segments are finalized
if session_dir and not readonly:
try:
rebuild_manifest(session_dir)
except Exception as e:
log.error("Failed to rebuild manifest on stop: %s", e)
self._streaming = False
self._gone_live = False
self._pending_transcript_audio.clear()
@@ -129,40 +138,51 @@ class StreamLifecycle:
GLib.idle_add(self._on_scene_marker, f["timestamp"])
self._on_new_frames(frames)
def _handle_new_audio(self, wav_path, start_time, duration):
def _handle_new_audio(self, wav_path, start_time, duration,
segment_path=None, local_start=None):
if not self._stream_mgr:
return
# start_time is global; waveform uses global time
self._waveform_engine.append_chunk(wav_path, start_time)
peaks = self._waveform_engine.peaks
bucket_dur = self._waveform_engine.bucket_duration
GLib.idle_add(self._on_waveform_update, peaks.copy(), bucket_dur)
self._pending_transcript_audio.append((wav_path, start_time, duration))
self._pending_transcript_audio.append({
"wav": wav_path, "global_start": start_time, "duration": duration,
"segment_path": segment_path or self._stream_mgr.recording_path,
"local_start": local_start if local_start is not None else start_time,
})
self._pending_transcript_duration += duration
if self._pending_transcript_duration < TRANSCRIBE_MIN_CHUNK_S:
return
first_start = self._pending_transcript_audio[0][1]
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_start):06d}.wav"
chunk_wav = mgr.audio_dir / f"transcript_{int(first_global):06d}.wav"
def _transcribe():
from cht.stream import ffmpeg as ff
try:
# Extract audio using local time within the segment file
ff.extract_audio_chunk(
mgr.recording_path, chunk_wav,
start_time=first_start, duration=total_dur,
seg_path, chunk_wav,
start_time=first_local, duration=total_dur,
)
except Exception as e:
log.error("Transcript audio extraction failed: %s", e)
return
if not chunk_wav.exists():
return
new_segs = self._transcriber.transcribe_chunk(chunk_wav, time_offset=first_start)
# Transcribe with global time offset so segment timestamps are global
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)