C++ OpenCV Program to Record or Screen Capture Video For Certain Time and Save it as MP4 File Full Project For Beginners

  • Post author:
  • Post category:c++
  • Post comments:0 Comments






#include "opencv2/opencv.hpp"
#include <iostream>
#include <ctime>
#include <cstdio>
#include <time.h>
#include <stdio.h>

    using namespace std;
    using namespace cv;

    int main() {

        //////////////////// Added Part
        time_t start, end;
        //////////////////// Added Part
        VideoCapture vcap(0);
        if (!vcap.isOpened()) {
            cout << "Error opening video stream or file" << endl;
            return -1;
        int frame_width = vcap.get(CV_CAP_PROP_FRAME_WIDTH);
        int frame_height = vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
        VideoWriter video("C:\\Users\\lenovo\\Desktop\\OpenCV Webcam Video Record With R Key\\WebcamRecorder\\WebcamRecorder\\data\\MyVideo.avi", CV_FOURCC('M', 'J', 'P', 'G'), 10, Size(frame_width, frame_height), true);

        //////////////////// Added Part
        //////////////////// Added Part

        for (;;) {

            Mat frame;
            vcap >> frame;
            imshow("Frame", frame);
            char c = (char)waitKey(33);
            if (c == 27) break;

            //////////////////// Added Part
            double dif = difftime(end, start);
            printf("Elasped time is %.2lf seconds.", dif);
            if (dif==10)
                std::cout << "DONE" << dif<< std::endl;
            //////////////////// Added Part
        return 0;

Leave a Reply