fdf
This commit is contained in:
@@ -14,8 +14,9 @@ import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import time as _time
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional, List, BinaryIO, cast
|
||||
from typing import Any, Dict, Optional, List, BinaryIO, Tuple, cast
|
||||
|
||||
from SYS.logger import debug
|
||||
|
||||
@@ -29,6 +30,44 @@ _LYRIC_PROCESS: Optional[subprocess.Popen] = None
|
||||
_LYRIC_LOG_FH: Optional[Any] = None
|
||||
|
||||
|
||||
_MPV_AVAILABILITY_CACHE: Optional[Tuple[bool, Optional[str]]] = None
|
||||
|
||||
|
||||
def _check_mpv_availability() -> Tuple[bool, Optional[str]]:
|
||||
"""Return (available, reason) for the mpv executable.
|
||||
|
||||
This checks that:
|
||||
- `mpv` is present in PATH
|
||||
- `mpv --version` can run successfully
|
||||
|
||||
Result is cached per-process to avoid repeated subprocess calls.
|
||||
"""
|
||||
global _MPV_AVAILABILITY_CACHE
|
||||
if _MPV_AVAILABILITY_CACHE is not None:
|
||||
return _MPV_AVAILABILITY_CACHE
|
||||
|
||||
mpv_path = shutil.which("mpv")
|
||||
if not mpv_path:
|
||||
_MPV_AVAILABILITY_CACHE = (False, "Executable 'mpv' not found in PATH")
|
||||
return _MPV_AVAILABILITY_CACHE
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[mpv_path, "--version"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=2,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
_MPV_AVAILABILITY_CACHE = (True, None)
|
||||
return _MPV_AVAILABILITY_CACHE
|
||||
_MPV_AVAILABILITY_CACHE = (False, f"MPV returned non-zero exit code: {result.returncode}")
|
||||
return _MPV_AVAILABILITY_CACHE
|
||||
except Exception as exc:
|
||||
_MPV_AVAILABILITY_CACHE = (False, f"Error running MPV: {exc}")
|
||||
return _MPV_AVAILABILITY_CACHE
|
||||
|
||||
|
||||
def _windows_list_lyric_helper_pids(ipc_path: str) -> List[int]:
|
||||
"""Return PIDs of `python -m MPV.lyric --ipc <ipc_path>` helpers (Windows only)."""
|
||||
if platform.system() != "Windows":
|
||||
@@ -130,6 +169,11 @@ class MPV:
|
||||
lua_script_path: Optional[str | Path] = None,
|
||||
timeout: float = 5.0,
|
||||
) -> None:
|
||||
|
||||
ok, reason = _check_mpv_availability()
|
||||
if not ok:
|
||||
raise MPVIPCError(reason or "MPV unavailable")
|
||||
|
||||
self.timeout = timeout
|
||||
self.ipc_path = ipc_path or get_ipc_pipe_path()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user