4#include <opencv2/opencv.hpp>
7int main(
int argc,
char **argv) {
9 std::cerr <<
"Usage: " << argv[0] <<
" camera index mode\n0 mode for normal mode, 1, for timestamp mode\n";
12 int index = std::stoi(argv[1]);
13 int mode = std::stoi(argv[2]);
15 if (mode != 0 && mode != 1) {
16 std::cerr <<
"Invalid mode..\n";
20 std::cout <<
"Initializing Camera: " << index <<
" mode: " << mode <<
"...\n";
22 cv::VideoCapture cap(index, cv::CAP_DSHOW);
24 cv::VideoCapture cap(index);
26 if (!cap.isOpened()) {
27 std::cerr <<
"Failed to open camera\n";
30 double fps = cap.get(cv::CAP_PROP_FPS);
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));
37 std::cout <<
"Camera settings: " << width <<
"x" << height <<
" @ " << fps <<
"fps\n";
43 status = writer.
open(
"output.mp4", width, height, fps,
"24");
45 status = writer.
open_ts(
"output.mp4", width, height, fps,
"24");
48 std::cerr <<
"Failed to open file for writing\n";
51 cv::namedWindow(
"Webcam", cv::WINDOW_AUTOSIZE);
55 if (cap.read(frame)) {
57 cv::cvtColor(frame, temp, cv::COLOR_BGR2RGBA);
59 writer.
write(temp.ptr());
63 cv::imshow(
"Webcam", frame);
65 if (cv::waitKey(1) == 27) {
71 cv::destroyAllWindows();
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.
FFmpeg-based video writer used by MXWrite.
std::atomic< bool > active