当前位置: 代码迷 >> 多媒体/流媒体开发 >> directshow 采集保存为avi文件的有关问题,请帮忙。
  详细解决方案

directshow 采集保存为avi文件的有关问题,请帮忙。

热度:3361   发布时间:2013-02-26 00:00:00.0
directshow 采集保存为avi文件的问题,请帮忙。。。。。。
独立的两个采集设备,一个视频,一个音频,想将采集到的音频和视频信息保存为一个avi文件。(图片如上)代码如下
hr = m_pCaptureBuilder2->SetFiltergraph( m_pGraph );
  if (FAILED( hr ))
  {
  return hr;
  }  
CComPtr<IBaseFilter> pVideo; //获取视频的filter
hr = GetCaptureFilter(CLSID_VideoInputDeviceCategory, strVideo, &pVideo);
if (FAILED(hr))
{
return hr;
}
hr = m_pGraph->AddFilter(pVideo, L"Cap");
  if( FAILED( hr ) )
  {  
  return hr;
  }

CComPtr<IBaseFilter> pAudio; //获取音频的filter
hr = GetCaptureFilter(CLSID_AudioInputDeviceCategory, strAudio, &pAudio);
if (FAILED(hr))
{
return hr;
}
hr = m_pGraph->AddFilter(pAudio, L"Audio");
  if( FAILED( hr ) )
  {  
  return hr;
  }

  CComPtr<IBaseFilter> pSmart; //获取视频的filter
  hr = pSmart.CoCreateInstance(CLSID_SmartTee);
  if( !pSmart )
  {
 
  return hr;
  }
  hr = m_pGraph->AddFilter(pSmart, L"SmartTee");
  if( FAILED( hr ) )
  {  
  return hr;
  }  

hr = m_pGrabber.CoCreateInstance( CLSID_SampleGrabber );
  if( !m_pGrabber )
  {

  return hr;
  }
IBaseFilter *pRender; //Video Renderer Filter
hr = CoCreateInstance(CLSID_VideoRenderer,NULL,CLSCTX_INPROC_SERVER,IID_IBaseFilter,(void **)&pRender);
hr = m_pGraph->AddFilter(pRender, L"VideoRenderer");
  if( FAILED( hr ) )
  {  
  return hr;
  }

  CComQIPtr< IBaseFilter, &IID_IBaseFilter > pGrabBase( m_pGrabber );
   
  // force it to connect to video, 24 bit  
  CMediaType VideoType; //Strmbase.lib
  VideoType.SetType( &MEDIATYPE_Video);
  VideoType.SetSubtype(&MEDIASUBTYPE_RGB24);
  hr = m_pGrabber->SetMediaType( &VideoType ); // shouldn't fail
  if( FAILED( hr ) )
  {
  return hr;
  }
   
  // add the grabber to the graph  
  hr = m_pGraph->AddFilter( pGrabBase, L"Grabber" );
  if( FAILED( hr ) )
  { 
  return hr;
  }
   
IBaseFilter *pMux;
hr = m_pCaptureBuilder2->SetOutputFileName(
&MEDIASUBTYPE_Avi,
L"C:\\test.avi", // File name.
&pMux, // Receives a pointer to the mux.
NULL); // (Optional) Receives a pointer to the file sink.
  

hr = ConnectFilters(pVideo, pSmart); //将两个filter连接
hr = ConnectFilters(pSmart, pGrabBase);
hr = ConnectFilters(pGrabBase, pRender);
hr = ConnectFilters(pSmart, pMux);
hr = ConnectFilters(pAudio, pMux);

hr = m_pCaptureBuilder2->RenderStream(
  &PIN_CATEGORY_PREVIEW,
  &MEDIATYPE_Interleaved, 
  pSmart, 
  NULL, 
  pMux);
{  
  // try to render preview pin
  hr = m_pCaptureBuilder2->RenderStream(
  &PIN_CATEGORY_PREVIEW, 
  &MEDIATYPE_Video,
  pSmart, 
  NULL, 
  pMux); 
  if( FAILED( hr ) )
  {
  hr = m_pCaptureBuilder2->RenderStream( 
  &PIN_CATEGORY_CAPTURE, 
  相关解决方案