Replace hand-written args functionality with Taywee/args
This commit is contained in:
parent
b2d9eadb9e
commit
1b8e9c529d
104
src/main.cpp
104
src/main.cpp
|
|
@ -11,74 +11,74 @@
|
|||
|
||||
#include "VideoProcessor.hpp"
|
||||
#include "Logger.hpp"
|
||||
#include "args/args.hxx"
|
||||
|
||||
namespace {
|
||||
|
||||
struct Args {
|
||||
std::string inputPath;
|
||||
std::string outputPath;
|
||||
int frameIndex = 0;
|
||||
};
|
||||
|
||||
std::optional<Args> parseArgs(int argc, char** argv) {
|
||||
if (argc < 3) {
|
||||
return std::nullopt;
|
||||
}
|
||||
Args args;
|
||||
args.inputPath = argv[1];
|
||||
args.outputPath = argv[2];
|
||||
if (argc >= 4) {
|
||||
try {
|
||||
args.frameIndex = std::stoi(argv[3]);
|
||||
} catch (const std::exception&) {
|
||||
std::cerr << "Invalid frame index: " << argv[3] << "\n";
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
void printUsage(const char* programName) {
|
||||
std::cerr << "Usage: " << programName
|
||||
<< " <input-video> <output-image> [frame-index]\n"
|
||||
<< " Extracts one frame (default: first) from a video file and\n"
|
||||
<< " writes it to <output-image> using OpenCV.\n";
|
||||
}
|
||||
|
||||
int run(const Args& args) {
|
||||
Logger log(1);
|
||||
int extract_frame (uint8_t logLevel, std::string inputPath, std::string outputPath, uint16_t frameIndex) {
|
||||
Logger log(logLevel);
|
||||
VideoProcessor processor;
|
||||
if (!processor.open(args.inputPath)) {
|
||||
log.message(LogLevel::Error, std::format("Could not open video: {}", args.inputPath));
|
||||
|
||||
if (!processor.open(inputPath)) {
|
||||
log.message(LogLevel::Error, std::format("Could not open video: {}", inputPath));
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Opened " << args.inputPath
|
||||
<< " (" << processor.width() << "x" << processor.height()
|
||||
<< " @ " << processor.fps() << " fps)\n";
|
||||
log.message(LogLevel::Info, std::format( "Opened {} ( {} x {} ) @ {} fps", inputPath, processor.width(), processor.height(), processor.fps()));
|
||||
|
||||
auto frame = processor.frameAt(args.frameIndex);
|
||||
auto frame = processor.frameAt(frameIndex);
|
||||
if (!frame) {
|
||||
std::cerr << "Could not decode frame " << args.frameIndex << "\n";
|
||||
log.message(LogLevel::Error, std::format("Could not decode frame {}", frameIndex));
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!cv::imwrite(args.outputPath, *frame)) {
|
||||
std::cerr << "Failed to write output image: " << args.outputPath << "\n";
|
||||
if (!cv::imwrite(outputPath, *frame)) {
|
||||
log.message(LogLevel::Error, std::format("Failed to write output image: {}", outputPath));
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Wrote frame " << args.frameIndex << " to " << args.outputPath << "\n";
|
||||
log.message(LogLevel::Info, std::format("Wrote frame {} to {}", frameIndex, outputPath));
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
int main (int argc, char** argv) {
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
auto args = parseArgs(argc, argv);
|
||||
if (!args) {
|
||||
printUsage(argv[0]);
|
||||
return 2;
|
||||
uint8_t logLevel = 1;
|
||||
|
||||
args::ArgumentParser parser("ffmpeg_opencv_example", "FFMpeg + OpenCV Example Application");
|
||||
args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"});
|
||||
args::Flag verbose(parser, "verbose", "Display verbose logging statements", {'v', "verbose"});
|
||||
args::Flag silent(parser, "silent", "Display verbose no logging statments (overrides verbose)", {'s', "silent"});
|
||||
args::Positional<std::string> input(parser, "input", "Video to extract a single frame from");
|
||||
args::Positional<std::string> output(parser, "output", "The file to write the frame to");
|
||||
args::ValueFlag<uint16_t> frame(parser, "frame", "Frame index to export at (default: 0)", {'f', "frame"}, 0);
|
||||
|
||||
try
|
||||
{
|
||||
parser.ParseCLI(argc, argv);
|
||||
}
|
||||
return run(*args);
|
||||
catch (args::Help)
|
||||
{
|
||||
std::cout << parser;
|
||||
return 0;
|
||||
}
|
||||
catch (args::ParseError e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
std::cerr << parser;
|
||||
return 1;
|
||||
}
|
||||
catch (args::ValidationError e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
std::cerr << parser;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
logLevel = 2;
|
||||
}
|
||||
if (silent) {
|
||||
logLevel = 0;
|
||||
}
|
||||
|
||||
return extract_frame(logLevel, args::get(input), args::get(output), args::get(frame));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue