当屏幕分辨率高于 1920x1080 时捕获视频

Posted

技术标签:

【中文标题】当屏幕分辨率高于 1920x1080 时捕获视频【英文标题】:Capturing video when screen resolution is above 1920x1080 【发布时间】:2019-08-23 23:05:09 【问题描述】:

我正在尝试编写一个使用Media Foundation 的 Win32 c++ 程序,用于捕获桌面的mp4 视频。我发现在屏幕分辨率超过 1920x1080 的 PC 上,捕获失败。失败是在调用 SetInputMediaType() 之后返回错误 0xc00d36b4(为媒体类型指定的数据无效、不一致或不受此对象支持)

有什么办法吗?

HRESULT InitializeDirect3D9(IDirect3DDevice9** ppDevice, IDirect3DSurface9** ppSurface, UINT32& uiWidth, UINT32& uiHeight) 


IDirect3D9* d3d = NULL;

d3d = Direct3DCreate9(D3D_SDK_VERSION);

if (d3d == NULL)
    return E_POINTER;

D3DDISPLAYMODE mode;
HRESULT hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);

if (FAILED(hr))

    SafeRelease(&d3d);
    return hr;


D3DPRESENT_PARAMETERS parameters =  0 ;

parameters.Windowed = TRUE;
parameters.BackBufferCount = 1;
uiHeight = parameters.BackBufferHeight = mode.Height;
uiWidth = parameters.BackBufferWidth = mode.Width;
parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
parameters.hDeviceWindow = NULL;

hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &parameters, ppDevice);

if (FAILED(hr))

    SafeRelease(&d3d);
    return hr;


hr = (*ppDevice)->CreateOffscreenPlainSurface(mode.Width, mode.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, ppSurface, nullptr);

SafeRelease(&d3d);

return hr;


HRESULT SGCaptureDecktopVideo::InitializeSinkWriter(std::wstring VideoPath, IMFSinkWriter **ppWriter, DWORD *pStreamIndex, const UINT32 uiWidth, const UINT32 uiHeight)

*ppWriter = NULL;
*pStreamIndex = NULL;

IMFSinkWriter   *pSinkWriter = NULL;
IMFMediaType    *pMediaTypeOut = NULL;
IMFMediaType    *pMediaTypeIn = NULL;
DWORD           streamIndex;

HRESULT hr = MFCreateSinkWriterFromURL(VideoPath.c_str(), NULL, NULL, &pSinkWriter);

// Set the output media type.
if (SUCCEEDED(hr))

    hr = MFCreateMediaType(&pMediaTypeOut);

if (SUCCEEDED(hr))

    hr = pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);

if (SUCCEEDED(hr))

    hr = pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, myVideoRecorder.CaptureParams.VIDEO_ENCODING_FORMAT);

�
void vidThread(vidThreadArgs *args)


WriteLogFile(L"Thread started");
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

