如何使用 C 插件加载 MPV Lua 脚本?

How do you load MPV Lua scripts with the C Plugin?

在意识到几乎不可能找到有关让键绑定与 MPV 中的 C 插件一起工作的帮助 (Possible to allow key bindings with MPV C API when no video (GUI) is being shown?) 后,我决定学习一些 Lua 来帮助解决这个问题。问题是,关于如何使用 C 插件 添加 Lua 脚本 的文档不是很清楚,我能找到什么是 check_error(mpv_set_option_string(ctx, "load-scripts", "yes")); 应该在 C 插件中初始化 mpv 之前调用,它指出应该有一种添加脚本的方法...在终端中加载脚本时,您可以执行 mpv video.mp4 --scripts="script_name.lua" 并且将从$HOME/.config/mpv...内部调用脚本...如何在MPV的C插件中实现对Lua脚本的调用?我尝试了一些东西,包括 check_error(mpv_set_option_string(ctx, "scripts", "test.lua"));check_error(mpv_set_property_string(ctx, "scripts", "test.lua"));const char *cmd2[] = {"scripts", "test.lua", NULL}; check_error(mpv_command(ctx, cmd2));,其中 none 有效...

如何从 C 插件为 MPV 调用 Lua 脚本?

下面是我用来测试的代码:

// Build with: g++ main.cpp -o output `pkg-config --libs --cflags mpv`

#include <iostream>
#include <mpv/client.h>

static inline void check_error(int status)
{
    if (status < 0)
    {
        std::cout << "mpv API error: " << mpv_error_string(status) << std::endl;
        exit(1);
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "pass a single media file as argument" << std::endl;

        return 1;
    }

    mpv_handle *ctx = mpv_create();
    if (!ctx)
    {
        std::cout << "failed creating context" << std::endl;
        return 1;
    }

    // Enable default key bindings, so the user can actually interact with
    // the player (and e.g. close the window).
    check_error(mpv_set_option_string(ctx, "input-default-bindings", "yes"));
    mpv_set_option_string(ctx, "input-vo-keyboard", "yes");
    check_error(mpv_set_option_string(ctx, "load-scripts", "yes"));
    check_error(mpv_set_option_string(ctx, "scripts", "test.lua")); // DOES NOT WORK :(
    int val = 1;
    check_error(mpv_set_option(ctx, "osc", MPV_FORMAT_FLAG, &val));

    // Done setting up options.
    check_error(mpv_initialize(ctx));

    // Play the file passed in as a parameter when executing program.
    const char *cmd[] = {"loadfile", argv[1], NULL};
    check_error(mpv_command(ctx, cmd));

    // check_error(mpv_set_option_string(ctx, "scripts", "test.lua"));
    check_error(mpv_set_option_string(ctx, "shuffle", "yes")); // shuffle videos
    check_error(mpv_set_option_string(ctx, "loop-playlist", "yes")); // loop playlists
    // check_error(mpv_set_option_string(ctx, "aspect", "0:0")); // set aspect
    
    // Let it play, and wait until the user quits.
    while (1)
    {
        mpv_event *event = mpv_wait_event(ctx, 10000);
        std::cout << "event: " << mpv_event_name(event->event_id) << std::endl;

        if (event->event_id == MPV_EVENT_SHUTDOWN)
            break;
    }

    mpv_terminate_destroy(ctx);
    return 0;
}

在使用 mpv_set_property_string 命令进行更多尝试后,我发现您必须指定 Lua 文件的 FULL 路径,默认情况下在正在播放的目录中搜索文件,因此如果我尝试在 /home/ 中播放它,它将搜索 /home/test.lua,因此要使其正常工作,我必须这样做check_error(mpv_set_property_string(ctx, "scripts", "/home/netsu/.config/mpv/test.lua"));(给出绝对路径)