e07c9ec77b
Several weeks of work accumulated without being committed. Grouped here for clarity; see CHANGELOG.md [Unreleased] for the user-facing summary. Highlights ---------- P1 #2 — ISO 639-2/B canonical migration - New Language VO + LanguageRegistry (alfred/domain/shared/knowledge/). - iso_languages.yaml as single source of truth for language codes. - SubtitleKnowledgeBase now delegates lookup to LanguageRegistry; subtitles.yaml only declares subtitle-specific tokens (vostfr, vf, vff, …). - SubtitlePreferences default → ["fre", "eng"]; subtitle filenames written as {iso639_2b}.srt (legacy fr.srt still read via alias). - Scanner: dropped _LANG_KEYWORDS / _SDH_TOKENS / _FORCED_TOKENS / SUBTITLE_EXTENSIONS hardcoded dicts. - Fixed: 'hi' token no longer marks SDH (conflicted with Hindi alias). - Added settings.min_movie_size_bytes (was a module constant). P1 #3 — Release parser unification + data-driven tokenizer - parse_release() is now the single source of truth for release-name parsing. - alfred/knowledge/release/separators.yaml declares the token separators used by the tokenizer (., space, [, ], (, ), _). New conventions can be added without code changes. - Tokenizer now splits on any configured separator instead of name.split('.'). Releases like 'The Father (2020) [1080p] [WEBRip] [5.1] [YTS.MX]' parse via the direct path without sanitization fallback. - Site-tag extraction always runs first; well-formedness only rejects truly forbidden chars. - _parse_season_episode() extended with NxNN / NxNNxNN alt forms. - Removed dead helpers: _sanitize, _normalize. Domain cleanup - Deleted fossil services with zero production callers: alfred/domain/movies/services.py alfred/domain/tv_shows/services.py alfred/domain/subtitles/services.py (replaced by subtitles/services/ package) alfred/domain/subtitles/repositories.py - Split monolithic subtitle services into a package (identifier, matcher, placer, pattern_detector, utils) + dedicated knowledge/ package. - MediaInfo split into dedicated package (alfred/domain/shared/media/: audio, video, subtitle, info, matching). Persistence cleanup - Removed dead JSON repositories (movie/subtitle/tvshow_repository.py). Tests - Major expansion of the test suite organized to mirror the source tree. - Removed obsolete *_edge_cases test files superseded by structured tests. - Suite: 990 passed, 8 skipped. Misc - .gitignore: exclude env_backup/ and *.bak. - Adjustments across agent/llm, app.py, application/filesystem, and infrastructure/filesystem to align with the new domain layout.
111 lines
3.2 KiB
Python
111 lines
3.2 KiB
Python
"""ffprobe — infrastructure adapter for extracting MediaInfo from a video file."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import logging
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from alfred.domain.shared.media import AudioTrack, MediaInfo, SubtitleTrack, VideoTrack
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
_FFPROBE_CMD = [
|
|
"ffprobe",
|
|
"-v",
|
|
"quiet",
|
|
"-print_format",
|
|
"json",
|
|
"-show_streams",
|
|
"-show_format",
|
|
]
|
|
|
|
|
|
def probe(path: Path) -> MediaInfo | None:
|
|
"""
|
|
Run ffprobe on path and return a MediaInfo.
|
|
|
|
Returns None if ffprobe is not available or the file cannot be probed.
|
|
"""
|
|
try:
|
|
result = subprocess.run(
|
|
[*_FFPROBE_CMD, str(path)],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=30,
|
|
)
|
|
except subprocess.TimeoutExpired:
|
|
logger.warning("ffprobe timed out on %s", path)
|
|
return None
|
|
|
|
if result.returncode != 0:
|
|
logger.warning("ffprobe failed on %s: %s", path, result.stderr.strip())
|
|
return None
|
|
|
|
try:
|
|
data = json.loads(result.stdout)
|
|
except json.JSONDecodeError:
|
|
logger.warning("ffprobe returned invalid JSON for %s", path)
|
|
return None
|
|
|
|
return _parse(data)
|
|
|
|
|
|
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)
|
|
if "duration" in fmt:
|
|
try:
|
|
info.duration_seconds = float(fmt["duration"])
|
|
except ValueError:
|
|
pass
|
|
if "bit_rate" in fmt:
|
|
try:
|
|
info.bitrate_kbps = int(fmt["bit_rate"]) // 1000
|
|
except ValueError:
|
|
pass
|
|
|
|
for stream in streams:
|
|
codec_type = stream.get("codec_type")
|
|
|
|
if codec_type == "video":
|
|
info.video_tracks.append(
|
|
VideoTrack(
|
|
index=stream.get("index", len(info.video_tracks)),
|
|
codec=stream.get("codec_name"),
|
|
width=stream.get("width"),
|
|
height=stream.get("height"),
|
|
is_default=stream.get("disposition", {}).get("default", 0) == 1,
|
|
)
|
|
)
|
|
|
|
elif codec_type == "audio":
|
|
info.audio_tracks.append(
|
|
AudioTrack(
|
|
index=stream.get("index", len(info.audio_tracks)),
|
|
codec=stream.get("codec_name"),
|
|
channels=stream.get("channels"),
|
|
channel_layout=stream.get("channel_layout"),
|
|
language=stream.get("tags", {}).get("language"),
|
|
is_default=stream.get("disposition", {}).get("default", 0) == 1,
|
|
)
|
|
)
|
|
|
|
elif codec_type == "subtitle":
|
|
info.subtitle_tracks.append(
|
|
SubtitleTrack(
|
|
index=stream.get("index", len(info.subtitle_tracks)),
|
|
codec=stream.get("codec_name"),
|
|
language=stream.get("tags", {}).get("language"),
|
|
is_default=stream.get("disposition", {}).get("default", 0) == 1,
|
|
is_forced=stream.get("disposition", {}).get("forced", 0) == 1,
|
|
)
|
|
)
|
|
|
|
return info
|