mirror of
https://github.com/tkmxqrdxddd/davinci-video-converter
synced 2026-03-29 23:35:32 +02:00
feat: add devcontainer/nix support and refactor into modules
- Add .devcontainer/devcontainer.json for Podman development - Add shell.nix for Nix users - Refactor monolithic main.cpp into modular components: - parser: argument parsing - validator: configuration validation - converter: ffmpeg command building/execution - Update Makefile with proper dependencies and test target - Simplify build.sh to use make - Update README with development environment docs
This commit is contained in:
44
src/converter.cpp
Normal file
44
src/converter.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "converter.hpp"
|
||||
#include <sstream>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
std::string build_ffmpeg_command(const Config& config) {
|
||||
std::stringstream cmd;
|
||||
cmd << "ffmpeg -i \"" << config.input_path << "\"";
|
||||
|
||||
if (config.codec == "h264") {
|
||||
cmd << " -c:v libx264";
|
||||
} else if (config.codec == "h265") {
|
||||
cmd << " -c:v libx265";
|
||||
} else if (config.codec == "prores") {
|
||||
cmd << " -c:v prores_ks";
|
||||
}
|
||||
|
||||
cmd << " -preset " << config.quality;
|
||||
cmd << " -crf " << config.crf;
|
||||
cmd << " -c:a copy";
|
||||
|
||||
if (config.verbose) {
|
||||
cmd << " -loglevel verbose";
|
||||
}
|
||||
|
||||
cmd << " \"" << config.output_path << "\"";
|
||||
|
||||
return cmd.str();
|
||||
}
|
||||
|
||||
int execute_conversion(const std::string& command, bool verbose) {
|
||||
if (verbose) {
|
||||
std::cout << "Executing: " << command << std::endl;
|
||||
}
|
||||
|
||||
int result = std::system(command.c_str());
|
||||
|
||||
if (result != 0) {
|
||||
std::cerr << "Error: ffmpeg command failed with exit code " << result << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
src/include/config.hpp
Normal file
15
src/include/config.hpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef CONFIG_HPP
|
||||
#define CONFIG_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
struct Config {
|
||||
std::string input_path;
|
||||
std::string output_path;
|
||||
std::string codec = "h264";
|
||||
std::string quality = "medium";
|
||||
int crf = 23;
|
||||
bool verbose = false;
|
||||
};
|
||||
|
||||
#endif // CONFIG_HPP
|
||||
10
src/include/converter.hpp
Normal file
10
src/include/converter.hpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef CONVERTER_HPP
|
||||
#define CONVERTER_HPP
|
||||
|
||||
#include "config.hpp"
|
||||
#include <string>
|
||||
|
||||
std::string build_ffmpeg_command(const Config& config);
|
||||
int execute_conversion(const std::string& command, bool verbose);
|
||||
|
||||
#endif // CONVERTER_HPP
|
||||
9
src/include/parser.hpp
Normal file
9
src/include/parser.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef PARSER_HPP
|
||||
#define PARSER_HPP
|
||||
|
||||
#include "config.hpp"
|
||||
|
||||
Config parse_arguments(int argc, char* argv[]);
|
||||
void print_usage(const char* program_name);
|
||||
|
||||
#endif // PARSER_HPP
|
||||
9
src/include/validator.hpp
Normal file
9
src/include/validator.hpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef VALIDATOR_HPP
|
||||
#define VALIDATOR_HPP
|
||||
|
||||
#include "config.hpp"
|
||||
#include <string>
|
||||
|
||||
bool validate_config(const Config& config, std::string& error);
|
||||
|
||||
#endif // VALIDATOR_HPP
|
||||
109
src/main.cpp
109
src/main.cpp
@@ -1,96 +1,29 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
|
||||
class VideoConverter {
|
||||
public:
|
||||
void startConversion(const std::string &inputFile, const std::string &outputFile);
|
||||
|
||||
private:
|
||||
bool conversionInProgress = false;
|
||||
};
|
||||
|
||||
void VideoConverter::startConversion(const std::string &inputFile, const std::string &outputFile) {
|
||||
if (conversionInProgress) {
|
||||
std::cerr << "Error: Conversion already in progress." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (inputFile.empty()) {
|
||||
std::cerr << "Error: Please provide an input file name." << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// Determine the output path
|
||||
std::filesystem::path outputPath;
|
||||
if (outputFile.empty()) {
|
||||
// If no output file is specified, create one in the current directory
|
||||
outputPath = std::filesystem::current_path() / (std::filesystem::path(inputFile).stem().string() + ".mov");
|
||||
} else {
|
||||
outputPath = std::filesystem::path(outputFile);
|
||||
if (outputPath.extension() != ".mov") {
|
||||
outputPath += ".mov";
|
||||
}
|
||||
}
|
||||
|
||||
conversionInProgress = true;
|
||||
std::cout << "Starting conversion..." << std::endl;
|
||||
|
||||
std::thread([this, inputFile, outputPath]() {
|
||||
std::string command = "ffmpeg -i \"" + inputFile + "\" -c:v dnxhd -profile:v dnxhr_hq -pix_fmt yuv422p -c:a alac \"" + outputPath.string() + "\" 2> /dev/null";
|
||||
int result = std::system(command.c_str());
|
||||
conversionInProgress = false;
|
||||
|
||||
if (result == 0) {
|
||||
std::cout << "Video conversion completed successfully." << std::endl;
|
||||
} else {
|
||||
std::cerr << "Error: Conversion failed with exit code " << result << std::endl;
|
||||
}
|
||||
}).detach();
|
||||
}
|
||||
|
||||
void printHelp() {
|
||||
std::cout << "Usage: davinci-convert /path/to/video [--output /path/to/output/folder]\n";
|
||||
std::cout << "Options:\n";
|
||||
std::cout << " /path/to/video Path to the input video file.\n";
|
||||
std::cout << " --output /path/to/output/folder Path to the output video file (optional).\n";
|
||||
std::cout << " --help Show this help message.\n";
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
VideoConverter converter;
|
||||
std::string inputFile, outputFile;
|
||||
#include "config.hpp"
|
||||
#include "parser.hpp"
|
||||
#include "validator.hpp"
|
||||
#include "converter.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
printHelp();
|
||||
print_usage(argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// The first argument is the input file
|
||||
inputFile = argv[1];
|
||||
|
||||
// Parse the remaining arguments
|
||||
for (int i = 2; i < argc; i++) {
|
||||
if (std::strcmp(argv[i], "--output") == 0 && i + 1 < argc) {
|
||||
outputFile = argv[++i];
|
||||
} else if (std::strcmp(argv[i], "--help") == 0) {
|
||||
printHelp();
|
||||
return 0;
|
||||
} else {
|
||||
std::cerr << "Unknown option: " << argv[i] << std::endl;
|
||||
printHelp();
|
||||
return 1;
|
||||
}
|
||||
|
||||
Config config = parse_arguments(argc, argv);
|
||||
|
||||
std::string error;
|
||||
if (!validate_config(config, error)) {
|
||||
std::cerr << "Error: " << error << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
converter.startConversion(inputFile, outputFile);
|
||||
|
||||
// Keep the application running until the conversion is done
|
||||
std::cout << "Press Enter to exit..." << std::endl;
|
||||
std::cin.get();
|
||||
|
||||
|
||||
std::string command = build_ffmpeg_command(config);
|
||||
|
||||
if (execute_conversion(command, config.verbose) != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Conversion completed successfully!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
62
src/parser.cpp
Normal file
62
src/parser.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include "parser.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
|
||||
void print_usage(const char* program_name) {
|
||||
std::cout << "Usage: " << program_name << " [options] <input> <output>\n"
|
||||
<< "Options:\n"
|
||||
<< " -c, --codec <codec> Video codec (h264, h265, prores) [default: h264]\n"
|
||||
<< " -q, --quality <qual> Quality preset (fast, medium, slow) [default: medium]\n"
|
||||
<< " -r, --crf <value> CRF value 0-51 [default: 23]\n"
|
||||
<< " -v, --verbose Enable verbose output\n"
|
||||
<< " -h, --help Show this help message\n";
|
||||
}
|
||||
|
||||
Config parse_arguments(int argc, char* argv[]) {
|
||||
Config config;
|
||||
std::vector<std::string> positional_args;
|
||||
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
std::string arg = argv[i];
|
||||
|
||||
if (arg == "-h" || arg == "--help") {
|
||||
print_usage(argv[0]);
|
||||
std::exit(0);
|
||||
} else if (arg == "-c" || arg == "--codec") {
|
||||
if (i + 1 < argc) {
|
||||
config.codec = argv[++i];
|
||||
}
|
||||
} else if (arg == "-q" || arg == "--quality") {
|
||||
if (i + 1 < argc) {
|
||||
config.quality = argv[++i];
|
||||
}
|
||||
} else if (arg == "-r" || arg == "--crf") {
|
||||
if (i + 1 < argc) {
|
||||
try {
|
||||
config.crf = std::stoi(argv[++i]);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error: Invalid CRF value" << std::endl;
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
} else if (arg == "-v" || arg == "--verbose") {
|
||||
config.verbose = true;
|
||||
} else if (arg[0] != '-') {
|
||||
positional_args.push_back(arg);
|
||||
} else {
|
||||
std::cerr << "Error: Unknown option: " << arg << std::endl;
|
||||
print_usage(argv[0]);
|
||||
std::exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
if (positional_args.size() >= 1) {
|
||||
config.input_path = positional_args[0];
|
||||
}
|
||||
if (positional_args.size() >= 2) {
|
||||
config.output_path = positional_args[1];
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
42
src/validator.cpp
Normal file
42
src/validator.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "validator.hpp"
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
bool validate_config(const Config& config, std::string& error) {
|
||||
if (config.input_path.empty()) {
|
||||
error = "Input path is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config.output_path.empty()) {
|
||||
error = "Output path is required";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!fs::exists(config.input_path)) {
|
||||
error = "Input file does not exist: " + config.input_path;
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<std::string> valid_codecs = {"h264", "h265", "prores"};
|
||||
if (std::find(valid_codecs.begin(), valid_codecs.end(), config.codec) == valid_codecs.end()) {
|
||||
error = "Invalid codec: " + config.codec + ". Valid options: h264, h265, prores";
|
||||
return false;
|
||||
}
|
||||
|
||||
const std::vector<std::string> valid_qualities = {"fast", "medium", "slow"};
|
||||
if (std::find(valid_qualities.begin(), valid_qualities.end(), config.quality) == valid_qualities.end()) {
|
||||
error = "Invalid quality preset: " + config.quality + ". Valid options: fast, medium, slow";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (config.crf < 0 || config.crf > 51) {
|
||||
error = "CRF must be between 0 and 51";
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user