add Moss result to db

This commit is contained in:
2026-09-12 20:50:54 +08:00
parent 96d0ff5318
commit ce8b765103
3 changed files with 285 additions and 5 deletions

View File

@@ -4,7 +4,7 @@ import subprocess
import tempfile
import uuid
from pathlib import Path
from typing import List, Optional
from typing import Any, Dict, List, Optional
from .audio_metrics import (
AudioAnalysisError,
@@ -124,7 +124,11 @@ class VideoProcessor:
)
if not document.sentences:
raise RuntimeError("MOSS returned no timestamped speech segments.")
self._finish_processing(document, transcript.text)
self._finish_processing(
document,
transcript.text,
moss_segments=_serialize_moss_segments(transcript),
)
else:
entry, _ = make_entry(media_path, video_hash=video_hash)
document = SentenceBoundaryDocument(
@@ -145,13 +149,18 @@ class VideoProcessor:
self,
document: SentenceBoundaryDocument,
transcription: Optional[str],
moss_segments: Optional[List[Dict[str, Any]]] = None,
) -> None:
if self.oss_uploader is None or not self.oss_uploader.enabled:
self.repository.save_processing_result(document, transcription, ready=True)
self.repository.save_processing_result(
document, transcription, ready=True, moss_segments=moss_segments
)
return
# Keep the course invisible while the mandatory OSS upload is running.
self.repository.save_processing_result(document, transcription, ready=False)
self.repository.save_processing_result(
document, transcription, ready=False, moss_segments=moss_segments
)
video = self.repository.get_video(document.video_hash)
if video is None:
raise ValueError(f"Unknown video: {document.video_hash}")
@@ -539,3 +548,32 @@ def _media_duration_ms(path: Path) -> int:
from .generate_boundaries import media_duration_ms
return media_duration_ms(path)
def _serialize_moss_segments(transcript: Transcript) -> List[Dict[str, Any]]:
rows: List[Dict[str, Any]] = []
for segment in transcript.segments:
text = segment.text.strip()
if not text:
continue
words = None
if segment.words:
words = [
{
"start_seconds": word.start_seconds,
"end_seconds": word.end_seconds,
"text": word.text,
}
for word in segment.words
]
rows.append(
{
"start_seconds": segment.start_seconds,
"end_seconds": segment.end_seconds,
"text": text,
"speaker": segment.speaker,
"words": words,
}
)
return rows