feat: improved file not found messages

Signed-off-by: k4yt3x <i@k4yt3x.com>
This commit is contained in:
k4yt3x 2024-11-02 00:00:00 +00:00
parent 27c76189d9
commit 9c491d4277
No known key found for this signature in database
5 changed files with 35 additions and 36 deletions

View File

@ -58,7 +58,7 @@ struct RealESRGANConfig {
#ifdef _WIN32 #ifdef _WIN32
const wchar_t *model_path; const wchar_t *model_path;
#else #else
const char *model_path; const char *model_name;
#endif #endif
}; };

View File

@ -1,8 +1,6 @@
#ifndef REALSRGAN_FILTER_H #ifndef REALSRGAN_FILTER_H
#define REALSRGAN_FILTER_H #define REALSRGAN_FILTER_H
#include <filesystem>
extern "C" { extern "C" {
#include <libavcodec/avcodec.h> #include <libavcodec/avcodec.h>
} }
@ -17,9 +15,11 @@ class RealesrganFilter : public Filter {
int gpuid; int gpuid;
bool tta_mode; bool tta_mode;
int scaling_factor; int scaling_factor;
const std::filesystem::path model_path; #ifdef _WIN32
const std::filesystem::path custom_model_param_path; const std::wstring model_name;
const std::filesystem::path custom_model_bin_path; #else
const std::string model_name;
#endif
AVRational in_time_base; AVRational in_time_base;
AVRational out_time_base; AVRational out_time_base;
AVPixelFormat out_pix_fmt; AVPixelFormat out_pix_fmt;
@ -30,9 +30,11 @@ class RealesrganFilter : public Filter {
int gpuid = 0, int gpuid = 0,
bool tta_mode = false, bool tta_mode = false,
int scaling_factor = 4, int scaling_factor = 4,
const std::filesystem::path model = std::filesystem::path("realesr-animevideov3"), #ifdef _WIN32
const std::filesystem::path custom_model_param_path = std::filesystem::path(), const std::wstring model_name = L"realesr-animevideov3"
const std::filesystem::path custom_model_bin_path = std::filesystem::path() #else
const std::string model_name = "realesr-animevideov3"
#endif
); );
// Destructor // Destructor

View File

@ -49,7 +49,7 @@ int LibplaceboFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
// Check if the shader file exists // Check if the shader file exists
if (!std::filesystem::exists(shader_full_path)) { if (!std::filesystem::exists(shader_full_path)) {
spdlog::error("libplacebo shader file not found: {}", shader_full_path.string()); spdlog::error("libplacebo shader file not found: '{}'", shader_path.string());
return -1; return -1;
} }

View File

@ -439,13 +439,13 @@ extern "C" int process_video(
}; };
} else if (filter_config->filter_type == FILTER_REALESRGAN) { } else if (filter_config->filter_type == FILTER_REALESRGAN) {
const auto &config = filter_config->config.realesrgan; const auto &config = filter_config->config.realesrgan;
if (!config.model_path) { if (!config.model_name) {
spdlog::error("Model name must be provided for the RealESRGAN filter"); spdlog::error("Model name must be provided for the RealESRGAN filter");
cleanup(); cleanup();
return -1; return -1;
} }
filter = new RealesrganFilter{ filter = new RealesrganFilter{
config.gpuid, config.tta_mode, config.scaling_factor, config.model_path config.gpuid, config.tta_mode, config.scaling_factor, config.model_name
}; };
} else { } else {
spdlog::error("Unknown filter type"); spdlog::error("Unknown filter type");

View File

@ -2,6 +2,7 @@
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>
#include <filesystem>
#include <string> #include <string>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
@ -13,17 +14,18 @@ RealesrganFilter::RealesrganFilter(
int gpuid, int gpuid,
bool tta_mode, bool tta_mode,
int scaling_factor, int scaling_factor,
const std::filesystem::path model_path, #ifdef _WIN32
const std::filesystem::path custom_model_param_path, const std::wstring model_name
const std::filesystem::path custom_model_bin_path #else
const std::string model_name
#endif
) )
: realesrgan(nullptr), : realesrgan(nullptr),
gpuid(gpuid), gpuid(gpuid),
tta_mode(tta_mode), tta_mode(tta_mode),
scaling_factor(scaling_factor), scaling_factor(scaling_factor),
model_path(std::move(model_path)), model_name(std::move(model_name)) {
custom_model_param_path(std::move(custom_model_param_path)), }
custom_model_bin_path(std::move(custom_model_bin_path)) {}
RealesrganFilter::~RealesrganFilter() { RealesrganFilter::~RealesrganFilter() {
if (realesrgan) { if (realesrgan) {
@ -37,22 +39,17 @@ int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
std::filesystem::path model_param_path; std::filesystem::path model_param_path;
std::filesystem::path model_bin_path; std::filesystem::path model_bin_path;
if (!model_path.empty()) { #ifdef _WIN32
// Find the model paths by model name if provided std::wstring param_file_name = model_name + L"-x" + std::to_wstring(scaling_factor) + L".param";
// TODO: ensure this works with wide strings on Windows std::wstring bin_file_name = model_name + L"-x" + std::to_wstring(scaling_factor) + L".bin";
model_param_path = std::filesystem::path("models") / "realesrgan" / #else
(model_path.string() + "-x" + std::to_string(scaling_factor) + ".param"); std::string param_file_name = model_name + "-x" + std::to_string(scaling_factor) + ".param";
model_bin_path = std::filesystem::path("models") / "realesrgan" / std::string bin_file_name = model_name + "-x" + std::to_string(scaling_factor) + ".bin";
(model_path.string() + "-x" + std::to_string(scaling_factor) + ".bin"); #endif
} else if (!custom_model_param_path.empty() && !custom_model_bin_path.empty()) {
// Use the custom model paths if provided // Find the model paths by model name if provided
model_param_path = custom_model_param_path; model_param_path = std::filesystem::path("models") / "realesrgan" / param_file_name;
model_bin_path = custom_model_bin_path; model_bin_path = std::filesystem::path("models") / "realesrgan" / bin_file_name;
} else {
// Neither model name nor custom model paths provided
spdlog::error("Model or model paths must be provided for RealESRGAN filter");
return -1;
}
// Get the full paths using a function that possibly modifies or validates the path // Get the full paths using a function that possibly modifies or validates the path
std::filesystem::path model_param_full_path = find_resource_file(model_param_path); std::filesystem::path model_param_full_path = find_resource_file(model_param_path);
@ -60,11 +57,11 @@ int RealesrganFilter::init(AVCodecContext *dec_ctx, AVCodecContext *enc_ctx, AVB
// Check if the model files exist // Check if the model files exist
if (!std::filesystem::exists(model_param_full_path)) { if (!std::filesystem::exists(model_param_full_path)) {
spdlog::error("RealESRGAN model param file not found: {}", model_param_full_path.string()); spdlog::error("RealESRGAN model param file not found: {}", model_param_path.string());
return -1; return -1;
} }
if (!std::filesystem::exists(model_bin_full_path)) { if (!std::filesystem::exists(model_bin_full_path)) {
spdlog::error("RealESRGAN model bin file not found: {}", model_bin_full_path.string()); spdlog::error("RealESRGAN model bin file not found: {}", model_bin_path.string());
return -1; return -1;
} }