繁体   English   中英

在DirectShow中使用ICapturegraphBuilder

[英]Using ICapturegraphBuilder in DirectShow

我正在研究Direct Show示例,但遇到了问题。 我想将从捕获设备收到的视频多路复用到预览和文件,但是不知道如何。

我用作基础的样本是playcap。 在那里,使用了ICaptureGraphBuilder2 它的文档中包含函数SetOutputFileName ,该函数可能会打开文件输出。

但是,在主动状态下,文件出现在文件系统中,但始终为空,并在应用程序退出时被删除。 我怀疑API的使用错误。

这是我正在使用的代码:

    HRESULT hr;
IBaseFilter *pSrcFilter=NULL;

// Get DirectShow interfaces
hr = GetInterfaces();
if (FAILED(hr))
{
    Msg(TEXT("Failed to get video interfaces!  hr=0x%x"), hr);
    return hr;
}

// Attach the filter graph to the capture graph
hr = g_pCapture->SetFiltergraph(g_pGraph);
if (FAILED(hr))
{
    Msg(TEXT("Failed to set capture filter graph!  hr=0x%x"), hr);
    return hr;
}

// Use the system device enumerator and class enumerator to find
// a video capture/preview device, such as a desktop USB video camera.
hr = FindCaptureDevice(&pSrcFilter);
if (FAILED(hr))
{
    // Don't display a message because FindCaptureDevice will handle it
    return hr;
}

// Add Capture filter to our graph.
hr = g_pGraph->AddFilter(pSrcFilter, L"Video Capture");
if (FAILED(hr))
{
    Msg(TEXT("Couldn't add the capture filter to the graph!  hr=0x%x\r\n\r\n") 
        TEXT("If you have a working video capture device, please make sure\r\n")
        TEXT("that it is connected and is not being used by another application.\r\n\r\n")
        TEXT("The sample will now close."), hr);
    pSrcFilter->Release();
    return hr;
}
IBaseFilter * pBaseFilter;
IFileSinkFilter  *pfSink;
// Attach the file writerto the capture graph
hr = g_pCapture->SetOutputFileName (&MEDIASUBTYPE_Avi,L"output.avi",&pBaseFilter,&pfSink);
if (FAILED(hr))
{
    Msg(TEXT("Failed to set capture filter graph!  hr=0x%x"), hr);
    return hr;
}
// Render the preview pin on the video capture filter
// Use this instead of g_pGraph->RenderFile
hr = g_pCapture->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
                               pSrcFilter, NULL, NULL);
if (FAILED(hr))
{
    Msg(TEXT("Couldn't render the video capture stream.  hr=0x%x\r\n")
        TEXT("The capture device may already be in use by another application.\r\n\r\n")
        TEXT("The sample will now close."), hr);
    pSrcFilter->Release();
    return hr;
}
// Now that the filter has been added to the graph and we have
// rendered its stream, we can release this reference to the filter.
pSrcFilter->Release();

// Set video window style and position
hr = SetupVideoWindow();
if (FAILED(hr))
{
    Msg(TEXT("Couldn't initialize video window!  hr=0x%x"), hr);
    return hr;
}

// Add our graph to the running object table, which will allow
// the GraphEdit application to "spy" on our graph
hr = AddGraphToRot(g_pGraph, &g_dwGraphRegister);
if (FAILED(hr))
{
    Msg(TEXT("Failed to register filter graph with ROT!  hr=0x%x"), hr);
    g_dwGraphRegister = 0;
}
// Start previewing video data
hr = g_pMC->Run();
if (FAILED(hr))
{
    Msg(TEXT("Couldn't run the graph!  hr=0x%x"), hr);
    return hr;
}

// Remember current state
g_psCurrent = Running;

return S_OK;

我应该如何实现我想要的功能?

在上面的代码中,您将NULL作为最后一个参数传递给RenderStream。

RenderStream msdn文档

声明:

如果pSink参数为NULL,则该方法尝试使用默认渲染器。 对于视频,它使用视频渲染器,对于音频,它使用音频渲染器(WaveOut)过滤器。

在以上相同的链接上,请查看代码段:

pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, L"C:\\Example.avi",
    &ppf, &pSink);
pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
    pCaptureFilter, NULL, ppf);

您可能还想看看AmCap

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM