c++ Windows API函数AVIFileOpenW将PAVISSTREAM作为输入,但AVISreamSetFormat将PAVIFILE作为输入

92vpleto  于 2023-01-06  发布在  Windows
关注(0)|答案(1)|浏览(104)

我正在制作一个剪辑软件,剪辑你的屏幕的最后30秒。我试图把结果写入AVI文件,但我遇到了问题。以下是我的代码:

#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;
}

我这样做对吗?我是C++的新手,所以我不确定这是否是我应该做的。

relj7zay

relj7zay1#

你可以使用AVIStreamOpenFromFile函数将avi文件打开为流,这将给予指向PAVISTREAM的指针。因此,没有必要调用AVIFileOpen函数。

相关问题