From fd96a88fc6ac1e083dfdc5c0a7190a9f65de8012 Mon Sep 17 00:00:00 2001 From: ilya <93557674+Disline1337@users.noreply.github.com> Date: Sun, 2 Apr 2023 08:19:52 +0500 Subject: [PATCH] screen(not)recording --- Overlay/Overlay.vcxproj | 10 ++++-- Overlay/functions/captureScreen.h | 4 ++- Overlay/functions/screenRecorder.h | 54 ++++++++++++++++++++++++++++-- Overlay/main.cpp | 7 ++-- 4 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Overlay/Overlay.vcxproj b/Overlay/Overlay.vcxproj index e06dac2..d581838 100644 --- a/Overlay/Overlay.vcxproj +++ b/Overlay/Overlay.vcxproj @@ -43,7 +43,7 @@ Application true v143 - Unicode + MultiByte Application @@ -73,6 +73,9 @@ true + + true + Level3 @@ -107,9 +110,11 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + stdcpp20 + MultiThreadedDebug - Console + Windows true @@ -122,6 +127,7 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp20 + MultiThreaded Windows diff --git a/Overlay/functions/captureScreen.h b/Overlay/functions/captureScreen.h index d9865a5..6317ce0 100644 --- a/Overlay/functions/captureScreen.h +++ b/Overlay/functions/captureScreen.h @@ -1,8 +1,10 @@ #pragma once #include "addWatermark.h" -void captureScreen(std::string file_screenshot) +inline void captureScreen() { + std::string file_screenshot = "screenshot_" + std::to_string(time(NULL)) + ".bmp"; + HDC hdcScreen; HDC hdcDIB; HBITMAP hbmDIB; diff --git a/Overlay/functions/screenRecorder.h b/Overlay/functions/screenRecorder.h index de25395..9d1504b 100644 --- a/Overlay/functions/screenRecorder.h +++ b/Overlay/functions/screenRecorder.h @@ -1,8 +1,56 @@ #pragma once #include "../Include/Include.h" +#include -void screenRecorder(std::string file_video) +inline void screenRecorder(std::string ffilename) { - std::string command = "ffmpeg -f gdigrab -framerate 60 -i desktop -c:v libx264 -preset ultrafast -tune zerolatency -crf 25 -pix_fmt yuv420p " + ("C:\\Users\\" + (std::string)std::getenv("USERNAME") + "\\Documents\\vos.team\\video\\" + file_video); - system(command.c_str()); + std::string filename = "video.avi"; //temporary fix + + int screen_width = GetSystemMetrics(SM_CXSCREEN); + int screen_height = GetSystemMetrics(SM_CYSCREEN); + cv::Size screen_size(screen_width, screen_height); + + int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); + + // Create video writer object + cv::VideoWriter writer(filename, fourcc, 60.0, screen_size); + + // Check if writer is ready + if (!writer.isOpened()) + { + std::cout << "Error opening video file" << std::endl; + return; + } + + // Capture screen and write frames to video file + while (true) + { + // Capture screen image + HDC hScreen = GetDC(NULL); + HDC hDC = CreateCompatibleDC(hScreen); + HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, screen_width, screen_height); + HGDIOBJ hOld = SelectObject(hDC, hBitmap); + BitBlt(hDC, 0, 0, screen_width, screen_height, hScreen, 0, 0, SRCCOPY); + cv::Mat img = cv::Mat(screen_height, screen_width, CV_8UC4); + //cv::cuda::GpuMat img = cv::cuda::GpuMat(screen_height, screen_width, CV_8UC4); + GetBitmapBits(hBitmap, screen_width * screen_height * 4, img.data); + cv::cvtColor(img, img, cv::COLOR_RGBA2RGB); + + // Write frame to video file + writer.write(img); + + // Release resources + SelectObject(hDC, hOld); + DeleteDC(hDC); + DeleteObject(hBitmap); + ReleaseDC(NULL, hScreen); + + // Check for escape key to exit loop (doesn't work) + if (GetAsyncKeyState(VK_ESCAPE)) + break; + } + + // Release video writer and close windows + writer.release(); + cv::destroyAllWindows(); } \ No newline at end of file diff --git a/Overlay/main.cpp b/Overlay/main.cpp index e44d963..41cd13a 100644 --- a/Overlay/main.cpp +++ b/Overlay/main.cpp @@ -10,7 +10,6 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) } std::string file_screenshot = "screenshot_" + std::to_string(time(NULL)) + ".bmp"; - std::string file_video = "video_" + std::to_string(time(NULL)) + ".mp4"; switch (msg) { @@ -26,11 +25,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) } else if (wParam == 3) { - captureScreen(file_screenshot); + captureScreen(); } else if (wParam == 4) { - screenRecorder(file_video); + std::thread t(screenRecorder, file_screenshot); + t.join(); } break; case WM_PAINT: @@ -104,6 +104,5 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine UnregisterHotKey(hwnd, 3); UnregisterHotKey(hwnd, 4); - exit(0); return Msg.wParam; } \ No newline at end of file