如何将 VLC 实例插入 QFrame [英] How to insert VLC instance into a QFrame
问题描述
我正在尝试通过在 PyQt 小部件(QFrame)中嵌入 VLC 实例来制作一个简单的视频播放器应用程序.我找到了一些可以帮助我的示例,但是我的代码不太好用.当我启动它时,它会播放test_video.mp4",但它会在自己的单独窗口中启动常规 VLC 播放器应用程序.当我关闭 VLC 播放器窗口时,显然视频停止了,但音频继续播放,直到我关闭我自己的 Qt (PyQt) 窗口.
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.
编辑 1:忘记提及我使用的是 python-vlc,通过 pip 下载.
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_())
我添加了一个print(self.ui.video_frame.win())"只是为了调试/健全性检查以确保这是一个合法的值.命令行输出如下.在我的 PyQt 窗口仍在运行时关闭 VLC 窗口后出现X 服务器故障".
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
video_player_main_window"是QtDesigner (+ pyuic4) 生成的模块.video_frame"是我试图将 VLC 实例放入的 QFrame 对象的名称.在此处查看 video_player_main_window.py 的完整代码:http://pastebin.com/cHpAHZN2
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
推荐答案
如果这样 :
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.
这篇关于如何将 VLC 实例插入 QFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!