【发布时间】:2021-07-11 20:06:22
【问题描述】:
在意识到几乎不可能在 MPV (Possible to allow key bindings with MPV C API when no video (GUI) is being shown?) 中找到有关使用 C 插件的键绑定方面的帮助后,我决定学习一些 Lua 来帮助解决这个问题。问题是,文档对如何使用 C 插件 添加 Lua 脚本 不是很清楚,我发现应该在初始化之前调用 check_error(mpv_set_option_string(ctx, "load-scripts", "yes")); mpv 在 C 插件中,它指出应该有一种添加脚本的方法...在终端中加载脚本时,您可以执行 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));,但都没有奏效......
如何从 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;
}
【问题讨论】:
-
使用命令
"load_script"代替"scripts"有效吗? -
不,很遗憾不是:/它说'mpv API错误:找不到选项'