Windows API Function AVIFileOpenW takes PAVISTREAM as input but AVIStreamSetFormat takes PAVIFILE

117 Views Asked by At

I am making a clipping software which clips the last 30 seconds of your screen. I am trying to write the results to an AVI file, but I am running into issues. Here is my code:

#include <Windows.h>
#include <Vfw.h>
#include <iostream>
#include <vector>

const int BUFFER_SIZE = 30 * 60; // 30 seconds at 60 fps

int main() {
    HMONITOR hMonitor = MonitorFromWindow(NULL, MONITOR_DEFAULTTOPRIMARY);
    MONITORINFO info;
    info.cbSize = sizeof(MONITORINFO);
    GetMonitorInfo(hMonitor, &info);
    int width = info.rcMonitor.right - info.rcMonitor.left;
    int height = info.rcMonitor.bottom - info.rcMonitor.top;

    HDC hDC = GetDC(NULL);

    HBITMAP hBitmap = CreateCompatibleBitmap(hDC, width, height);

    // Create a device context for the bitmap
    HDC hMemDC = CreateCompatibleDC(hDC);
    SelectObject(hMemDC, hBitmap);

    std::vector<HBITMAP> buffer(BUFFER_SIZE);
    int index = 0;

    while (true) {
        BitBlt(hMemDC, 0, 0, width, height, hDC, 0, 0, SRCCOPY);

        buffer[index] = hBitmap;
        index = (index + 1) % BUFFER_SIZE;

        if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) {
            break;
        }
    }

    // PROBLEM HERE:

    PAVISTREAM pStream;
    AVIFileInit();
    AVIFileOpenW(&pStream, L"screen_recording.avi", OF_WRITE | OF_CREATE, NULL); // takes PAVIFILE as first parameter 
    AVIStreamSetFormat(pStream, 0, &hBitmap, sizeof(BITMAPINFOHEADER)); // takes PAVISTREAM as first parameter

    // Write the stored frames to the AVISTREAM object
    for (int i = 0; i < BUFFER_SIZE; i++) {
        AVIStreamWrite(pStream, i, 1, &buffer[i], sizeof(BITMAPINFOHEADER), AVIIF_KEYFRAME, NULL, NULL);
    }

    AVIStreamClose(pStream);
    AVIFileExit();

    ReleaseDC(NULL, hDC);
    DeleteDC(hMemDC);
    DeleteObject(hBitmap);

    return 0;
}

Am I doing this right? I am new to C++, so I am not sure if this is what I should be doing.

1

There are 1 best solutions below

0
Anand Sowmithiran On BEST ANSWER

You can use the function AVIStreamOpenFromFile to open the avi file as stream, that will give the pointer to PAVISTREAM. So, not necessary to call the AVIFileOpen function.