if (SUCCEEDED(hr))

    WriteLogFile(L"CoInitializeEx - success");

    hr = MFStartup(MF_VERSION);

    if (SUCCEEDED(hr))
    
        WriteLogFile(L"MFStartup - success");

        UINT32 uiWidth = 0;
        UINT32 uiHeight = 0;

        IDirect3DDevice9* pDevice = NULL;
        IDirect3DSurface9* pSurface = NULL;


        hr = myVideoRecorder.InitializeDirect3D9(&pDevice, &pSurface, uiWidth, uiHeight);

        if (SUCCEEDED(hr))
        
            WriteLogFile(L"InitializeDirect3D9 - success");

            IMFSinkWriter *pSinkWriter = NULL;
            DWORD stream;

            hr = myVideoRecorder.InitializeSinkWriter(myVideoRecorder.CaptureParams.VideoPath, &pSinkWriter, &stream, uiWidth, uiHeight);

            if (SUCCEEDED(hr))
            

                WriteLogFile(L"InitializeSinkWriter - success");

                LONGLONG rtStart = 0;
                unsigned long long int frameCounter = 0;

                while (true)
                

                    hr = myVideoRecorder.WriteFrame(pDevice, pSurface, pSinkWriter, stream, rtStart, uiWidth, uiHeight);
                    frameCounter++;

                    if (FAILED(hr))
                    

                        WriteLogFile(L"Internal error %d Frame %d\n",
                            GetLastError(), frameCounter);

                        break;
                    

                    rtStart += myVideoRecorder.CaptureParams.VIDEO_FRAME_DURATION;
                    if (bStopRecording)
                    
                        WriteLogFile(L"Finalizing recording of file %s. Frame %llu",
                            myVideoRecorder.CaptureParams.VideoPath.c_str(),
                            frameCounter);

                        break;
                    
                


            
            else
            

                DWORD error = GetLastError();
                WriteLogFile(L"failed here. Error %d\n", error);
            

            if (SUCCEEDED(hr))
            
                hr = pSinkWriter->Finalize();
            

            SafeRelease(&pSinkWriter);
        
        else
        
            WriteLogFile(L"InitializeDirect3D9 - failed Error %d",GetLastError());
            result = STATUS_ERROR_INIT_DIRECT3D9;
        
        SafeRelease(&pDevice);
        SafeRelease(&pSurface);
        MFShutdown();
        WriteLogFile(L"MFShutDown");
    
    else
    
        result = STATUS_ERROR_MFSSTARTUP;
        WriteLogFile(L"MFStartup failed");

    
    CoUninitialize();

else

    result = STATUS_ERROR_COINITILIZE;
    WriteLogFile(L"CoInitilize - failed Error %d", GetLastError());




HRESULT WriteFrame(IDirect3DDevice9* pDevice, IDirect3DSurface9*
pSurface, IMFSinkWriter* pWriter, DWORD streamIndex, const
LONGLONG& rtStart, const UINT32 uiWidth, const UINT32 uiHeight)

    HRESULT hr = pDevice->GetFrontBufferData(0, pSurface);

    if(FAILED(hr))
        return hr;
    

    D3DLOCKED_RECT rc;
    hr = pSurface->LockRect(&rc, NULL, 0);

    if(FAILED(hr))
        return hr;
    

    IMFSample *pSample = NULL;
    IMFMediaBuffer *pBuffer = NULL;

    const LONG cbWidth = 4 * uiWidth;
    const DWORD cbBuffer = cbWidth * uiHeight;

    BYTE *pData = NULL;

    // Create a new memory buffer.
    hr = MFCreateMemoryBuffer(cbBuffer, &pBuffer);

    // Lock the buffer and copy the video frame to the buffer.
    if(SUCCEEDED(hr))
        hr = pBuffer->Lock(&pData, NULL, NULL);
    

    if(SUCCEEDED(hr))

#ifdef REVERSE_IMAGE
        for(int i = 0, j = uiHeight - 1; i < uiHeight; i++, j--)
            for(int k = 0; k < cbWidth; k++)
                    pData[(i * cbWidth) + k] = ((BYTE*)rc.pBits)[(j *
cbWidth) + k];
#else
        hr = MFCopyImage(pData, cbWidth, (BYTE*)rc.pBits, rc.Pitch,
cbWidth, uiHeight);
#endif
    

    if(pBuffer)
        pBuffer->Unlock();
    

    // Set the data length of the buffer.
    if(SUCCEEDED(hr))
        hr = pBuffer->SetCurrentLength(cbBuffer);
    

    // Create a media sample and add the buffer to the sample.
    if(SUCCEEDED(hr))
        hr = MFCreateSample(&pSample);
    

    if(SUCCEEDED(hr))
        hr = pSample->AddBuffer(pBuffer);
    

    // Set the time stamp and the duration.
    if(SUCCEEDED(hr))
        hr = pSample->SetSampleTime(rtStart);
    

    if(SUCCEEDED(hr))
        hr = pSample->SetSampleDuration(VIDEO_FRAME_DURATION);
    

    // Send the sample to the Sink Writer.
    if(SUCCEEDED(hr))
        hr = pWriter->WriteSample(streamIndex, pSample);
    

    hr = pSurface->UnlockRect();

    SafeRelease(&pSample);
    SafeRelease(&pBuffer);
    return hr;

