如何在PyQt5的视频上绘制/动画透明图像/对象?

时间:2020-07-07 20:52:52

标签: python pyqt pyqt5 vlc qframe

我使用python-vlcPyQt5创建了一个媒体播放器应用程序,与this qtvlc code非常相似。 最小的可复制代码如下所示:

class Player(QMainWindow):
    """A simple Media Player using VLC and Qt
    """
    def __init__(self, master=None):
        QMainWindow.__init__(self, master)
        self.setWindowTitle("Media Player")

        # creating a basic vlc instance
        self.instance = vlc.Instance()
        # creating an empty vlc media player
        self.mediaplayer = self.instance.media_player_new()

        self.createUI()
        self.isPaused = False

    def createUI(self):
        """Set up the user interface, signals & slots
        """
        self.widget = QWidget(self)
        self.setCentralWidget(self.widget)

        self.videoframe = QFrame()
        self.videoframe.setAutoFillBackground(True)

        pixmap = QtGui.QPixmap()
        pixmap.load("path to transparent_image.png")
        self.my_label = QLabel(self)
        self.my_label.setPixmap(pixmap)
        self.my_label.setGeometry(300, 250, 100, 100)
        self.my_label.setFrameStyle(QFrame.NoFrame)
        self.my_label.setAttribute(Qt.WA_TranslucentBackground)

        self.vboxlayout = QVBoxLayout()
        self.vboxlayout.addWidget(self.videoframe)

        self.widget.setLayout(self.vboxlayout)

        self.media = self.instance.media_new("path to video file")
        # put the media in the media player
        self.mediaplayer.set_media(self.media)

        # the media player has to be 'connected' to the QFrame
        # (otherwise a video would be displayed in it's own window)
        # this is platform specific!
        # you have to give the id of the QFrame (or similar object) to
        # vlc, different platforms have different functions for this
        if sys.platform.startswith('linux'): # for Linux using the X Server
            self.mediaplayer.set_xwindow(self.videoframe.winId())
        elif sys.platform == "win32": # for Windows
            self.mediaplayer.set_hwnd(self.videoframe.winId())
        elif sys.platform == "darwin": # for MacOS
            self.mediaplayer.set_nsobject(self.videoframe.winId())
        self.mediaplayer.play()

    
if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = Player()
    player.show()
    player.resize(640, 480)
    sys.exit(app.exec_())

运行代码后,我可以看到透明图像绘制在视频顶部,但是该图像而不是通过它显示背景视频,而是继续显示QFrame的一部分作为背景。谁能帮助我解决这个问题?

0 个答案:

没有答案