A DirectX hardware video + audio capture with mixing capabilities and Media Foundation file encoding (Windows 8+)
Article: https://www.codeproject.com/Articles/5256890/ScreenCapture-Single-header-DirectX-library
Features:
int wmain()
{
CoInitializeEx(0, COINIT_APARTMENTTHREADED);
MFStartup(MF_VERSION);
std::cout << "Capturing screen for 10 seconds...";
DESKTOPCAPTUREPARAMS dp;
dp.f = L"capture.mp4";
dp.EndMS = 10000;
DesktopCapture(dp);
std::cout << "Done.\r\n";
return 0;
}
Where the DESKTOPCAPTUREPARAMS is this structure:
struct DESKTOPCAPTUREPARAMS
{
bool HasVideo = 1;
bool HasAudio = 1;
std::vector<std::tuple<std::wstring, std::vector<int>>> AudioFrom;
GUID VIDEO_ENCODING_FORMAT = MFVideoFormat_H264;
GUID AUDIO_ENCODING_FORMAT = MFAudioFormat_MP3;
std::wstring f;
std::function<HRESULT(const BYTE* d, size_t sz)> Streamer;
std::function<HRESULT(const BYTE* d, size_t sz)> Framer;
std::function<void(IMFAttributes* a)> PrepareAttributes;
int fps = 25;
int NumThreads = 0;
int Qu = -1;
int vbrm = 0;
int vbrq = 0;
int BR = 4000;
int NCH = 2;
int SR = 44100;
int ABR = 192;
bool Cursor = true;
RECT rx = { 0,0,0,0 };
HWND hWnd = 0;
IDXGIAdapter1* ad = 0;
UINT nOutput = 0;
unsigned long long StartMS = 0; // 0, none
unsigned long long EndMS = 0; // 0, none
bool MustEnd = false;
bool Pause = false;
};
Where:
The library can also record from a playback device (like your speakers) in loopback. You can specify multiple sources of recording and the library will mix them all into the final audio stream.
If you add the Streamer callback, f can be empty. In this case, you have a MP4 or ASF stream in your callback. If you add the Framer callback, the library captures a screenshot (DWORD array of RGBA (or DXGI_FORMAT_R16G16B16A16_FLOAT for HDR) to the callback) until the callback returns S_OK,in which the library returns.