修改 \DXSDK\Samples\C++\DirectShow\Editing\StillCap 中的 CStillCapDlg::InitCaptureGraph 函数如下 :HRESULT CStillCapDlg::InitCaptureGraph( TCHAR * pFilename )
{    HRESULT hr;    // make a filter graph
    //
    m_pGraph.CoCreateInstance( CLSID_FilterGraph );
    if( !m_pGraph )
    {
        Error(TEXT("Could not create filter graph"));
        return E_FAIL;
    }    // get whatever capture device exists
    //
    CComPtr< IBaseFilter > pCap;
    GetDefaultCapDevice( &pCap );
    if( !pCap )
    {
        Error( TEXT("No video capture device was detected on your system.\r\n\r\n")
               TEXT("This sample requires a functional video capture device, such\r\n")
               TEXT("as a USB web camera.  Video capture will be disabled.") );
        return E_FAIL;
    }    // add the capture filter to the graph
    //
    hr = m_pGraph->AddFilter( pCap, L"Cap" );
    if( FAILED( hr ) )
    {
        Error( TEXT("Could not put capture device in graph"));
        return hr;
    }
    // make a capture builder graph (for connecting help)
    //
    CComPtr< ICaptureGraphBuilder2 > pBuilder;
    hr = pBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder2 );
    if( !pBuilder )
    {
        Error( TEXT("Could not create capture graph builder2"));
        return hr;
    }    hr = pBuilder->SetFiltergraph( m_pGraph );
    if( FAILED( hr ) )
    {
        Error( TEXT("Could not set filtergraph on capture graph builder2"));
        return hr;
    }    CComPtr< IBaseFilter > pMux;
    CComPtr< IFileSinkFilter > pSink;
    USES_CONVERSION; // The floging will be changed to asf , save as a asf file
    hr = pBuilder->SetOutputFileName( &MEDIASUBTYPE_Asf,  T2W( pFilename ), 
                                      &pMux, &pSink );
    if( FAILED( hr ) )
    {
        Error( TEXT("Could not create/hookup mux and writer"));
        return hr;
    }
// The flowing for configuring pConfigAsfWriter
// 
IConfigAsfWriter *pConfigAsfWriter = 0 ;
hr = pMux->QueryInterface(IID_IConfigAsfWriter, (void **)&pConfigAsfWriter) ;
if (FAILED(hr))
{
return hr ;
} // Convert the numerical profile index into a IWMProfile interface pointer
CComPtr<IWMProfile> pProfile;
    hr = MapProfileIdToProfile(3, &pProfile);  // 见下
    if(FAILED(hr)) 
{
        return hr ;
    }    hr = pConfigAsfWriter->ConfigureFilterUsingProfile(pProfile);
    if(FAILED(hr)) 
{
       return hr ;
    }       
    
hr = pConfigAsfWriter->SetIndexMode(FALSE);
    if(FAILED(hr)) 
{
return false ;
} pConfigAsfWriter->Release() ;
//
// end configure

    hr = pBuilder->RenderStream(&PIN_CATEGORY_CAPTURE , &MEDIATYPE_Video , pCap , 0 , pMux);
    if( FAILED( hr ) )
    {
        Error( TEXT("Could not connect capture pin Video"));
        return hr;
    } // for preview when capturing
    hr = pBuilder->RenderStream( &PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,
                                 pCap, NULL, NULL );
    if( FAILED( hr ) )
    {
        Error( TEXT("Could not render capture pin"));
        return hr;
    }
    if( hr == VFW_S_NOPREVIEWPIN )
    {
        // preview was faked up using the capture pin, so we can't
        // turn capture on and off at will.
        hr = 0;
    }    // find the video window and stuff it in our window
    //
    CComQIPtr< IVideoWindow, &IID_IVideoWindow > pWindow = m_pGraph;
    if( !pWindow )
    {
        Error( TEXT("Could not get video window interface"));
        return hr;
    }
    // set up the preview window to be in our dialog
    // instead of floating popup
    //
    HWND hwndPreview = NULL;
    GetDlgItem( IDC_PREVIEW, &hwndPreview );
    RECT rc;
    ::GetWindowRect( hwndPreview, &rc );    hr = pWindow->put_Owner( (OAHWND) hwndPreview );
    hr = pWindow->put_Left( 0 );
    hr = pWindow->put_Top( 0 );
    hr = pWindow->put_Width( rc.right - rc.left );
    hr = pWindow->put_Height( rc.bottom - rc.top );
    hr = pWindow->put_Visible( OATRUE );
    hr = pWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS ); // run the graph
    // wrong ! 这一步总会出错
    CComQIPtr< IMediaControl, &IID_IMediaControl > pControl = m_pGraph;
    hr = pControl->Run( );
if( FAILED( hr ) )
    {
OAFilterState p;
pControl->GetState(1000,&p);
        Error( TEXT("Could not run graph"));
        return hr;
    }    UpdateStatus(_T("Capturing Video To Disk"));
    return 0;
}HRESULT MapProfileIdToProfile(int iProfile, IWMProfile **ppProfile)
{
    DWORD cProfiles;    if (!ppProfile)
        return E_POINTER;
        
    *ppProfile = 0;
    
    CComPtr <IWMProfileManager> pIWMProfileManager;    HRESULT hr = WMCreateProfileManager( &pIWMProfileManager );
    if(FAILED(hr)) 
    {
        return hr;
    }    CComQIPtr<IWMProfileManager2, &IID_IWMProfileManager2> pIPM2(pIWMProfileManager);    if(!pIPM2) 
    {
        return E_UNEXPECTED;
    }    // Set the system profile version (4, 7, 8)
    hr = pIPM2->SetSystemProfileVersion( g_WMTVersion );
    if(FAILED(hr))
    {
        return hr;
    }    // Read back the current version to verify
    //hr = pIPM2->GetSystemProfileVersion( &g_WMTVersion );
    if(FAILED(hr)) 
    {
        return hr;
    }    // How many profiles exist for this version?
    hr = pIWMProfileManager->GetSystemProfileCount( &cProfiles );
    if(FAILED(hr))
    {
        return hr;
    }    // Invalid profile requested?
    if( (DWORD)iProfile >= cProfiles ) 
    {
        return E_INVALIDARG;
    }    // Load the requested systme profile and return its interface
    // in the ppProfile parameter
    return (pIWMProfileManager->LoadSystemProfile( iProfile, ppProfile ));}graph 不能 run , 是不是 Asf Writer filter 的属性设置不对或不全 ? 谢谢

解决方案 »

  1.   

    是不是必须要有 wmstub.lib ?
      

  2.   

    DSCopy (DirectShow) Transcodes one or more files to an ASF file using the DirectShow&reg; WM ASF Writer filter. The input file may be any compressed or uncompressed format supported by DirectShow.  DSPlay (DirectShow) This sample is an interactive audio/video media file player with DRM support. It uses DirectShow's WM ASF Reader filter to play Windows Media files (ASF, WMA, WMV) without DRM protection and files which use DRM at a level of 100 or below. See readme.txt in the sample's directory for more information. DSSeekFm (DirectShow) This sample demonstrates how to use the DirectShow WM ASF Reader Filter to play ASF content in a DirectShow filter graph, and also how to use the frame seeking support in the Windows Media Format SDK.  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmform95/htm/sampleapplications.asp
      

  3.   

    谢谢,hr = MapProfileIdToProfile(23, &pProfile);  // Change 3 to 22 , 23后可以保存为 .asf 文件了,但不能捕获声音,该怎么处理呢 ? 谢谢