How to insert VLC instance into a QFrame

痞子三分冷 提交于 2020-03-01 02:04:48

问题


I'm trying to make a simple video player app by embedding a VLC instance inside a PyQt widget (a QFrame). I found a few examples that go me going, but my code doesn't quite work. When I launch it, it plays "test_video.mp4", but it launches the regular VLC player app in its own, separate window. When I close out of the VLC player window, obviously the video stops, but the audio continues playing until I close my own Qt (PyQt) window.

edit 1: Forgot to mention I am using python-vlc, downloaded via 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_())

I added a "print(self.ui.video_frame.win())" just for debugging / sanity check to make sure that was a legitimate value. Command line output below. The "X server failure" shows up after I close the VLC window while my PyQt window is still running.

### command line output

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

The "video_player_main_window" is the module that QtDesigner (+ pyuic4) generates. "video_frame" is the name of the QFrame object I'm trying to put the VLC instance into. See full code for video_player_main_window.py here: http://pastebin.com/cHpAHZN2


回答1:


how if like this :

 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_())

i usualy use this for my simple player.



来源:https://stackoverflow.com/questions/39917150/how-to-insert-vlc-instance-into-a-qframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!