Skip to content

Commit

Permalink
installer support & fixed command-line mode
Browse files Browse the repository at this point in the history
  • Loading branch information
f0e committed Aug 19, 2021
1 parent 7bd27dc commit cafa096
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 52 deletions.
71 changes: 43 additions & 28 deletions blur/blur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ void c_blur::remove_temp_path(const std::string& path) {
}

void c_blur::run(int argc, char* argv[], const cxxopts::ParseResult& cmd) {
using_ui = !cmd["disable-ui"].as<bool>();
using_ui = !cmd["noui"].as<bool>();
verbose = using_ui || cmd["verbose"].as<bool>();

console.setup();
if (using_ui) {
console.setup();

if (blur.using_ui) {
// print art
const std::vector<const char*> art{
" _/ _/ ",
Expand All @@ -76,32 +76,38 @@ void c_blur::run(int argc, char* argv[], const cxxopts::ParseResult& cmd) {
console.print_line();
}

// check if dependencies were installed
try {
if (!helpers::detect_command("ffmpeg"))
throw std::exception("FFmpeg could not be found");
path = std::filesystem::path(helpers::get_executable_path()).parent_path().string(); // folder the exe is in
used_installer = std::filesystem::exists(fmt::format("{}/lib/vapoursynth/vspipe.exe", path))
&& std::filesystem::exists(fmt::format("{}/lib/ffmpeg/ffmpeg.exe", path));

if (!helpers::detect_command("py"))
throw std::exception("Python could not be found");
if (!used_installer) {
// didn't use installer, check if dependencies were installed
try {
if (!helpers::detect_command("ffmpeg"))
throw std::exception("FFmpeg could not be found");

if (!helpers::detect_command("vspipe"))
throw std::exception("VapourSynth could not be found");
}
catch (const std::exception& e) {
console.print(e.what());
console.print_blank_line();
console.print("Make sure you have followed the installation instructions");
console.print("https://github.com/f0e/blur/blob/master/README.md#Requirements");
console.print_blank_line();
console.print("If you're still unsure, open an issue on the GitHub");
console.print_line();
if (!helpers::detect_command("py"))
throw std::exception("Python could not be found");

if (using_ui) {
console.print("Press any key to exit.");
_getch();
if (!helpers::detect_command("vspipe"))
throw std::exception("VapourSynth could not be found");
}
catch (const std::exception& e) {
console.print(e.what());
console.print_blank_line();
console.print("Make sure you have followed the installation instructions");
console.print("https://github.com/f0e/blur/blob/master/README.md#Requirements");
console.print_blank_line();
console.print("If you're still unsure, open an issue on the GitHub");
console.print_line();

if (using_ui) {
console.print("Press any key to exit.");
_getch();
}

return;
return;
}
}

if (using_ui) {
Expand Down Expand Up @@ -187,13 +193,22 @@ void c_blur::run(int argc, char* argv[], const cxxopts::ParseResult& cmd) {
std::optional<std::string> config_path;

if (manual_config_files)
config_path = config_paths[i];
config_path = std::filesystem::canonical(config_paths[i]).string();

if (manual_output_files)
output_filename = output_filenames[i];
if (manual_output_files) {
// create output directory if needed
auto path = std::filesystem::weakly_canonical(output_filenames[i]);
if (!std::filesystem::exists(path.parent_path()))
std::filesystem::create_directories(path.parent_path());

output_filename = path.string();
}

// set up render
c_render render(input_filename, output_filename, config_path);
c_render render(std::filesystem::canonical(input_filename).string(),
output_filename,
config_path);

rendering.queue_render(render);

if (verbose) {
Expand Down
3 changes: 3 additions & 0 deletions blur/blur.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class c_blur {
bool using_ui = true;
bool verbose = true;

std::string path;
bool used_installer = false;

private:
std::vector<std::string> get_files(int& argc, char* argv[]);

Expand Down
8 changes: 7 additions & 1 deletion blur/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ int helpers::exec(const std::string& command) {
FILE* pipe;

// run command and redirect output to pipe
if ((pipe = _popen(command.c_str(), "rt")) == NULL)
if ((pipe = _popen(std::string("\"" + command + "\"").c_str(), "rt")) == NULL)
return -1;

// read from pipe until end
Expand All @@ -82,3 +82,9 @@ bool helpers::detect_command(const std::string& command) {
auto ret = exec("where /q " + command);
return ret == 0;
}

std::string helpers::get_executable_path() {
char buf[MAX_PATH];
GetModuleFileNameA(NULL, buf, MAX_PATH);
return std::string(buf);
}
2 changes: 2 additions & 0 deletions blur/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace helpers {
int exec(const std::string& command);
bool detect_command(const std::string& command);

std::string get_executable_path();

template <typename TP>
inline std::time_t to_time_t(TP tp) {
using namespace std::chrono;
Expand Down
21 changes: 13 additions & 8 deletions blur/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ int main(int argc, char* argv[]) {
("i,input", "Input file name(s)", cxxopts::value<std::vector<std::string>>())
("o,output", "Output file name(s) (optional)", cxxopts::value<std::vector<std::string>>())
("c,config-path", "Manual configuration file path(s) (optional)", cxxopts::value<std::vector<std::string>>())
("d,disable-ui", "Disable user interface", cxxopts::value<bool>())
("n,noui", "Disable user interface", cxxopts::value<bool>())
("v,verbose", "Verbose mode", cxxopts::value<bool>())
;

auto result = options.parse(argc, argv);

if (result.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}
try {
auto result = options.parse(argc, argv);

if (result.count("help")) {
std::cout << options.help() << std::endl;
return 0;
}

blur.run(argc, argv, result);
blur.run(argc, argv, result);
}
catch (const cxxopts::OptionParseException&) {
std::cout << "Failed to parse arguments, use -h or --help for help.";
}

return 0;
}
29 changes: 16 additions & 13 deletions blur/rendering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void c_render::build_output_filename() {
// build output filename
int num = 1;
do {
this->output_filename = fmt::format("{} - blur", this->video_name);
this->output_path = this->video_folder + fmt::format("{} - blur", this->video_name);

if (this->settings.detailed_filenames) {
std::string extra_details;
Expand All @@ -98,17 +98,17 @@ void c_render::build_output_filename() {
}

if (extra_details != "") {
this->output_filename += " ~ " + extra_details;
this->output_path += " ~ " + extra_details;
}
}

if (num > 1)
this->output_filename += fmt::format(" ({})", num);
this->output_path += fmt::format(" ({})", num);

this->output_filename += ".mp4";
this->output_path += ".mp4";

num++;
} while (std::filesystem::exists(this->output_filename));
} while (std::filesystem::exists(this->output_path));
}

c_render::c_render(const std::string& input_path, std::optional<std::string> output_filename, std::optional<std::string> config_path) {
Expand All @@ -119,9 +119,6 @@ c_render::c_render(const std::string& input_path, std::optional<std::string> out

this->input_filename = std::filesystem::path(this->video_path).filename().string();

// set working directory
std::filesystem::current_path(this->video_folder);

if (blur.using_ui) {
// print message
if (!rendering.queue.empty()) {
Expand Down Expand Up @@ -168,22 +165,28 @@ c_render::c_render(const std::string& input_path, std::optional<std::string> out
}

if (output_filename.has_value())
this->output_filename = output_filename.value();
this->output_path = output_filename.value();
else
this->build_output_filename();

if (blur.using_ui) {
console.print(fmt::format("writing to {}", this->output_filename));
console.print(fmt::format("writing to {}", this->output_path));

console.print_line();
}
}

std::string c_render::build_ffmpeg_command() {
std::string pipe_command = fmt::format("vspipe -y \"{}\" -", script_handler.script_filename);
std::string vspipe_path = "vspipe", ffmpeg_path = "ffmpeg";
if (blur.used_installer) {
vspipe_path = fmt::format("\"{}/lib/vapoursynth/vspipe.exe\"", blur.path);
ffmpeg_path = fmt::format("\"{}/lib/ffmpeg/ffmpeg.exe\"", blur.path);
}

std::string pipe_command = fmt::format("{} -y \"{}\" -", vspipe_path, script_handler.script_filename);

// build ffmpeg command
std::string ffmpeg_command = "ffmpeg";
std::string ffmpeg_command = ffmpeg_path;
{
// ffmpeg settings
ffmpeg_command += " -loglevel error -hide_banner -stats";
Expand Down Expand Up @@ -237,7 +240,7 @@ std::string c_render::build_ffmpeg_command() {
ffmpeg_command += " -movflags +faststart";

// output
ffmpeg_command += fmt::format(" \"{}\"", output_filename);
ffmpeg_command += fmt::format(" \"{}\"", output_path);

// extra output for preview. generate low-quality preview images.
if (settings.preview) {
Expand Down
4 changes: 2 additions & 2 deletions blur/rendering.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class c_render {
std::string video_folder;

std::string input_filename;
std::string output_filename;
std::string output_path;

std::string temp_path;

Expand All @@ -31,7 +31,7 @@ class c_render {
}

std::string get_output_video_path() {
return output_filename;
return output_path;
}

public:
Expand Down
File renamed without changes.

0 comments on commit cafa096

Please sign in to comment.