change sentence cut method

This commit is contained in:
2026-08-18 20:34:58 +08:00
parent a36e776343
commit f5c6717186
7 changed files with 440 additions and 29 deletions

View File

@@ -9,10 +9,10 @@ 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
from .transcription import Transcript, Transcriber, split_segment_by_periods
MOSS_ALGORITHM_VERSION = "moss-timestamp-v1"
MOSS_ALGORITHM_VERSION = "moss-period-v2"
class VideoProcessor:
@@ -108,28 +108,29 @@ def document_from_transcript(
sentences: List[SentenceBoundary] = []
previous_end = 0
for segment in sorted(transcript.segments, key=lambda item: (item.start_seconds, item.end_seconds)):
start_ms = max(previous_end, int(round(segment.start_seconds * 1000)))
end_ms = min(duration_ms, int(round(segment.end_seconds * 1000)))
if not 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=segment.text.strip(),
language=language,
reference_speech_duration_ms=max(1, speech_duration_ms),
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),
)
)
)
previous_end = end_ms
previous_end = end_ms
return SentenceBoundaryDocument(
video_hash=video_hash,
duration_ms=duration_ms,