代码在 5 台不同的机器上进行了测试,全部使用 Windows 10 x64,发现如果我将 uiWidthuiHeight 的值存储在日志文件中,只需在以下行之前:

hr = MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, uiWidth, uiHeight);

当它们的值低于 1220 和 1080 时,代码可以完美运行,并且当这些值中的一个(或两个)超过 1220x1080 时,它们会获得 0 大小的视频文件。

更新: 我也在发布 InitializeSinkWriter()。在第 17 步,SetInputMediaType() 失败并出现错误 c00d36b4。

HRESULT SGCaptureDecktopVideo::InitializeSinkWriter(std::wstring VideoPath, IMFSinkWriter **ppWriter, DWORD *pStreamIndex, const UINT32 uiWidth, const UINT32 uiHeight)

    *ppWriter = NULL;
    *pStreamIndex = NULL;

    IMFSinkWriter   *pSinkWriter = NULL;
    IMFMediaType    *pMediaTypeOut = NULL;
    IMFMediaType    *pMediaTypeIn = NULL;
    DWORD           streamIndex;

    // Step 1 - MFCreateSinkWriterFromURL
    HRESULT hr = MFCreateSinkWriterFromURL(VideoPath.c_str(), NULL, NULL, &pSinkWriter);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 1 - MFCreateSinkWriterFromURL() failed. %x", hr);
        goto cleanup;
    

    // Step 2 - MFCreateMediaType
    hr = MFCreateMediaType(&pMediaTypeOut);
    if(SUCCEEDED(hr)==0)
    
        WriteLogFile(L"Step 2 - MFCreateMediaType() failed. %x", hr);
        goto cleanup;
    

    // Step 3 - SetGUID (MF_MT_MAJOR_TYPE)
    hr = pMediaTypeOut->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
    if(SUCCEEDED(hr)==0)
    
        WriteLogFile(L"Step 3 - SetGUID (MF_MT_MAJOR_TYPE) failed. %x", hr);
        goto cleanup;
    

    // Step 4 - SetGUID (MF_MT_SUBTYPE)
    hr = pMediaTypeOut->SetGUID(MF_MT_SUBTYPE, myVideoRecorder.CaptureParams.VIDEO_ENCODING_FORMAT);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 4 - SetGUID (MF_MT_SUBTYPE) failed. %x", hr);
        goto cleanup;
    

    // Step 5 - SetUINT32 (MF_MT_AVG_BITRATE)
    hr = pMediaTypeOut->SetUINT32(MF_MT_AVG_BITRATE, myVideoRecorder.CaptureParams.VIDEO_BIT_RATE);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 5 - SetUINT32 (MF_MT_AVG_BITRATE) failed. %x", hr);
        goto cleanup;
    

    // Step 6 - SetUINT32(MF_MT_INTERLACE_MODE)
    hr = pMediaTypeOut->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 6 - SetUINT32(MF_MT_INTERLACE_MODE) failed. %x", hr);
        goto cleanup;
    

    // Step 7 - MFSetAttributeSize
    WriteLogFile(L"w %d h %d",uiWidth, uiHeight);
    hr = MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, uiWidth, uiHeight);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 7 - MFSetAttributeSize() failed. %x", hr);
        goto cleanup;
    

    // Step 8 - MFSetAttributeRatio - MF_MT_FRAME_RATE
    hr = MFSetAttributeRatio(pMediaTypeOut, MF_MT_FRAME_RATE, myVideoRecorder.CaptureParams.VIDEO_FPS, 1);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 8 - MFSetAttributeRatio(MF_MT_FRAME_RATE) failed. %x", hr);
        goto cleanup;
    

    // Step 9 - MFSetAttributeRatio - MF_MT_PIXEL_ASPECT_RATIO
    hr = MFSetAttributeRatio(pMediaTypeOut, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 9 - MFSetAttributeRatio(MF_MT_PIXEL_ASPECT_RATIO) failed. %x", hr);
        goto cleanup;
    

    // Step 10 - AddStream
    hr = pSinkWriter->AddStream(pMediaTypeOut, &streamIndex);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 10 - AddStream() failed. %x", hr);
        goto cleanup;
    

    // Step 11 - MFCreateMediaType
    hr = MFCreateMediaType(&pMediaTypeIn);
    if (SUCCEEDED(hr) == 0 || pMediaTypeIn==NULL)
    
        WriteLogFile(L"Step 11 - MFCreateMediaType() failed. %x", hr);
        goto cleanup;
    

    // Step 12 - SetGUID (MF_MT_MAJOR_TYPE)
    hr = pMediaTypeIn->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 12 - SetGUID (MF_MT_MAJOR_TYPE) failed. %x", hr);
        goto cleanup;
    

    // Step 13 - SetGUID (MF_MT_SUBTYPE)
    hr = pMediaTypeIn->SetGUID(MF_MT_SUBTYPE, myVideoRecorder.CaptureParams.VIDEO_INPUT_FORMAT);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 13 - SetGUID (MF_MT_SUBTYPE) failed. %x", hr);
        goto cleanup;
    

    // Step 13 - SetUINT32 (MF_MT_INTERLACE_MODE)
    hr = pMediaTypeIn->SetUINT32(MF_MT_INTERLACE_MODE, MFVideoInterlace_Progressive);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"SetUINT32 (MF_MT_INTERLACE_MODE). %x", hr);
        goto cleanup;
    

    // Step 14 - SMFSetAttributeSize
    hr = MFSetAttributeSize(pMediaTypeIn, MF_MT_FRAME_SIZE, uiWidth, uiHeight);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 14 - MFSetAttributeSize() failed. %x", hr);
        goto cleanup;
    

    // Step 15 - MFSetAttributeRatio(MF_MT_FRAME_RATE)
    hr = MFSetAttributeRatio(pMediaTypeIn, MF_MT_FRAME_RATE, myVideoRecorder.CaptureParams.VIDEO_FPS, 1);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 15 - MFSetAttributeRatio(MF_MT_FRAME_RATE) failed. %x", hr);
        goto cleanup;
    

    // Step 16 - MFSetAttributeRatio(MF_MT_PIXEL_ASPECT_RATIO)
    hr = MFSetAttributeRatio(pMediaTypeIn, MF_MT_PIXEL_ASPECT_RATIO, 1, 1);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 16 - MFSetAttributeRatio(MF_MT_PIXEL_ASPECT_RATIO) failed. %x", hr);
        goto cleanup;
    
    // Step 17 - SetInputMediaType
    hr = pSinkWriter->SetInputMediaType(streamIndex, pMediaTypeIn, NULL);
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 17 - SetInputMediaType() failed. %x", hr);
        goto cleanup;
    

    // Step 18 - BeginWriting() Tell the sink writer to start accepting data.
    hr = pSinkWriter->BeginWriting();
    if (SUCCEEDED(hr) == 0)
    
        WriteLogFile(L"Step 18 - BeginWriting() failed. %x", hr);
        goto cleanup;
    
    // Return the pointer to the caller.
    WriteLogFile(L"Success");

    *ppWriter = pSinkWriter;
    (*ppWriter)->AddRef();
    *pStreamIndex = streamIndex;

