在Qt中嵌入mpv

安装Qt环境

参考:Qt 使用指南 | Bianbu | 进迭时空开发者社区

安装libmpv

apt install libmpv2 libmpv-dev

example

下载mpv-examples

下载地址:https://github.com/mpv-player/mpv-examples/tree/master/libmpv

修改example

使用硬件解码及GPU:

diff --git a/qtexample.cpp b/qtexample.cpp
index 219d12a..0418235 100755
--- a/qtexample.cpp
+++ b/qtexample.cpp
@@ -85,6 +85,8 @@ MainWindow::MainWindow(QWidget *parent) :
     // Enable keyboard input on the X11 window. For the messy details, see
     // --input-vo-keyboard on the manpage.
     mpv_set_option_string(mpv, "input-vo-keyboard", "yes");
+    mpv_set_option_string(mpv, "vd", "h264_stcodec");    // h264硬解
+    mpv_set_option_string(mpv, "gpu-context", "x11egl");

     // Let us receive property change events with MPV_EVENT_PROPERTY_CHANGE if
     // this property changes.

编译

cd libmpv/qt
qmake
make

配置环境变量

开启Xwayland硬件渲染

注释/etc/environment中XWAYLAND_NO_GLAMOR环境变量,重启生效。

GDK_GL=gles
#XWAYLAND_NO_GLAMOR=1
SDL_VIDEODRIVER=wayland
MESA_LOADER_DRIVER_OVERRIDE=pvr

指定应用程序窗口系统

export QT_QPA_PLATFORM=xcb

运行

./qtexample
2 个赞

可以将

mpv_set_option_string(ctx, "vd", "h264_stcodec");

修改成是 启用硬解框架,让 mpv 自己协商 stcodec 硬解输出格式

mpv_set_option_string(ctx, "hwdec", "stcodec");
mpv_set_option_string(ctx, "hwdec-codecs", "h264,hevc,vp8,vp9,mjpeg,mpeg2video,mpeg4,vc1");

输出的时候会使用到drm_prime

帖子介绍的是x11的方式嵌入,如果要使用wayland平台方式嵌入,可以参考qt_opengl