2022-09-27 21:28:38 +02:00
|
|
|
// Copyright (c) 2022, arch1t3cht <arch1t3cht@gmail.com>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
//
|
|
|
|
// Aegisub Project http://www.aegisub.org/
|
|
|
|
|
|
|
|
#ifdef WITH_VAPOURSYNTH
|
|
|
|
#include "vapoursynth_common.h"
|
|
|
|
|
2023-01-30 02:52:18 +01:00
|
|
|
#include "vapoursynth_wrap.h"
|
2022-09-27 21:28:38 +02:00
|
|
|
#include "options.h"
|
2023-01-31 21:35:24 +01:00
|
|
|
#include "utils.h"
|
2023-02-24 01:41:56 +01:00
|
|
|
#include <libaegisub/background_runner.h>
|
2022-09-27 21:28:38 +02:00
|
|
|
#include <libaegisub/fs.h>
|
2023-01-30 02:52:18 +01:00
|
|
|
#include <libaegisub/path.h>
|
2022-09-27 21:28:38 +02:00
|
|
|
|
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
|
|
|
|
2023-01-30 02:52:18 +01:00
|
|
|
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))
|
2023-03-16 02:22:59 +01:00
|
|
|
throw VapourSynthError("Failed to set VSMap entry");
|
2023-01-30 02:52:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int OpenScriptOrVideo(const VSAPI *api, const VSSCRIPTAPI *sapi, VSScript *script, agi::fs::path const& filename, std::string default_script) {
|
2022-11-01 20:16:56 +01:00
|
|
|
int result;
|
2022-09-27 21:28:38 +02:00
|
|
|
if (agi::fs::HasExtension(filename, "py") || agi::fs::HasExtension(filename, "vpy")) {
|
2023-01-30 02:52:18 +01:00
|
|
|
result = sapi->evaluateFile(script, filename.string().c_str());
|
2022-09-27 21:28:38 +02:00
|
|
|
} else {
|
2023-01-30 02:52:18 +01:00
|
|
|
VSMap *map = api->createMap();
|
|
|
|
if (map == nullptr)
|
2023-03-16 02:22:59 +01:00
|
|
|
throw VapourSynthError("Failed to create VSMap for script info");
|
2023-01-30 02:52:18 +01:00
|
|
|
|
|
|
|
SetStringVar(api, map, "filename", filename.string());
|
2023-01-31 21:35:24 +01:00
|
|
|
SetStringVar(api, map, "__aegi_vscache", config::path->Decode("?local/vscache").string());
|
2023-03-16 02:01:55 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
SetStringVar(api, map, "__aegi_vsplugins", config::path->Decode("?data/vapoursynth").string());
|
|
|
|
#else
|
|
|
|
SetStringVar(api, map, "__aegi_vsplugins", "");
|
|
|
|
#endif
|
2023-01-30 02:52:18 +01:00
|
|
|
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))
|
2023-03-16 02:22:59 +01:00
|
|
|
throw VapourSynthError("Failed to set script info variables");
|
2023-01-30 02:52:18 +01:00
|
|
|
|
|
|
|
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");
|
2022-09-27 21:28:38 +02:00
|
|
|
}
|
2022-11-01 20:16:56 +01:00
|
|
|
return result;
|
2022-09-27 21:28:38 +02:00
|
|
|
}
|
|
|
|
|
2023-02-24 01:41:56 +01:00
|
|
|
void VSLogToProgressSink(int msgType, const char *msg, void *userData) {
|
|
|
|
int loglevel = 0;
|
|
|
|
std::string loglevel_str = OPT_GET("Provider/Video/VapourSynth/Log Level")->GetString();
|
|
|
|
if (loglevel_str == "Quiet")
|
|
|
|
loglevel = 5;
|
|
|
|
else if (loglevel_str == "Fatal")
|
|
|
|
loglevel = 4;
|
|
|
|
else if (loglevel_str == "Critical")
|
|
|
|
loglevel = 3;
|
|
|
|
else if (loglevel_str == "Warning")
|
|
|
|
loglevel = 2;
|
|
|
|
else if (loglevel_str == "Information")
|
|
|
|
loglevel = 1;
|
|
|
|
else if (loglevel_str == "Debug")
|
|
|
|
loglevel = 0;
|
|
|
|
|
|
|
|
if (msgType < loglevel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
reinterpret_cast<agi::ProgressSink *>(userData)->Log(msg);
|
|
|
|
}
|
|
|
|
|
2023-01-31 21:35:24 +01:00
|
|
|
void VSCleanCache() {
|
|
|
|
CleanCache(config::path->Decode("?local/vscache/"),
|
|
|
|
"",
|
|
|
|
OPT_GET("Provider/VapourSynth/Cache/Size")->GetInt(),
|
|
|
|
OPT_GET("Provider/VapourSynth/Cache/Files")->GetInt());
|
|
|
|
}
|
|
|
|
|
2022-09-27 21:28:38 +02:00
|
|
|
#endif // WITH_VAPOURSYNTH
|