Files
SageTube/ytsage/core/ytsage_mpv.py
T
Jaroslav Beneš f886125857 Add embedded mpv player (render API + QOpenGLWidget)
New ytsage/gui/ytsage_gui_player.py:
- MpvRenderWidget hosts libmpv through MpvRenderContext in a
  QOpenGLWidget - works on native Wayland where wid-embedding cannot -
  with all mpv-thread callbacks marshalled to the GUI thread via queued
  signals only.
- PlayerPanel adds transport controls: play/pause, seek slider, time
  display, quality selector (caps ytdl-format height and reloads in
  place), speed, volume (persisted), subtitle toggle, fullscreen
  (reparent to top-level window), and Space/F/Escape keys.
- Playback resolves watch URLs through mpv's ytdl_hook pointed at the
  app-managed SHA256-verified yt-dlp binary (script-opts
  ytdl_hook-ytdl_path), inheriting cookies and proxy settings via
  ytdl-raw-options - stream freshness, DASH muxing and nsig handling
  stay in yt-dlp's hands.

New ytsage/core/ytsage_mpv.py probes libmpv availability; without it
the Watch UI shows a per-OS install hint and everything else works.
python-mpv added to dependencies (libmpv itself is a system package).
Config gains player.* and feed.* defaults.

Verified on Wayland: real YouTube video streams with position/duration
signals flowing and no thread-safety errors.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-25 01:44:25 +02:00

39 lines
1.3 KiB
Python

"""
libmpv availability probe
=========================
python-mpv is a ctypes binding: importing it raises OSError when the libmpv
shared library is missing from the system. All player code must import mpv
through probe_player() so the rest of the app (search, browse, downloads)
keeps working without libmpv installed.
"""
from typing import Optional, Tuple
from ..utils.ytsage_constants import OS_NAME
from ..utils.ytsage_logger import logger
_probe_result: Optional[Tuple[bool, str]] = None
def probe_player() -> Tuple[bool, str]:
"""Return (available, hint). hint explains how to install libmpv when absent."""
global _probe_result
if _probe_result is not None:
return _probe_result
try:
import mpv # noqa: F401
_probe_result = (True, "")
except (ImportError, OSError, AttributeError) as e:
logger.warning(f"libmpv unavailable, watch features disabled: {e}")
if OS_NAME == "Windows":
hint = "Place libmpv-2.dll next to the application, or install mpv and add it to PATH."
elif OS_NAME == "Darwin":
hint = "Install mpv with Homebrew: brew install mpv"
else:
hint = "Install mpv with your package manager, e.g. sudo pacman -S mpv or sudo apt install libmpv2"
_probe_result = (False, hint)
return _probe_result