在意识到在MPV (Possible to allow key bindings with MPV C API when no video (GUI) is being shown?)中使用C插件时几乎不可能找到帮助,我决定学习一些Lua来帮助解决这个问题。问题是,对于如何在C插件中添加Lua脚本,文档并不十分清楚,我发现在C插件中初始化mpv之前应该调用check_error(mpv_set_option_string(ctx, "load-scripts", "yes"));,这就指出应该有一种添加脚本的方法.在终端中加载脚本时,可以执行$HOME/.config/mpv...,这将从mpv video.mp4 --scripts="script_name.lua"内部调用脚本如何在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;
}发布于 2021-04-17 12:35:54
在更多地使用mpv_set_property_string命令之后,我发现您必须为Lua文件指定完整的路径,默认情况下,它将搜索正在播放的目录中的文件,因此如果我尝试在/home/它将搜索/home/test.lua,那么要使它工作,我必须执行check_error(mpv_set_property_string(ctx, "scripts", "/home/netsu/.config/mpv/test.lua")); (给出绝对路径)。
https://stackoverflow.com/questions/67131289
复制相似问题