python-mpv icon indicating copy to clipboard operation
python-mpv copied to clipboard

Hardware decoding with libmpv

Open shibotto opened this issue 6 months ago • 1 comments

I'm currently trying to use this inside a GTK+ 3 window on X11, but I chose the OpenGL approach because I want it to work on Wayland as well. This means that to embed it I need to use vo=libmpv, which works fine, although I'm having a hard time making vaapi work for hardware decoding. For reference, I'm doing a mix of your gist example and https://github.com/trin94/python-mpv-gtk4.

Digging around libmpv and OpenGL, I found https://www.ccoderun.ca/programming/doxygen/mpv/structmpv__opengl__init__params.html, which says it is fully supported, but needs to be set up. So I digged more and I found mpvqt (used by Haruna) and celluloid which seem to do this. The problem is that they are in C++ and C respectively and I'm not sure how to translate it to Python.

I kinda figured that I need to pass either "x11_display=" or "wl_display=" to MpvRenderContext() (mainly because it doesn't complain about them, to be honest), but then I'm not sure what the "display" argument should be. Trying to copy Celluloid for GTK, I arrived at something like

display = GdkDisplay.get_default()
x11_display = display.get_xdisplay()

but of course I can't just pass that and I'm not so versed in C to really understand what's going on (is it expecting a pointer?). Do you think you can help me out?

shibotto avatar Jul 25 '25 16:07 shibotto

One step at a time. I am able to use vaapi on GTK 4 with X11 adding the following:

gi.require_version("Gdk", "4.0")
from gi.repository import Gdk

...

        display = Gdk.Display.get_default()
        self._ctx = MpvRenderContext(
            ...
            x11_display=hash(display.get_xdisplay())
        )

As far as I understood it's not possible on GTK+ 3 with X11, as it only uses GLX for OpenGL, while MPV's vaapi requires EGL.

I have still no idea how to make it work on Wayland. I quickly tried in weston but Gdk.Display.get_default() returned None.

shibotto avatar Jul 27 '25 19:07 shibotto