cleanup:;
    SafeRelease(&pSinkWriter);
    SafeRelease(&pMediaTypeOut);
    SafeRelease(&pMediaTypeIn);
    return hr;

在分辨率更高的机器上,这部分仍然有错误。 CreateDevice() 失败,错误为 0x7E。

D3DDISPLAYMODE mode;

HRESULT hr = d3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &mode);

if (FAILED(hr))

    WriteLogFile(L"GetAdapterDisplayMode() failed. Error =%x", GetLastError());
    SafeRelease(&d3d);
    return hr;

WriteLogFile(L"Fetched mode. Width=%d Height=%d", mode.Width, mode.Height);
D3DPRESENT_PARAMETERS parameters =  0 ;

parameters.Windowed = TRUE;
parameters.BackBufferCount = 1;
uiHeight = parameters.BackBufferHeight = mode.Height;
uiWidth = parameters.BackBufferWidth = mode.Width;
parameters.SwapEffect = D3DSWAPEFFECT_DISCARD;
parameters.hDeviceWindow = NULL;

hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &parameters, ppDevice);

if (FAILED(hr))

    WriteLogFile(L"CreateDevice failed. Error =%x",GetLastError());
    SafeRelease(&d3d);
    return hr;

【问题讨论】:

您能否提供更多关于如何(或什么)失败的信息?你得到什么错误信息?能否提供一些示例代码? @J.AntonioPerez 我用部分代码更新了我的问题。基本上,当屏幕分辨率较高时,会创建大小为 0 的视频文件。 这是非常有用的信息!你能把关于大小为零的视频文件的部分介绍给问题本身吗? @J.AntonioPerez 我可以,但是问题是 WriteFrame 失败,所以由于我打开目标视频文件进行写入,它的大小为 0,因为没有写入数据 你有没有在调试器中运行它来找到它失败的地方? 【参考方案1】:

