void AudioExtraction(int Second) { IMediaControl *pControl = NULL; ICaptureGraphBuilder2 *pBuilder = NULL;//捕捉 IGraphBuilder *pGraph = NULL; //图像滤镜管理器 IAMTimeline *pTimeline = NULL; //时间线对象 IAMTimelineObj *pAudioGroupObj = NULL; //音频组对象 IRenderEngine *pRenderEngine = NULL; //输出控制引擎 IMediaEvent *pEvent = NULL; //获取图像滤镜的事件 IBaseFilter *pWaveDest = NULL; //转换成WAV IBaseFilter *pWriter = NULL; //写文件 IFileSinkFilter *pSink = NULL; IAMTimelineObj *pSource10bj = NULL; IAMTimelineGroup *pAudioGroup = NULL; IAMTimelineObj *pTrack10bj = NULL; IAMTimelineSrc *pSource1Src = NULL; IAMTimelineTrack *pTrack1 = NULL; IAMTimelineComp *pRootComp = NULL; IPin *pin = NULL; IPin *pOut = NULL; AM_MEDIA_TYPE mtGroup; HRESULT hr; hr = CoInitialize(NULL); hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); if (FAILED(hr)){MessageBox(NULL,TEXT("fail pGraph"),TEXT("f"),MB_OK);} hr = CoCreateInstance(CLSID_AMTimeline, NULL, CLSCTX_INPROC_SERVER, IID_IAMTimeline, (void**)&pTimeline); if (FAILED(hr)){MessageBox(NULL,TEXT("fail timeline"),TEXT("f"),MB_OK);} hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuilder); if (FAILED(hr)){MessageBox(NULL,TEXT("fail captureGraphBuilder"),TEXT("f"),MB_OK);} hr = CoCreateInstance(CLSID_WavDest, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **)&pWaveDest); if (FAILED(hr)){MessageBox(NULL,TEXT("fail wavdest"),TEXT("f"),MB_OK);} hr = CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_ALL, IID_IBaseFilter, (void **)&pWriter); if (FAILED(hr)){MessageBox(NULL, TEXT("fail writer"), TEXT("f"), MB_OK);} hr = pTimeline->CreateEmptyNode(&pAudioGroupObj, TIMELINE_MAJOR_TYPE_GROUP);//调用音频组接口对象 if (FAILED(hr)){MessageBox(NULL, TEXT("fail major type group"), TEXT("f") ,MB_OK);} pAudioGroupObj->QueryInterface(IID_IAMTimelineGroup, (void**)&pAudioGroup); ZeroMemory(&mtGroup, sizeof(AM_MEDIA_TYPE)); mtGroup.majortype = MEDIATYPE_Audio; pAudioGroup->SetMediaType(&mtGroup); hr = pTimeline->AddGroup(pAudioGroupObj); if (FAILED(hr)){MessageBox(NULL, TEXT("fail addGroup"), TEXT("f"), MB_OK);} pTimeline->CreateEmptyNode(&pTrack10bj, TIMELINE_MAJOR_TYPE_TRACK); pAudioGroup->QueryInterface(IID_IAMTimelineComp, (void**)&pRootComp); pRootComp->VTrackInsBefore(pTrack10bj, -1); pTimeline->CreateEmptyNode(&pSource10bj, TIMELINE_MAJOR_TYPE_SOURCE); const LONGLONG NANOSECONDS = (1000000000); // 10 ^ 9 const LONGLONG UNITS = (NANOSECONDS / 100); // 10 ^ 7 REFERENCE_TIME TLStart = 0 * UNITS; REFERENCE_TIME TLStop = 4 * UNITS; pSource10bj->SetStartStop(TLStart, TLStop); pSource10bj->QueryInterface(IID_IAMTimelineSrc, (void**)&pSource1Src); //REFERENCE_TIME MediaStart=g_nNum * UNITS; //REFERENCE_TIME MediaStop=(g_nNum+5) * UNITS;//分时时间设置 REFERENCE_TIME MediaStart; REFERENCE_TIME MediaStop; MediaStart = (Second-2) * UNITS; MediaStop = (Second+2) * UNITS; //else //{ // MediaStart=5 * UNITS; // MediaStop=11 * UNITS; //} pSource1Src->SetMediaTimes(MediaStart, MediaStop); pSource1Src->SetMediaTimes(MediaStart, MediaStop); BSTR bstrFile1 = SysAllocString(OLESTR("D:\\pictures\\SampleVideo.AVI")); pSource1Src->SetMediaName(bstrFile1); pTrack10bj->QueryInterface(IID_IAMTimelineTrack,(void**)&pTrack1); pTrack1->SrcAdd(pSource10bj); hr = pTimeline->ValidateSourceNames(SFN_VALIDATEF_CHECK | SFN_VALIDATEF_POPUP | SFN_VALIDATEF_REPLACE, NULL, 0 ); if (FAILED(hr)){MessageBox(NULL,TEXT("fail to add"),TEXT("f"),MB_OK);} hr = CoCreateInstance(CLSID_RenderEngine,NULL,CLSCTX_INPROC_SERVER,IID_IRenderEngine,(void**)&pRenderEngine); if (FAILED(hr)){MessageBox(NULL,TEXT("fail renderEngine"),TEXT("f"),MB_OK);} pRenderEngine->SetTimelineObject(pTimeline); hr = pRenderEngine->ConnectFrontEnd(); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to ConnectFrontEnd"), TEXT("f"), MB_OK);} hr = pRenderEngine->GetFilterGraph(&pGraph); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to getFilterGraph"), TEXT("f"), MB_OK);} hr = pGraph->QueryInterface( IID_IMediaEvent, (void**) &pEvent ); if (FAILED(hr)){printf("ERROR - failed to MediaEvent"); } hr = pGraph->QueryInterface( IID_IMediaControl, (void**) &pControl ); if (FAILED(hr)){ printf("ERROR - failed to IID_IMediaControl"); } pWriter->QueryInterface(IID_IFileSinkFilter, (void**)&pSink); BSTR bstrFile2 = SysAllocString(OLESTR("C:\\1.wav")); hr = pSink->SetFileName(bstrFile2,NULL); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to set filename"), TEXT("f"), MB_OK);} pSink->Release(); hr=pGraph->AddFilter(pWaveDest,L"WavDest"); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to add WaveDest"), TEXT("f"), MB_OK);} hr=pGraph->AddFilter(pWriter,L"FileWriter"); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to add FileWriter"), TEXT("f"), MB_OK);} IPin *pPin; hr = pRenderEngine->GetGroupOutputPin(0, &pPin); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to add GroupOut"), TEXT("f"), MB_OK);} if(pWaveDest) { hr = pWaveDest->FindPin(L"In",&pin); } if (FAILED(hr)){MessageBox(NULL, TEXT("fail to add wav"), TEXT("f"), MB_OK);} hr = pGraph->Connect(pPin,pin); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to Add renderEngine->wavdest"), TEXT("f"), MB_OK);} if(pWaveDest) { hr = pWaveDest->FindPin(L"Out", &pOut); } if (FAILED(hr)){MessageBox(NULL, TEXT("fail to Add wavedest_out"),TEXT("f"), MB_OK);} hr = pWriter->FindPin(L"in",&pin); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to Add Write in"),TEXT("f"), MB_OK);} hr = pGraph->Connect(pOut,pin); if (FAILED(hr)){MessageBox(NULL, TEXT("fail to Add wavdest->writer"),TEXT("f"), MB_OK);} pControl->Run(); long EventCode = 0; hr = pEvent->WaitForCompletion( INFINITE, &EventCode ); pControl->Stop(); pEvent->Release(); pControl->Release(); if(pWaveDest) { pWaveDest->Release(); } pWriter->Release(); CoUninitialize(); }