Skip to content

Commit

Permalink
feat: switch to cli11, fix rendering process space handling, fix blur…
Browse files Browse the repository at this point in the history
….py imports
  • Loading branch information
f0e committed Oct 28, 2024
1 parent 57b7784 commit 8bb57d1
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 203 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build/${CMAKE_BUILD_TYPE})
find_package(fmt CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)
find_package(Boost REQUIRED COMPONENTS system filesystem)
find_package(cxxopts CONFIG REQUIRED)
find_package(CLI11 CONFIG REQUIRED)

# source files
file(GLOB_RECURSE COMMON_SOURCES
Expand Down Expand Up @@ -44,7 +44,7 @@ function(setup_target target)
fmt::fmt
nlohmann_json::nlohmann_json
Boost::system Boost::filesystem
cxxopts::cxxopts
CLI11::CLI11
)
target_compile_definitions(${target} PRIVATE
NOMINMAX
Expand Down
82 changes: 39 additions & 43 deletions src/cli/cli.cpp
Original file line number Diff line number Diff line change
@@ -1,69 +1,60 @@
#include "cli.h"

void cli::run(const cxxopts::ParseResult& cmd) {
if (!blur.initialise(
cmd["verbose"].as<bool>(),
cmd["preview"].as<bool>()
)) {
printf("Blur failed to initialise\n");
return;
bool cli::run(
std::vector<std::string> inputs,
std::vector<std::string> outputs,
std::vector<std::string> config_paths,
bool preview,
bool verbose
) {
if (!blur.initialise(verbose, preview)) {
std::wcout << L"Blur failed to initialize" << std::endl;
return false;
}

std::vector<std::string> inputs, outputs, config_paths;

if (!cmd.count("input")) {
printf("No input files specified. Use -i or --input.\n");
return;
bool manual_output_files = !outputs.empty();
if (manual_output_files && inputs.size() != outputs.size()) {
std::wcout << L"Input/output filename count mismatch ("
<< inputs.size() << L" inputs, "
<< outputs.size() << L" outputs)." << std::endl;
return false;
}

inputs = cmd["input"].as<std::vector<std::string>>();

bool manual_output_files = cmd.count("output");
if (manual_output_files) {
if (cmd.count("input") != cmd.count("output")) {
printf("Input/output filename count mismatch (%zu inputs, %zu outputs).\n", cmd.count("input"), cmd.count("output"));
return;
}

outputs = cmd["output"].as<std::vector<std::string>>();
bool manual_config_files = !config_paths.empty();
if (manual_config_files && inputs.size() != config_paths.size()) {
std::wcout << L"Input filename/config paths count mismatch ("
<< inputs.size() << L" inputs, "
<< config_paths.size() << L" config paths)." << std::endl;
return false;
}

bool manual_config_files = cmd.count("config-path"); // todo: cleanup redundancy ^^
if (manual_config_files) {
if (cmd.count("input") != cmd.count("config-path")) {
printf("Input filename/config paths count mismatch (%zu inputs, %zu config paths).\n", cmd.count("input"), cmd.count("config-path"));
return;
}

config_paths = cmd["config-path"].as<std::vector<std::string>>();

// check if all configs exist
bool paths_ok = true;
for (const auto& path : config_paths) {
if (!std::filesystem::exists(path)) {
printf("Specified config file path '%s' not found.\n", path.c_str());
paths_ok = false;
// TODO: test printing works with unicode
std::cout << "Specified config file path '"
<< path
<< "' not found." << std::endl;
return false;
}
}

if (!paths_ok)
return;
}

// queue videos to be rendered
for (size_t i = 0; i < inputs.size(); i++) {
for (size_t i = 0; i < inputs.size(); ++i) {
std::filesystem::path input_path = std::filesystem::canonical(inputs[i]);

if (!std::filesystem::exists(input_path)) {
wprintf(L"Video '%ls' was not found (wrong path?)\n", input_path.wstring().c_str());
return;
// TODO: test with unicode
std::wcout << L"Video '" << input_path.wstring()
<< L"' was not found (wrong path?)" << std::endl;
return false;
}

std::optional<std::filesystem::path> output_path;
std::optional<std::filesystem::path> config_path;

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

if (manual_output_files) {
output_path = std::filesystem::canonical(outputs[i]);
Expand All @@ -75,6 +66,7 @@ void cli::run(const cxxopts::ParseResult& cmd) {

// set up render
auto render = std::make_shared<c_render>(input_path, output_path, config_path);

rendering.queue_render(render);

if (blur.verbose) {
Expand All @@ -86,4 +78,8 @@ void cli::run(const cxxopts::ParseResult& cmd) {
rendering.render_videos();

// preview.stop();
}

std::wcout << L"Finished rendering" << std::endl;

return true;
}
10 changes: 8 additions & 2 deletions src/cli/cli.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#pragma once

namespace cli {
void run(const cxxopts::ParseResult& cmd);
}
bool run(
std::vector<std::string> inputs,
std::vector<std::string> outputs,
std::vector<std::string> config_paths,
bool preview,
bool verbose
);
}
2 changes: 1 addition & 1 deletion src/cli/cli_pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <common/common_pch.h>

// libs
#include <cxxopts.hpp>
#include <CLI/CLI.hpp>

#include <boost/process.hpp>
#include <boost/asio.hpp>
45 changes: 23 additions & 22 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
#include "cli.h"

int main(int argc, char* argv[]) {
cxxopts::Options options("blur", "Add motion blur to videos");
options.add_options()
("h,help", "Print usage")
("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>>())
("p,preview", "Enable preview", cxxopts::value<bool>())
("v,verbose", "Verbose mode", cxxopts::value<bool>())
;
CLI::App app{"Add motion blur to videos"};

std::vector<std::string> inputs;
std::vector<std::string> outputs;
std::vector<std::string> configPaths;
bool preview = false;
bool verbose = false;

try {
auto result = options.parse(argc, argv);
app.add_option("-i,--input", inputs, "Input file name(s)")
->required();
app.add_option("-o,--output", outputs, "Output file name(s) (optional)");
app.add_option("-c,--config-path", configPaths, "Manual configuration file path(s) (optional)");
app.add_flag("-p,--preview", preview, "Enable preview");
app.add_flag("-v,--verbose", verbose, "Verbose mode");

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

cli::run(inputs, outputs, configPaths, preview, verbose);

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

return 0;
return 0;
}
catch (const CLI::ParseError &e) {
std::cout << "Failed to parse arguments, use -h or --help for help." << std::endl;
return 1;
}
}
20 changes: 14 additions & 6 deletions src/common/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,18 @@ namespace helpers {

std::string get_executable_path();

template<typename TP>
inline std::time_t to_time_t(TP tp) {
using namespace std::chrono;
auto sctp = time_point_cast<system_clock::duration>(tp - TP::clock::now() + system_clock::now());
return system_clock::to_time_t(sctp);
template<typename Container>
auto join(const Container& container, const typename Container::value_type::value_type* delimiter) {
using CharT = typename Container::value_type::value_type;
std::basic_ostringstream<CharT> result;

auto it = container.begin();
if (it != container.end()) {
result << *it++; // Add the first element without a delimiter
}
while (it != container.end()) {
result << delimiter << *it++;
}
return result.str();
}
}
}
Loading

0 comments on commit 8bb57d1

Please sign in to comment.