I am trying to record my desktop screen and save it into a video using opencv videoWriter but always end up having a 6kb video which is not even playable.
Here is my code for the same, I'm first creating mat object for the screen and then writing them into the file.
#include "pch.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <opencv2/objdetect/objdetect.hpp>
#include <Windows.h>
using namespace std;
using namespace cv;
int height;
int width;
Mat hwnd2mat()
{
// returning Mat object for screen and working fine as I'm showing it into a window
}
void CaptureScreen()
{
int nScreenWidth = GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hDesktopWnd = GetDesktopWindow();
int key = 0;
string filename = "D:/outcpp.avi";
cv::Size targetSize = cv::Size(320, 240);
VideoWriter video(filename, cv::VideoWriter::fourcc('M', 'J', 'P', 'G'), 10, targetSize);
while (1)
{
Mat frame = hwnd2mat();
cv::Mat image = frame;
cv::Mat dst;
cv::resize(image, dst, targetSize);
if (dst.empty())
break;
video.write(dst);
imshow("Frame", dst);
char c = (char)waitKey(1);
if (c == 27)
break;
}
video.release();
destroyAllWindows();
//readVideo(filename);
}
int main(int argc, char **argv)
{
CaptureScreen();
return 0;
}