-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathargs_parser.cpp
More file actions
550 lines (511 loc) · 22.5 KB
/
Copy pathargs_parser.cpp
File metadata and controls
550 lines (511 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/**
* @file args_parser.cpp
* @brief Command-line argument parser implementation
* @author wysaid (this@wysaid.org)
* @date 2025-12
*/
#include "args_parser.h"
#include <algorithm>
#include <ccap_config.h>
#include <cctype>
#include <cstdlib>
#include <filesystem>
#include <iostream>
namespace ccap_cli {
// Default values
constexpr int DEFAULT_WIDTH = 1280;
constexpr int DEFAULT_HEIGHT = 720;
constexpr int DEFAULT_PREVIEW_WIDTH = 1920;
constexpr int DEFAULT_PREVIEW_HEIGHT = 1080;
constexpr double DEFAULT_FPS = 30.0;
constexpr int DEFAULT_TIMEOUT_MS = 5000;
std::string normalizeWindowsCameraBackend(std::string value) {
std::transform(value.begin(), value.end(), value.begin(), [](unsigned char ch) {
return static_cast<char>(std::tolower(ch));
});
if (value == "auto") {
return "auto";
}
if (value == "msmf" || value == "mediafoundation") {
return "msmf";
}
if (value == "dshow" || value == "directshow") {
return "dshow";
}
return {};
}
[[noreturn]] void printWindowsBackendOptionErrorAndExit(const char* programName, const std::string& message) {
std::cerr << "Error: " << message << "\n\n";
printUsage(programName);
std::exit(1);
}
void setWindowsCameraBackend(CLIOptions& opts, const char* programName, const std::string& rawValue) {
#if defined(_WIN32) || defined(_WIN64)
std::string normalized = normalizeWindowsCameraBackend(rawValue);
if (normalized.empty()) {
printWindowsBackendOptionErrorAndExit(programName,
"invalid Windows camera backend '" + rawValue + "'. Use auto, msmf, or dshow.");
}
opts.windowsCameraBackend = std::move(normalized);
#else
(void)opts;
printWindowsBackendOptionErrorAndExit(programName,
"Windows camera backend options are only supported on Windows.");
#endif
}
void printVersion() {
std::cout << "ccap version " << CCAP_VERSION_STRING << " Copyright (c) 2025 wysaid\n";
#if defined(CCAP_CLI_BUILD_COMPILER) || defined(CCAP_CLI_BUILD_PLATFORM) || defined(CCAP_CLI_BUILD_ARCH)
std::cout << " built with";
#ifdef CCAP_CLI_BUILD_COMPILER
std::cout << " " << CCAP_CLI_BUILD_COMPILER;
#endif
#ifdef CCAP_CLI_BUILD_PLATFORM
std::cout << " on " << CCAP_CLI_BUILD_PLATFORM;
#endif
#ifdef CCAP_CLI_BUILD_ARCH
std::cout << " (" << CCAP_CLI_BUILD_ARCH << ")";
#endif
#ifdef CCAP_CLI_LIBC_TYPE
std::cout << " / " << CCAP_CLI_LIBC_TYPE;
#endif
std::cout << "\n";
#endif
#ifdef CCAP_CLI_BUILD_TYPE
std::cout << " cmake options: -DCMAKE_BUILD_TYPE=" << CCAP_CLI_BUILD_TYPE;
#ifdef CCAP_CLI_STATIC_RUNTIME
#if CCAP_CLI_STATIC_RUNTIME
std::cout << " -DCCAP_BUILD_CLI_STANDALONE=ON";
#else
std::cout << " -DCCAP_BUILD_CLI_STANDALONE=OFF";
#endif
#else
std::cout << " -DCCAP_BUILD_CLI_STANDALONE=OFF";
#endif
#ifdef CCAP_CLI_WITH_GLFW
std::cout << " -DCCAP_CLI_WITH_GLFW=ON";
#else
std::cout << " -DCCAP_CLI_WITH_GLFW=OFF";
#endif
#ifdef CCAP_CLI_WITH_STB_IMAGE
std::cout << " -DCCAP_CLI_WITH_STB_IMAGE=ON";
#else
std::cout << " -DCCAP_CLI_WITH_STB_IMAGE=OFF";
#endif
std::cout << "\n";
#endif
std::cout << " libccap " << CCAP_VERSION_STRING << "\n";
#ifdef CCAP_CLI_STATIC_RUNTIME
#if CCAP_CLI_STATIC_RUNTIME
std::cout << " runtime static\n";
#else
std::cout << " runtime dynamic\n";
#endif
#else
std::cout << " runtime dynamic\n";
#endif
std::cout << "Simple and efficient camera capture tool\n";
}
void printUsage(const char* programName) {
std::cout << "usage: " << programName << " [options]\n"
<< "\n"
<< "Print help / information / capabilities:\n"
<< " -h, --help show this help message\n"
<< " -v, --version show version information\n"
<< " -l, --list-devices list all available camera devices\n"
<< " -I, --device-info index show detailed info for device at index\n"
<< "\n"
<< "Global options:\n"
<< " --verbose enable verbose logging (shows all messages)\n"
<< " -q, --quiet quiet mode (only show errors, equivalent to log level Error)\n"
<< " --json emit structured JSON output for supported commands\n"
<< " --schema-version version schema version for JSON output (default: 1.0)\n"
<< " --timeout seconds program timeout (auto-exit after N seconds)\n"
<< " --timeout-exit-code code exit code when timeout occurs (default: 0)\n"
<< "\n"
<< "Input options:\n"
<< " -i, --input source input source: video file, device index, or device name\n"
<< " -d, --device index|name select camera device by index or name (default: 0)\n"
<< "\n"
<< "Capture options (camera mode):\n"
<< " -w, --width width set capture width (default: " << DEFAULT_WIDTH << ")\n"
<< " -H, --height height set capture height (default: " << DEFAULT_HEIGHT << ")\n"
<< " -f, --fps fps set frame rate (default: " << DEFAULT_FPS << ")\n"
<< " Camera mode: sets camera capture frame rate\n"
<< " Video mode: calculates playback speed from video's native fps\n"
<< " Note: Cannot be used with --speed\n"
<< " -c, --count count number of frames to capture, then exit\n"
<< " -t, --grab-timeout ms timeout for grabbing a single frame (default: " << DEFAULT_TIMEOUT_MS << ")\n"
<< " --format, --output-format output pixel format: rgb24, bgr24, rgba32, bgra32, nv12, i420, yuyv, uyvy\n"
<< " --internal-format format internal pixel format (camera native format, camera mode only)\n"
<< "\n"
<< "Save options:\n"
<< " -o, --output dir output directory for captured images\n"
<< " -s, --save save captured frames to output directory\n"
<< " --save-yuv save YUV frames directly (auto-enables --save)\n"
<< " --save-format format image format: jpg, png, bmp (auto-enables --save)\n"
<< " --save-jpg save as JPEG format (shortcut for --save-format jpg)\n"
<< " --save-png save as PNG format (shortcut for --save-format png)\n"
<< " --save-bmp save as BMP format (shortcut for --save-format bmp)\n"
#ifdef CCAP_CLI_WITH_STB_IMAGE
<< " --image-format format image format: jpg, png, bmp (default: jpg)\n"
<< " --jpeg-quality quality JPEG quality 1-100 (default: 90, only for jpg)\n"
#else
<< " --image-format format image format: bmp (jpg/png require CCAP_CLI_WITH_STB_IMAGE=ON)\n"
#endif
<< "\n";
#ifdef CCAP_CLI_WITH_GLFW
std::cout << "Preview options:\n"
<< " -p, --preview enable window preview\n"
<< " --preview-only same as --preview (kept for compatibility)\n"
<< " Camera preview requests 1920x1080 by default when -w/-H are omitted\n"
<< "\n";
#endif
std::cout << "Video playback options:\n"
<< " --loop[=N] loop video playback (N times, omit for infinite)\n"
<< " Note: --loop and -c are mutually exclusive\n"
<< " --speed speed playback speed multiplier\n"
<< " 0.0 = no frame rate control (process as fast as possible)\n"
<< " 1.0 = normal speed (match video's original frame rate)\n"
<< " >1.0 = speed up (e.g., 2.0 = 2x speed)\n"
<< " <1.0 = slow down (e.g., 0.5 = half speed)\n"
<< " Default: 0.0 for capture mode, 1.0 for preview mode\n"
<< " Note: Cannot be used with -f/--fps\n"
<< "\n";
std::cout << "Format conversion options:\n"
<< " --convert input convert YUV file to image\n"
<< " --yuv-format format YUV format: nv12, nv12f, i420, i420f, yuyv, yuyvf, uyvy, uyvyf\n"
<< " --yuv-width width width of YUV input\n"
<< " --yuv-height height height of YUV input\n"
<< " --convert-output file output file for conversion\n"
<< "\n";
#if defined(_WIN32) || defined(_WIN64)
std::cout << "Windows camera backend options:\n"
<< " --camera-backend backend camera backend: auto, msmf, dshow\n"
<< " --auto same as --camera-backend auto\n"
<< " --msmf same as --camera-backend msmf\n"
<< " --dshow same as --camera-backend dshow\n"
<< " environment: CCAP_WINDOWS_BACKEND=auto|msmf|dshow (process-wide override)\n"
<< "\n";
#endif
std::cout << "Examples:\n"
<< " " << programName << " --list-devices\n"
<< " " << programName << " --device-info 0\n"
<< " " << programName << " -d 0 # print camera info\n"
<< " " << programName << " -i /path/to/video.mp4 # print video info\n"
<< " " << programName << " -d 0 -c 10 -o ./captures # capture 10 frames\n"
<< " " << programName << " -d 0 -c 10 -o ./captures --save-jpg # capture and save as JPEG\n"
<< " " << programName << " -i /path/to/video.mp4 -c 30 -o ./frames # extract 30 frames from video\n"
<< " " << programName << " -i /path/to/video.mp4 --loop # loop video playback\n"
<< " " << programName << " --timeout 60 -d 0 --preview # preview for 60 seconds then exit\n";
#if defined(_WIN32) || defined(_WIN64)
std::cout << " " << programName << " --list-devices --msmf # list devices through Media Foundation\n"
<< " " << programName << " -d 0 -c 1 --dshow # capture with DirectShow explicitly\n";
#endif
#ifdef CCAP_CLI_WITH_GLFW
std::cout << " " << programName << " -d 0 --preview\n"
<< " " << programName << " -i /path/to/video.mp4 --preview\n";
#endif
#ifdef CCAP_CLI_WITH_STB_IMAGE
std::cout << " " << programName << " --convert input.yuv --yuv-format nv12 --yuv-width 1920 --yuv-height 1080 --convert-output output.jpg --image-format jpg\n";
#else
std::cout << " " << programName << " --convert input.yuv --yuv-format nv12 --yuv-width 1920 --yuv-height 1080 --convert-output output.bmp\n";
#endif
}
PixelFormatInfo parsePixelFormat(const std::string& formatStr) {
PixelFormatInfo info;
std::string lower = formatStr;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower == "rgb24") {
info.format = ccap::PixelFormat::RGB24;
info.isYuv = false;
} else if (lower == "bgr24") {
info.format = ccap::PixelFormat::BGR24;
info.isYuv = false;
} else if (lower == "rgba32") {
info.format = ccap::PixelFormat::RGBA32;
info.isYuv = false;
} else if (lower == "bgra32") {
info.format = ccap::PixelFormat::BGRA32;
info.isYuv = false;
} else if (lower == "nv12") {
info.format = ccap::PixelFormat::NV12;
info.isYuv = true;
} else if (lower == "nv12f") {
info.format = ccap::PixelFormat::NV12f;
info.isYuv = true;
} else if (lower == "i420") {
info.format = ccap::PixelFormat::I420;
info.isYuv = true;
} else if (lower == "i420f") {
info.format = ccap::PixelFormat::I420f;
info.isYuv = true;
} else if (lower == "yuyv") {
info.format = ccap::PixelFormat::YUYV;
info.isYuv = true;
} else if (lower == "yuyvf") {
info.format = ccap::PixelFormat::YUYVf;
info.isYuv = true;
} else if (lower == "uyvy") {
info.format = ccap::PixelFormat::UYVY;
info.isYuv = true;
} else if (lower == "uyvyf") {
info.format = ccap::PixelFormat::UYVYf;
info.isYuv = true;
} else {
info.format = ccap::PixelFormat::Unknown;
info.isYuv = false;
}
return info;
}
ImageFormat parseImageFormat(const std::string& formatStr) {
std::string lower = formatStr;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower == "jpg" || lower == "jpeg") {
return ImageFormat::JPG;
} else if (lower == "png") {
return ImageFormat::PNG;
} else if (lower == "bmp") {
return ImageFormat::BMP;
}
// Default to JPG if format is not recognized
return ImageFormat::JPG;
}
void parseInputSource(const std::string& inputStr, CLIOptions& opts) {
// Check if input is a pure number (device index)
bool isNumber = true;
for (char c : inputStr) {
if (!std::isdigit(c) && c != '-') {
isNumber = false;
break;
}
}
if (isNumber) {
// Input is a device index
opts.deviceIndex = std::atoi(inputStr.c_str());
opts.deviceName.clear();
opts.videoFilePath.clear();
return;
}
// Check if input is an existing file (video file)
std::filesystem::path inputPath(inputStr);
std::error_code ec;
if (std::filesystem::exists(inputPath, ec) && std::filesystem::is_regular_file(inputPath, ec)) {
// Input is a video file
opts.videoFilePath = inputStr;
opts.deviceName.clear();
return;
}
// Input is a device name (not a file, not a number)
opts.deviceName = inputStr;
opts.videoFilePath.clear();
}
CLIOptions parseArgs(int argc, char* argv[]) {
CLIOptions opts;
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "-h" || arg == "--help") {
opts.showHelp = true;
} else if (arg == "-v" || arg == "--version") {
opts.showVersion = true;
} else if (arg == "--verbose") {
opts.verbose = true;
} else if (arg == "--json") {
opts.jsonOutput = true;
} else if (arg == "--schema-version") {
if (i + 1 >= argc || argv[i + 1][0] == '-') {
std::cerr << "Error: --schema-version requires a value.\n\n";
printUsage(argv[0]);
std::exit(1);
}
opts.schemaVersion = argv[++i];
} else if (arg == "-l" || arg == "--list-devices") {
opts.listDevices = true;
} else if (arg == "-I" || arg == "--device-info") {
opts.showDeviceInfo = true;
if (i + 1 < argc && argv[i + 1][0] != '-') {
opts.deviceInfoIndex = std::atoi(argv[++i]);
}
} else if (arg == "-i" || arg == "--input") {
if (i + 1 < argc) {
std::string inputStr = argv[++i];
opts.inputSource = inputStr;
parseInputSource(inputStr, opts);
}
} else if (arg == "-d" || arg == "--device") {
if (i + 1 < argc) {
const char* val = argv[++i];
// Check if it's a number (handles positive and negative integers safely)
if (std::isdigit(val[0]) || (val[0] == '-' && val[1] != '\0' && std::isdigit(val[1]))) {
opts.deviceIndex = std::atoi(val);
} else {
opts.deviceName = val;
}
}
} else if (arg == "--camera-backend") {
if (i + 1 >= argc) {
printWindowsBackendOptionErrorAndExit(argv[0], "--camera-backend requires a value.");
}
setWindowsCameraBackend(opts, argv[0], argv[++i]);
} else if (arg == "--auto") {
setWindowsCameraBackend(opts, argv[0], "auto");
} else if (arg == "--msmf") {
setWindowsCameraBackend(opts, argv[0], "msmf");
} else if (arg == "--dshow") {
setWindowsCameraBackend(opts, argv[0], "dshow");
} else if (arg == "--video") {
if (i + 1 < argc) {
opts.videoFilePath = argv[++i];
}
} else if (arg == "-w" || arg == "--width") {
if (i + 1 < argc) {
opts.width = std::atoi(argv[++i]);
opts.widthSpecified = true;
}
} else if (arg == "-H" || arg == "--height") {
if (i + 1 < argc) {
opts.height = std::atoi(argv[++i]);
opts.heightSpecified = true;
}
} else if (arg == "-f" || arg == "--fps") {
if (i + 1 < argc) {
opts.fps = std::atof(argv[++i]);
opts.fpsSpecified = true;
}
} else if (arg == "-c" || arg == "--count") {
if (i + 1 < argc) {
opts.captureCount = std::atoi(argv[++i]);
opts.captureCountSpecified = true;
}
} else if (arg == "-t" || arg == "--grab-timeout") {
if (i + 1 < argc) {
opts.grabTimeoutMs = std::atoi(argv[++i]);
}
} else if (arg == "--timeout") {
if (i + 1 < argc) {
opts.timeoutSeconds = std::atoi(argv[++i]);
}
} else if (arg == "--timeout-exit-code") {
if (i + 1 < argc) {
opts.timeoutExitCode = std::atoi(argv[++i]);
}
} else if (arg == "-o" || arg == "--output") {
if (i + 1 < argc) {
opts.outputDir = argv[++i];
}
} else if (arg == "-s" || arg == "--save") {
opts.saveFrames = true;
opts.saveFramesSpecified = true;
} else if (arg == "--format" || arg == "--output-format") {
if (i + 1 < argc) {
auto info = parsePixelFormat(argv[++i]);
opts.outputFormat = info.format;
}
} else if (arg == "--internal-format") {
if (i + 1 < argc) {
auto info = parsePixelFormat(argv[++i]);
opts.internalFormat = info.format;
}
} else if (arg == "--save-yuv") {
opts.saveYuv = true;
opts.saveFrames = true;
// Auto-set internal format to NV12 if not specified
if (opts.internalFormat == ccap::PixelFormat::Unknown) {
opts.internalFormat = ccap::PixelFormat::NV12;
}
} else if (arg == "--save-format") {
if (i + 1 < argc) {
opts.imageFormat = parseImageFormat(argv[++i]);
opts.saveFrames = true;
}
} else if (arg == "--save-jpg" || arg == "--save-jpeg") {
opts.imageFormat = ImageFormat::JPG;
opts.saveFrames = true;
} else if (arg == "--save-png") {
opts.imageFormat = ImageFormat::PNG;
opts.saveFrames = true;
} else if (arg == "--save-bmp") {
opts.imageFormat = ImageFormat::BMP;
opts.saveFrames = true;
} else if (arg == "--image-format") {
if (i + 1 < argc) {
opts.imageFormat = parseImageFormat(argv[++i]);
}
} else if (arg == "--jpeg-quality") {
if (i + 1 < argc) {
int quality = std::atoi(argv[++i]);
opts.jpegQuality = std::max(1, std::min(100, quality)); // Clamp to 1-100
}
} else if (arg == "--loop" || arg.rfind("--loop=", 0) == 0) {
opts.enableLoop = true;
if (arg.rfind("--loop=", 0) == 0) {
// Parse loop count from --loop=N
std::string countStr = arg.substr(7);
opts.loopCount = std::atoi(countStr.c_str());
} else if (i + 1 < argc && argv[i + 1][0] != '-') {
// Check if next arg is a number (optional loop count)
const char* nextArg = argv[i + 1];
bool isNum = true;
for (const char* p = nextArg; *p; ++p) {
if (!std::isdigit(*p)) {
isNum = false;
break;
}
}
if (isNum) {
opts.loopCount = std::atoi(argv[++i]);
}
}
// loopCount = 0 means infinite loop
} else if (arg == "--speed") {
if (i + 1 < argc) {
opts.playbackSpeed = std::atof(argv[++i]);
opts.playbackSpeedSpecified = true;
}
} else if (arg == "-p" || arg == "--preview") {
opts.enablePreview = true;
} else if (arg == "--preview-only") {
opts.enablePreview = true;
opts.previewOnly = true;
} else if (arg == "--convert") {
if (i + 1 < argc) {
opts.convertInput = argv[++i];
}
} else if (arg == "--yuv-format") {
if (i + 1 < argc) {
auto info = parsePixelFormat(argv[++i]);
opts.yuvFormat = info.format;
}
} else if (arg == "--yuv-width") {
if (i + 1 < argc) {
opts.yuvWidth = std::atoi(argv[++i]);
}
} else if (arg == "--yuv-height") {
if (i + 1 < argc) {
opts.yuvHeight = std::atoi(argv[++i]);
}
} else if (arg == "--convert-output") {
if (i + 1 < argc) {
opts.convertOutput = argv[++i];
}
} else {
std::cerr << "Error: Unknown option: " << arg << "\n\n";
printUsage(argv[0]);
std::exit(1);
}
}
// Post-processing: auto-enable save mode if -o is specified without preview
if (!opts.outputDir.empty() && !opts.enablePreview && !opts.saveFramesSpecified) {
opts.saveFrames = true;
}
// Post-processing: if -o is specified with save options, enable save
if (!opts.outputDir.empty() && (opts.saveYuv || opts.saveFramesSpecified)) {
opts.saveFrames = true;
}
if (opts.enablePreview && opts.videoFilePath.empty() && !opts.widthSpecified && !opts.heightSpecified) {
opts.width = DEFAULT_PREVIEW_WIDTH;
opts.height = DEFAULT_PREVIEW_HEIGHT;
}
return opts;
}
} // namespace ccap_cli