7 {
8 if (argc != 3) {
9 std::cerr << "Usage: " << argv[0] << " camera index mode\n0 mode for normal mode, 1, for timestamp mode\n";
10 return 1;
11 }
12 int index = std::stoi(argv[1]);
13 int mode = std::stoi(argv[2]);
14
15 if (mode != 0 && mode != 1) {
16 std::cerr << "Invalid mode..\n";
17 return 1;
18 }
19
20 std::cout << "Initializing Camera: " << index << " mode: " << mode << "...\n";
21#ifdef _WIN32
22 cv::VideoCapture cap(index, cv::CAP_DSHOW);
23#else
24 cv::VideoCapture cap(index);
25#endif
26 if (!cap.isOpened()) {
27 std::cerr << "Failed to open camera\n";
28 return 1;
29 }
30 double fps = cap.get(cv::CAP_PROP_FPS);
31 if (fps <= 0)
32 fps = 30.0;
33
34 int width = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
35 int height = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
36
37 std::cout << "Camera settings: " << width << "x" << height << " @ " << fps << "fps\n";
38
40 bool status;
41
42 if (mode == 0)
43 status = writer.
open(
"output.mp4", width, height, fps,
"24");
44 else if (mode == 1)
45 status = writer.
open_ts(
"output.mp4", width, height, fps,
"24");
46
47 if (!status) {
48 std::cerr << "Failed to open file for writing\n";
49 return 1;
50 }
51 cv::namedWindow("Webcam", cv::WINDOW_AUTOSIZE);
52 cv::Mat frame;
55 if (cap.read(frame)) {
56 cv::Mat temp;
57 cv::cvtColor(frame, temp, cv::COLOR_BGR2RGBA);
58 if (mode == 0)
59 writer.
write(temp.ptr());
60 else if (mode == 1)
62
63 cv::imshow("Webcam", frame);
64 }
65 if (cv::waitKey(1) == 27) {
67 }
68 }
70 cap.release();
71 cv::destroyAllWindows();
72 return 0;
73}
FFmpeg-backed RGBA video writer.
bool open(const std::string &filename, int width, int height, float fps, const char *crf)
Open an output file using the legacy CRF string interface.
void write(void *rgba_buffer)
Queue a host RGBA frame for immediate-mode encoding.
bool open_ts(const std::string &filename, int width, int height, float fps, const char *crf)
Open a timestamp-based output stream using the legacy CRF string interface.
void close()
Close the writer and flush pending packets.
void write_ts(void *rgba_buffer)
Queue a host RGBA frame using capture timestamps.
std::atomic< bool > active