您的瓶颈是视频编码器中的分辨率支持。

视频编码器通常对分辨率有限制,但限制会有所不同。广泛支持的最大安全分辨率为 1920x1088。在您的情况下,您可能正在使用 Microsoft 的软件编码器。我想过去一段时间MSDN documetnation 直接提到了最大分辨率,但是目前那里不存在此信息。

没有办法可靠地确定支持的最大分辨率,而且它看起来像是一个 API 设计缺陷。

作为记录,Windows 10 版本 1903(2019 年 5 月更新)附带软件 H.264 视频编码器,能够编码至少 3840x2160 视频。

另见:

IMFSinkWriter can't export a large size video of mp4 The max resolution for mp4(h264) encoder

【讨论】:

【参考方案2】:

首先,这段代码的错误处理是有问题的。 If 将在任何失败时简单地跳过操作,但从不实际报告任何失败。几乎可以肯定其中一个 API 调用失败了,此时您需要实际报告错误并显示 hr 的十六进制值(最容易阅读和查找),然后保释。所以你需要修复错误报告并告诉我们哪个 API 失败了,结果是什么。

至于为什么会失败,像 MP4 这样的编解码器具有编码配置文件,并且他们更喜欢使用特定的分辨率或比特率。较高的分辨率可能超出了该配置文件的限制,或者它是 MP4 不支持(或具有配置文件)的非标准分辨率。或者配置文件受到比特率限制,并且请求的更高分辨率超过了可用的比特率。

【讨论】:

查看更新的问题和代码。失败是调用 MFSetAttributeSize()【参考方案3】:

最后发现uiWidthuiHeight不能超过1920x1080,所以在调用MFSetAttributeSize()之前需要调整一下。

INT32 outWidth = uiWidth;
    INT32 outHeight = uiHeight;

    if (uiWidth > 1920 || uiHeight > 1080)
    
        outWidth = 1920;
        outHeight = 1080;
    
hr = MFSetAttributeSize(pMediaTypeOut, MF_MT_FRAME_SIZE, outWidth, outHeight);

这显然解决了分辨率高于 1920x1080 的计算机上的问题。

【讨论】:

以上是关于当屏幕分辨率高于 1920x1080 时捕获视频的主要内容,如果未能解决你的问题,请参考以下文章

视频分辨率、帧率、码率

ubuntu/linux系统知识(24)ubuntu自带的录屏软件

ubuntu/linux系统知识(24)ubuntu自带的录屏软件

手机看视频不是全部能全屏,怎么处理?

Android全面屏(长屏)闪屏背景拉伸问题解决

录制手机屏幕视频 好用的录屏软件推荐