92 lines
2.2 KiB
Python
92 lines
2.2 KiB
Python
"""Subtitle domain value objects."""
|
|
|
|
from dataclasses import dataclass
|
|
from enum import Enum
|
|
|
|
from ..shared.exceptions import ValidationError
|
|
|
|
|
|
class Language(Enum):
|
|
"""Supported subtitle languages."""
|
|
|
|
ENGLISH = "en"
|
|
FRENCH = "fr"
|
|
|
|
@classmethod
|
|
def from_code(cls, code: str) -> "Language":
|
|
"""
|
|
Get language from ISO 639-1 code.
|
|
|
|
Args:
|
|
code: Two-letter language code
|
|
|
|
Returns:
|
|
Language enum value
|
|
|
|
Raises:
|
|
ValidationError: If code is not supported
|
|
"""
|
|
code_lower = code.lower()
|
|
for lang in cls:
|
|
if lang.value == code_lower:
|
|
return lang
|
|
raise ValidationError(f"Unsupported language code: {code}")
|
|
|
|
|
|
class SubtitleFormat(Enum):
|
|
"""Supported subtitle formats."""
|
|
|
|
SRT = "srt" # SubRip
|
|
ASS = "ass" # Advanced SubStation Alpha
|
|
SSA = "ssa" # SubStation Alpha
|
|
VTT = "vtt" # WebVTT
|
|
SUB = "sub" # MicroDVD
|
|
|
|
@classmethod
|
|
def from_extension(cls, extension: str) -> "SubtitleFormat":
|
|
"""
|
|
Get format from file extension.
|
|
|
|
Args:
|
|
extension: File extension (with or without dot)
|
|
|
|
Returns:
|
|
SubtitleFormat enum value
|
|
|
|
Raises:
|
|
ValidationError: If extension is not supported
|
|
"""
|
|
ext = extension.lower().lstrip(".")
|
|
for fmt in cls:
|
|
if fmt.value == ext:
|
|
return fmt
|
|
raise ValidationError(f"Unsupported subtitle format: {extension}")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class TimingOffset:
|
|
"""
|
|
Value object representing subtitle timing offset in milliseconds.
|
|
|
|
Used for synchronizing subtitles with video.
|
|
"""
|
|
|
|
milliseconds: int
|
|
|
|
def __post_init__(self):
|
|
"""Validate timing offset."""
|
|
if not isinstance(self.milliseconds, int):
|
|
raise ValidationError(
|
|
f"Timing offset must be an integer, got {type(self.milliseconds)}"
|
|
)
|
|
|
|
def to_seconds(self) -> float:
|
|
"""Convert to seconds."""
|
|
return self.milliseconds / 1000.0
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.milliseconds}ms"
|
|
|
|
def __repr__(self) -> str:
|
|
return f"TimingOffset({self.milliseconds})"
|