AI-video-lifeplan-demo/backend/video_processor/schemas.py

91 lines
3.0 KiB
Python

"""
Data schemas for video processing results.
Defines the standard output format that all strategies must produce.
This ensures consistency when integrating with the main pipeline.
"""
from dataclasses import dataclass, field
from typing import Literal
@dataclass
class VideoSegment:
"""Represents a meaningful segment in the video."""
start_time: float # seconds
end_time: float # seconds
description: str # What happens in this segment
transcript: str | None = None # Spoken text (if available)
importance: Literal["high", "medium", "low"] = "medium"
tags: list[str] = field(default_factory=list)
speaker: str | None = None # For multi-speaker videos
@property
def duration(self) -> float:
"""Duration of this segment in seconds."""
return self.end_time - self.start_time
def to_dict(self) -> dict:
"""Convert to JSON-serializable dict."""
return {
"startTime": self.start_time,
"endTime": self.end_time,
"description": self.description,
"transcript": self.transcript,
"importance": self.importance,
"tags": self.tags,
"speaker": self.speaker,
"duration": self.duration,
}
@dataclass
class KeyMoment:
"""Represents a single important moment (quote, visual, emotion)."""
timestamp: float # seconds
type: Literal["quote", "chart", "emotion", "action"]
content: str # Description or transcript
context: str | None = None # Additional context
def to_dict(self) -> dict:
"""Convert to JSON-serializable dict."""
return {
"timestamp": self.timestamp,
"type": self.type,
"content": self.content,
"context": self.context,
}
@dataclass
class VideoAnalysisResult:
"""Complete analysis result for a video."""
source_video: str # Original file path
duration: float # Total video duration in seconds
strategy: Literal["direct", "audio-first"]
segments: list[VideoSegment] = field(default_factory=list)
key_moments: list[KeyMoment] = field(default_factory=list)
metadata: dict = field(default_factory=dict) # Extra info (language, etc.)
def to_dict(self) -> dict:
"""Convert to JSON-serializable dict."""
return {
"sourceVideo": self.source_video,
"duration": self.duration,
"strategy": self.strategy,
"segments": [seg.to_dict() for seg in self.segments],
"keyMoments": [moment.to_dict() for moment in self.key_moments],
"metadata": self.metadata,
}
def get_high_importance_segments(self) -> list[VideoSegment]:
"""Filter segments marked as high importance."""
return [seg for seg in self.segments if seg.importance == "high"]
def get_quotes(self) -> list[KeyMoment]:
"""Get all key moments that are quotes."""
return [moment for moment in self.key_moments if moment.type == "quote"]