From b4c0399399c9493c9cd5f52da5f4a6f824e6ea89 Mon Sep 17 00:00:00 2001 From: tkmxqrdxddd Date: Sat, 19 Oct 2024 15:57:57 +0200 Subject: [PATCH] fixed problems --- Makefile | 14 +++---- build.sh | 2 +- src/main.cpp | 112 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 82 insertions(+), 46 deletions(-) diff --git a/Makefile b/Makefile index 6067d78..a652c86 100644 --- a/Makefile +++ b/Makefile @@ -3,25 +3,25 @@ # 'make clean' removes all .o and executable files # -# define the Cpp compiler to use +# Define the C++ compiler to use CXX = g++ -# define any compile-time flags +# Define any compile-time flags CXXFLAGS = -std=c++17 -Wall -Wextra -g -# define output directory +# Define output directory OUTPUT = output -# define source directory +# Define source directory SRC = src -# define the main executable name +# Define the main executable name MAIN = davinci-convert -# define the C source files +# Define the C source files SOURCES = $(wildcard $(SRC)/*.cpp) -# define the C object files +# Define the C object files OBJECTS = $(SOURCES:.cpp=.o) OUTPUTMAIN = $(OUTPUT)/$(MAIN) diff --git a/build.sh b/build.sh index 8c42ab2..387b58f 100644 --- a/build.sh +++ b/build.sh @@ -93,4 +93,4 @@ echo "Installing the application..." sudo make install # Inform the user about the executable -echo "You can run the application using davinci-convert" \ No newline at end of file +echo "You can run the application using 'davinci-convert'" \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4896bfe..f15e63d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,53 +2,89 @@ #include #include #include +#include +#include class VideoConverter { -private: - bool conversionInProgress; - public: - VideoConverter() : conversionInProgress(false) {} + void startConversion(const std::string &inputFile, const std::string &outputFile); - void startConversion(const std::string &inputFile, const std::string &outputFile) { - if (conversionInProgress) { - std::cerr << "Error: Conversion already in progress." << std::endl; - return; - } - - if (inputFile.empty() || outputFile.empty()) { - std::cerr << "Error: Please enter both input and output file names." << std::endl; - return; - } - - conversionInProgress = true; - std::cout << "Starting conversion..." << std::endl; - - std::thread conversionThread([this, inputFile, outputFile]() { - std::string command = "ffmpeg -i \"" + inputFile + "\" -c:v dnxhd -profile:v dnxhr_hq -pix_fmt yuv422p -c:a alac \"" + outputFile + "\""; - 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; - } - }); - - conversionThread.detach(); // Detach the thread to allow it to run independently - } +private: + bool conversionInProgress = false; }; -int main() { +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> ffmpeg_error.log"; + 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; - std::cout << "Welcome to the Video Converter!" << std::endl; - std::cout << "Enter the input file path: "; - std::getline(std::cin, inputFile); - std::cout << "Enter the output file path: "; - std::getline(std::cin, outputFile); + if (argc < 2) { + printHelp(); + 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; + } + } converter.startConversion(inputFile, outputFile);