3

在意识到几乎不可能找到有关在 MPV 中使用 C 插件的键绑定的帮助(可能在没有显示视频 (GUI) 时允许使用 MPV C API 进行键绑定?),我决定学习一些 Lua 来帮助与原因。问题是,文档对如何使用C 插件添加Lua 脚本不是很清楚,我发现应该在 C 插件中初始化 mpv 之前调用它,这指出应该有一种方法可以添加脚本...在终端中加载脚本时,您可以执行此操作,这将从$HOME/.config/mpv内部调用脚本...如何在 MPV 的 C 插件中调用 Lua 脚本?我尝试了一些东西,包括和check_error(mpv_set_option_string(ctx, "load-scripts", "yes"));mpv video.mp4 --scripts="script_name.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));,这些都不起作用...

如何从 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;
}
4

1 回答 1

3

在玩了更多mpv_set_property_string命令之后,我发现你必须指定 Lua 文件的完整路径,默认情况下它会在正在播放的目录中搜索文件,所以如果我尝试在 /home/ 中播放它它会搜索/home/test.lua,所以为了让它工作我必须做check_error(mpv_set_property_string(ctx, "scripts", "/home/netsu/.config/mpv/test.lua"));(给出绝对路径)

于 2021-04-17T12:35:54.733 回答