vapoursynth: Set path variables and import path in python

This commit is contained in:
arch1t3cht 2023-01-30 02:52:18 +01:00
parent 88867d402c
commit 32aaf6372c
4 changed files with 32 additions and 10 deletions

View file

@ -61,7 +61,7 @@ VapoursynthAudioProvider::VapoursynthAudioProvider(agi::fs::path const& filename
throw VapoursynthError("Error creating script API"); throw VapoursynthError("Error creating script API");
} }
vs.GetScriptAPI()->evalSetWorkingDir(script, 1); vs.GetScriptAPI()->evalSetWorkingDir(script, 1);
if (OpenScriptOrVideo(vs.GetScriptAPI(), script, filename, OPT_GET("Provider/Audio/VapourSynth/Default Script")->GetString())) { if (OpenScriptOrVideo(vs.GetAPI(), vs.GetScriptAPI(), script, filename, OPT_GET("Provider/Audio/VapourSynth/Default Script")->GetString())) {
std::string msg = agi::format("Error executing VapourSynth script: %s", vs.GetScriptAPI()->getError(script)); std::string msg = agi::format("Error executing VapourSynth script: %s", vs.GetScriptAPI()->getError(script));
vs.GetScriptAPI()->freeScript(script); vs.GetScriptAPI()->freeScript(script);
throw VapoursynthError(msg); throw VapoursynthError(msg);

View file

@ -17,21 +17,43 @@
#ifdef WITH_VAPOURSYNTH #ifdef WITH_VAPOURSYNTH
#include "vapoursynth_common.h" #include "vapoursynth_common.h"
#include "vapoursynth_wrap.h"
#include "options.h" #include "options.h"
#include <libaegisub/fs.h> #include <libaegisub/fs.h>
#include <libaegisub/path.h>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
int OpenScriptOrVideo(const VSSCRIPTAPI *api, VSScript *script, agi::fs::path const& filename, std::string default_script) { void SetStringVar(const VSAPI *api, VSMap *map, std::string variable, std::string value) {
if (api->mapSetData(map, variable.c_str(), value.c_str(), -1, dtUtf8, 1))
throw VapoursynthError("Failed to set VSMap entry");
}
int OpenScriptOrVideo(const VSAPI *api, const VSSCRIPTAPI *sapi, VSScript *script, agi::fs::path const& filename, std::string default_script) {
int result; int result;
if (agi::fs::HasExtension(filename, "py") || agi::fs::HasExtension(filename, "vpy")) { if (agi::fs::HasExtension(filename, "py") || agi::fs::HasExtension(filename, "vpy")) {
result = api->evaluateFile(script, filename.string().c_str()); result = sapi->evaluateFile(script, filename.string().c_str());
} else { } else {
std::string fname = filename.string(); VSMap *map = api->createMap();
boost::replace_all(fname, "\\", "\\\\"); if (map == nullptr)
boost::replace_all(fname, "'", "\\'"); throw VapoursynthError("Failed to create VSMap for script info");
std::string vscript = "filename = '" + fname + "'\n" + default_script;
result = api->evaluateBuffer(script, vscript.c_str(), "aegisub"); SetStringVar(api, map, "filename", filename.string());
for (std::string dir : { "data", "dictionary", "local", "script", "temp", "user", })
// Don't include ?audio and ?video in here since these only hold the paths to the previous audio/video files.
SetStringVar(api, map, "__aegi_" + dir, config::path->Decode("?" + dir).string());
if (sapi->setVariables(script, map))
throw VapoursynthError("Failed to set script info variables");
api->freeMap(map);
std::string vscript;
vscript += "import sys\n";
vscript += "sys.path.append(f'{__aegi_data}/automation/vapoursynth')\n";
vscript += "sys.path.append(f'{__aegi_user}/automation/vapoursynth')\n";
vscript += default_script;
result = sapi->evaluateBuffer(script, vscript.c_str(), "aegisub");
} }
return result; return result;
} }

View file

@ -19,6 +19,6 @@
#include <libaegisub/fs_fwd.h> #include <libaegisub/fs_fwd.h>
int OpenScriptOrVideo(const VSSCRIPTAPI *api, VSScript *script, agi::fs::path const& filename, std::string default_script); int OpenScriptOrVideo(const VSAPI *api, const VSSCRIPTAPI *sapi, VSScript *script, agi::fs::path const& filename, std::string default_script);
#endif // WITH_VAPOURSYNTH #endif // WITH_VAPOURSYNTH

View file

@ -116,7 +116,7 @@ VapoursynthVideoProvider::VapoursynthVideoProvider(agi::fs::path const& filename
throw VapoursynthError("Error creating script API"); throw VapoursynthError("Error creating script API");
} }
vs.GetScriptAPI()->evalSetWorkingDir(script, 1); vs.GetScriptAPI()->evalSetWorkingDir(script, 1);
if (OpenScriptOrVideo(vs.GetScriptAPI(), script, filename, OPT_GET("Provider/Video/VapourSynth/Default Script")->GetString())) { if (OpenScriptOrVideo(vs.GetAPI(), vs.GetScriptAPI(), script, filename, OPT_GET("Provider/Video/VapourSynth/Default Script")->GetString())) {
std::string msg = agi::format("Error executing VapourSynth script: %s", vs.GetScriptAPI()->getError(script)); std::string msg = agi::format("Error executing VapourSynth script: %s", vs.GetScriptAPI()->getError(script));
vs.GetScriptAPI()->freeScript(script); vs.GetScriptAPI()->freeScript(script);
throw VapoursynthError(msg); throw VapoursynthError(msg);