|
| 1 | +/** |
| 2 | + * @file 6-record_video.cpp |
| 3 | + * @author wysaid (this@wysaid.org) |
| 4 | + * @brief Example: open a camera and record frames to a video file. |
| 5 | + * @date 2025-05 |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * ./6-record_video [output_path.mp4] |
| 9 | + * |
| 10 | + * Records ~5 seconds (150 frames at 30 fps) from the first available camera |
| 11 | + * and saves them to output_path.mp4 (default: camera_capture.mp4 next to the binary). |
| 12 | + */ |
| 13 | + |
| 14 | +#include "utils/helper.h" |
| 15 | + |
| 16 | +#include <ccap.h> |
| 17 | +#include <cstdio> |
| 18 | +#include <filesystem> |
| 19 | +#include <iostream> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#ifndef CCAP_ENABLE_VIDEO_WRITER |
| 23 | + |
| 24 | +int main() { |
| 25 | + std::cerr << "[WARNING] Video writing is not supported on this platform.\n" |
| 26 | + << "Rebuild with -DCCAP_ENABLE_VIDEO_WRITER=ON (requires Windows or macOS).\n"; |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +#else |
| 31 | + |
| 32 | +#include <ccap_writer.h> |
| 33 | + |
| 34 | +int main(int argc, char** argv) { |
| 35 | + ExampleCommandLine commandLine{}; |
| 36 | + initExampleCommandLine(&commandLine, argc, argv); |
| 37 | + applyExampleCameraBackend(&commandLine); |
| 38 | + |
| 39 | + ccap::setLogLevel(ccap::LogLevel::Verbose); |
| 40 | + |
| 41 | + ccap::setErrorCallback([](ccap::ErrorCode errorCode, std::string_view description) { |
| 42 | + std::cerr << "Error - Code: " << static_cast<int>(errorCode) |
| 43 | + << ", Description: " << description << "\n"; |
| 44 | + }); |
| 45 | + |
| 46 | + // Determine output path |
| 47 | + std::string outputPath; |
| 48 | + if (commandLine.argc >= 2) { |
| 49 | + outputPath = commandLine.argv[1]; |
| 50 | + } else { |
| 51 | + std::string exeDir = commandLine.argv[0]; |
| 52 | + if (auto pos = exeDir.find_last_of("/\\"); pos != std::string::npos && exeDir[0] != '.') { |
| 53 | + exeDir = exeDir.substr(0, pos); |
| 54 | + } else { |
| 55 | + exeDir = std::filesystem::current_path().string(); |
| 56 | + } |
| 57 | + outputPath = exeDir + "/camera_capture.mp4"; |
| 58 | + } |
| 59 | + |
| 60 | + std::cout << "Output video: " << outputPath << "\n"; |
| 61 | + |
| 62 | + // Open camera |
| 63 | + ccap::Provider cameraProvider; |
| 64 | + cameraProvider.set(ccap::PropertyName::Width, 1280); |
| 65 | + cameraProvider.set(ccap::PropertyName::Height, 720); |
| 66 | + cameraProvider.set(ccap::PropertyName::FrameRate, 30.0); |
| 67 | + |
| 68 | + int deviceIndex = selectCamera(cameraProvider, &commandLine); |
| 69 | + cameraProvider.open(deviceIndex, true); |
| 70 | + |
| 71 | + if (!cameraProvider.isStarted()) { |
| 72 | + std::cerr << "Failed to start camera!\n"; |
| 73 | + return -1; |
| 74 | + } |
| 75 | + |
| 76 | + int realWidth = static_cast<int>(cameraProvider.get(ccap::PropertyName::Width)); |
| 77 | + int realHeight = static_cast<int>(cameraProvider.get(ccap::PropertyName::Height)); |
| 78 | + double realFps = cameraProvider.get(ccap::PropertyName::FrameRate); |
| 79 | + |
| 80 | + printf("Camera started: %dx%d @ %.2f fps\n", realWidth, realHeight, realFps); |
| 81 | + |
| 82 | + // Configure and open video writer |
| 83 | + ccap::WriterConfig writerConfig; |
| 84 | + writerConfig.width = static_cast<uint32_t>(realWidth); |
| 85 | + writerConfig.height = static_cast<uint32_t>(realHeight); |
| 86 | + writerConfig.frameRate = realFps > 0.0 ? realFps : 30.0; |
| 87 | + |
| 88 | + ccap::VideoWriter writer; |
| 89 | + if (!writer.open(outputPath, writerConfig)) { |
| 90 | + std::cerr << "Failed to open video writer!\n"; |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + // Record ~5 seconds |
| 95 | + constexpr int kMaxFrames = 150; |
| 96 | + int recorded = 0; |
| 97 | + std::cout << "Recording " << kMaxFrames << " frames (~5 seconds)...\n"; |
| 98 | + |
| 99 | + while (recorded < kMaxFrames) { |
| 100 | + auto frame = cameraProvider.grab(3000); |
| 101 | + if (!frame) { |
| 102 | + std::cerr << "Timeout waiting for camera frame.\n"; |
| 103 | + break; |
| 104 | + } |
| 105 | + |
| 106 | + if (!writer.writeFrame(*frame)) { |
| 107 | + std::cerr << "Failed to write frame " << recorded << "\n"; |
| 108 | + } |
| 109 | + |
| 110 | + if (++recorded % 30 == 0) { |
| 111 | + printf(" Recorded %d/%d frames...\n", recorded, kMaxFrames); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + writer.close(); |
| 116 | + cameraProvider.stop(); |
| 117 | + cameraProvider.close(); |
| 118 | + |
| 119 | + printf("Done! %d frames saved to: %s\n", recorded, outputPath.c_str()); |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +#endif // CCAP_ENABLE_VIDEO_WRITER |
0 commit comments