refactor(domain): freeze media track value objects
AudioTrack, VideoTrack, SubtitleTrack and MediaInfo are snapshots of a single ffprobe run — model them as proper immutable value objects. - @dataclass(frozen=True) on all four - MediaInfo track collections become tuple[...] instead of list[...] - ffprobe adapter rewritten to build tuples up-front instead of appending/setattr'ing on a constructed instance
This commit is contained in:
@@ -57,27 +57,31 @@ def _parse(data: dict) -> MediaInfo:
|
||||
streams = data.get("streams", [])
|
||||
fmt = data.get("format", {})
|
||||
|
||||
info = MediaInfo()
|
||||
|
||||
# File-level duration/bitrate (ffprobe ``format`` block — independent of streams)
|
||||
duration_seconds: float | None = None
|
||||
bitrate_kbps: int | None = None
|
||||
if "duration" in fmt:
|
||||
try:
|
||||
info.duration_seconds = float(fmt["duration"])
|
||||
duration_seconds = float(fmt["duration"])
|
||||
except ValueError:
|
||||
pass
|
||||
if "bit_rate" in fmt:
|
||||
try:
|
||||
info.bitrate_kbps = int(fmt["bit_rate"]) // 1000
|
||||
bitrate_kbps = int(fmt["bit_rate"]) // 1000
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
video_tracks: list[VideoTrack] = []
|
||||
audio_tracks: list[AudioTrack] = []
|
||||
subtitle_tracks: list[SubtitleTrack] = []
|
||||
|
||||
for stream in streams:
|
||||
codec_type = stream.get("codec_type")
|
||||
|
||||
if codec_type == "video":
|
||||
info.video_tracks.append(
|
||||
video_tracks.append(
|
||||
VideoTrack(
|
||||
index=stream.get("index", len(info.video_tracks)),
|
||||
index=stream.get("index", len(video_tracks)),
|
||||
codec=stream.get("codec_name"),
|
||||
width=stream.get("width"),
|
||||
height=stream.get("height"),
|
||||
@@ -86,9 +90,9 @@ def _parse(data: dict) -> MediaInfo:
|
||||
)
|
||||
|
||||
elif codec_type == "audio":
|
||||
info.audio_tracks.append(
|
||||
audio_tracks.append(
|
||||
AudioTrack(
|
||||
index=stream.get("index", len(info.audio_tracks)),
|
||||
index=stream.get("index", len(audio_tracks)),
|
||||
codec=stream.get("codec_name"),
|
||||
channels=stream.get("channels"),
|
||||
channel_layout=stream.get("channel_layout"),
|
||||
@@ -98,9 +102,9 @@ def _parse(data: dict) -> MediaInfo:
|
||||
)
|
||||
|
||||
elif codec_type == "subtitle":
|
||||
info.subtitle_tracks.append(
|
||||
subtitle_tracks.append(
|
||||
SubtitleTrack(
|
||||
index=stream.get("index", len(info.subtitle_tracks)),
|
||||
index=stream.get("index", len(subtitle_tracks)),
|
||||
codec=stream.get("codec_name"),
|
||||
language=stream.get("tags", {}).get("language"),
|
||||
is_default=stream.get("disposition", {}).get("default", 0) == 1,
|
||||
@@ -108,4 +112,10 @@ def _parse(data: dict) -> MediaInfo:
|
||||
)
|
||||
)
|
||||
|
||||
return info
|
||||
return MediaInfo(
|
||||
video_tracks=tuple(video_tracks),
|
||||
audio_tracks=tuple(audio_tracks),
|
||||
subtitle_tracks=tuple(subtitle_tracks),
|
||||
duration_seconds=duration_seconds,
|
||||
bitrate_kbps=bitrate_kbps,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user