Search code examples
pythonpyqt5pyside6mpv

How to draw button or widget upon QWidget using python-MPV on windows?


I use this library https://github.com/jaseg/python-mpv and PySide6 to embed MPV in container (QWidget) in python. I want to draw the button (or something else maybe qlabel for example?) over QWidget (embedded mpv) on top of player but how can I do that? I tried to draw button but when mpv starts playing player overrides the button with itself. How to place button on top of player?

Update:

I read here that I should use opengl as render api.:https://github.com/mpv-player/mpv-examples/blob/master/libmpv/README.md

Update 2: Maybe i need to use qml?

How can I use opengl along with pyside6 (or pyqt5) and mpv to render properly?

My code there:

import os

from PySide6 import QtWidgets


os.environ['PATH'] += os.path.dirname(__file__) #you need to place mpv-2.dll (or mpv-1.dll) in folder with project
import mpv
import sys

from PySide6.QtWidgets import *
from PySide6.QtCore import *

class Ui_Form(object):
    def setupUi(self, Form):
        if not Form.objectName():
            Form.setObjectName(u"Form")
        Form.resize(780, 477)
        self.pushButton = QPushButton(Form)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(30, 430, 75, 24))

        self.retranslateUi(Form)

        QMetaObject.connectSlotsByName(Form)
    # setupUi

    def retranslateUi(self, Form):
        Form.setWindowTitle(QCoreApplication.translate("Form", u"Form", None))
        self.pushButton.setText(QCoreApplication.translate("Form", u"PushButton", None))
    # retranslateUi

class PlayerWidget(QtWidgets.QWidget, Ui_Form):
    def __init__(self, parent=None):
        super(PlayerWidget, self).__init__(parent)
        self.setupUi(self)
        self.retranslateUi(self)


class Test(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.container = PlayerWidget()
        self.setCentralWidget(self.container)
        self.container.setAttribute(Qt.WA_DontCreateNativeAncestors)
        self.container.setAttribute(Qt.WA_NativeWindow)
        player = mpv.MPV(wid=str(int(self.container.winId())),
                vo='gpu',
                log_handler=print,
                loglevel='debug', ytdl=True)
        player.play('test.mp4')  # place your test video in folder
app = QApplication(sys.argv)

# This is necessary since PyQT stomps over the locale settings needed by libmpv.
# This needs to happen after importing PyQT before creating the first mpv.MPV instance.
import locale
locale.setlocale(locale.LC_NUMERIC, 'C')
win = Test()
win.show()
win.resize(1280, 720)
sys.exit(app.exec_())

Solution

  • As far as I know this is not possible if you use the window id embedding method.

    In order to draw over the video you would need to use the render api. An example with qml exists: https://github.com/jaseg/python-mpv#using-opengl-from-pyqt5qml

    It should also be possible to achieve this using QtWidgets (and not Qml).