(PyQt)如何将VLC实例插入QFrame

时间:2016-10-07 12:19:02

标签: python-3.x pyqt vlc

我试图通过在PyQt小部件(QFrame)中嵌入VLC实例来制作一个简单的视频播放器应用。我发现了一些让我走的例子,但我的代码并不是很有用。当我启动它时,它会播放" test_video.mp4",但它会在自己独立的窗口中启动常规VLC播放器应用程序。当我关闭VLC播放器窗口时,显然视频会停止,但音频会继续播放,直到我关闭自己的Qt(PyQt)窗口。

编辑1:忘记提及我正在使用python-vlc,通过pip下载。

    ### video_player.py

    import sys
    import vlc
    from PyQt4 import QtCore, QtGui
    from video_player_main_window import Ui_MainWindow

    class StartQT4(QtGui.QMainWindow):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
            self.ui = Ui_MainWindow()
            self.ui.setupUi(self)

            self.vlc_instance = vlc.Instance("--no-xlib --sout-all")
            self.mediaplayer = self.vlc_instance.media_player_new()
            self.mediaplayer.set_xwindow(self.ui.video_frame.winId())
            print(self.ui.video_frame.winId())
            self.media_path = "test_video.mp4"
            self.media = self.vlc_instance.media_new(self.media_path)
            self.mediaplayer = self.vlc_instance.media_player_new()
            self.mediaplayer.set_media(self.media)
            self.mediaplayer.play()

    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        myapp = StartQT4()
        myapp.show()
        sys.exit(app.exec_())

我添加了一个" print(self.ui.video_frame.win())"仅用于调试/健全性检查以确保它是合法的值。下面的命令行输出。 " X服务器故障"在我的PyQt窗口仍在运行时关闭VLC窗口后显示。

### command line output

106954771
[00007f9c48055168] vdpau_avcodec generic error: Xlib is required for VDPAU
[00007f9c3c003968] xcb_window window error: X server failure

" video_player_main_window"是QtDesigner(+ pyuic4)生成的模块。 " video_frame"是我尝试将VLC实例放入的QFrame对象的名称。在此处查看video_player_main_window.py的完整代码:http://pastebin.com/cHpAHZN2

1 个答案:

答案 0 :(得分:0)

如果这样的话:

 import sys
 import vlc
 from PyQt4 import QtCore, QtGui
 from video_player_main_window import Ui_MainWindow

 class StartQT4(QtGui.QMainWindow):
     def __init__(self, parent=None):
         QtGui.QWidget.__init__(self, parent)
         self.ui = Ui_MainWindow()
         self.ui.setupUi(self)

         self.vlc_instance = vlc.Instance()
         self.mediaplayer = self.vlc_instance.media_player_new()
         self.mediaplayer.set_hwnd(int(self.frame.winId()))
         self.media_path = "test_video.mp4"
         self.media = self.vlc_instance.media_new(self.media_path)
         self.media.get_mrl()
         self.mediaplayer.set_media(self.media)
         self.mediaplayer.play()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = StartQT4()
    myapp.show()
    sys.exit(app.exec_())

我通常将它用于我的简单播放器。