fixed a bug

This commit is contained in:
2026-08-18 20:54:42 +08:00
parent f5c6717186
commit 1fb3146590
5 changed files with 238 additions and 123 deletions

View File

@@ -9,7 +9,7 @@ from .config import Settings
from .generate_boundaries import ALGORITHM_VERSION, make_entry
from .models import SentenceBoundary, SentenceBoundaryDocument
from .repository import VideoRepository
from .transcription import Transcript, Transcriber, split_segment_by_periods
from .transcription import Transcript, Transcriber, split_sentences_at_punctuation
MOSS_ALGORITHM_VERSION = "moss-period-v2"
@@ -107,30 +107,29 @@ def document_from_transcript(
samples, sample_rate = decode_audio_mono(audio_path)
sentences: List[SentenceBoundary] = []
previous_end = 0
for segment in sorted(transcript.segments, key=lambda item: (item.start_seconds, item.end_seconds)):
for sentence_segment in split_segment_by_periods(segment):
start_ms = max(previous_end, int(round(sentence_segment.start_seconds * 1000)))
end_ms = min(duration_ms, int(round(sentence_segment.end_seconds * 1000)))
if not sentence_segment.text.strip() or end_ms <= start_ms:
continue
start_sample = max(0, int(start_ms / 1000 * sample_rate))
end_sample = min(samples.size, int(end_ms / 1000 * sample_rate))
try:
metrics = analyze_samples(samples[start_sample:end_sample], sample_rate)
speech_duration_ms = metrics.speech_duration_ms
except AudioAnalysisError:
speech_duration_ms = end_ms - start_ms
sentences.append(
SentenceBoundary(
index=len(sentences),
start_ms=start_ms,
end_ms=end_ms,
text=sentence_segment.text.strip(),
language=language,
reference_speech_duration_ms=max(1, speech_duration_ms),
)
for sentence_segment in split_sentences_at_punctuation(transcript.segments):
start_ms = max(previous_end, int(round(sentence_segment.start_seconds * 1000)))
end_ms = min(duration_ms, int(round(sentence_segment.end_seconds * 1000)))
if not sentence_segment.text.strip() or end_ms <= start_ms:
continue
start_sample = max(0, int(start_ms / 1000 * sample_rate))
end_sample = min(samples.size, int(end_ms / 1000 * sample_rate))
try:
metrics = analyze_samples(samples[start_sample:end_sample], sample_rate)
speech_duration_ms = metrics.speech_duration_ms
except AudioAnalysisError:
speech_duration_ms = end_ms - start_ms
sentences.append(
SentenceBoundary(
index=len(sentences),
start_ms=start_ms,
end_ms=end_ms,
text=sentence_segment.text.strip(),
language=language,
reference_speech_duration_ms=max(1, speech_duration_ms),
)
previous_end = end_ms
)
previous_end = end_ms
return SentenceBoundaryDocument(
video_hash=video_hash,
duration_ms=duration_ms,