void PrepareSampleAndTarget(IltmmConvert* pSourceConvert, WCHAR pwszSourceFile[256], WCHAR mtType[40], IltmmSampleTarget** ppSampleTarget, IltmmMultiStreamSource* pVideoAndAudioSource, long lStream){IltmmMediaTypeDisp *pInsertedMeidaType;IltmmMediaTypeDisp *pmt;USES_CONVERSION;pSourceConvert->put_SourceFile(pwszSourceFile);HRESULT hr = CoCreateInstance(CLSID_ltmmSampleTarget, NULL, CLSCTX_INPROC_SERVER, IID_IltmmSampleTarget, (void**) ppSampleTarget);if(FAILED(hr)){return;}hr = CoCreateInstance(CLSID_ltmmMediaType, NULL, CLSCTX_INPROC_SERVER, IID_IltmmMediaTypeDisp, (void**) &pmt);if(FAILED(hr)){return;}// create a ltmmMediaType and indicate the accepted typepmt->put_Type(mtType);// make the SampleTarget object accept connections of this media typehr = (*ppSampleTarget)->SetAcceptedMediaType(pmt);pmt->Release();pmt = NULL;// set the output target object to be sample targetpSourceConvert->put_TargetObject(*ppSampleTarget);// start the conversion, so I know the exact media typepSourceConvert->StartConvert();// set the media type for the source stream to be the same as the media type in the input file (video or audio)(*ppSampleTarget)->GetConnectedMediaType(&pmt);pVideoAndAudioSource->SetMediaType(lStream, pmt);pVideoAndAudioSource->GetMediaType(0, &pInsertedMeidaType);if (pInsertedMeidaType)pInsertedMeidaType->Release();}void CopySamples(IltmmConvert* pSourceCtrl, IltmmSampleTarget* pSampleTarget,IltmmMultiStreamSource *pVideoAndAudioSource, long lStream){IltmmMediaSampleDisp* pmsSrc = NULL;IltmmMediaSampleDisp* pmsDst = NULL;long lStartTimeHi;long lStartTimeLo;long lStopTimeHi;long lStopTimeLo;HRESULT hr;do{hr = pSampleTarget->GetSample(3000, &pmsSrc);if (!pmsSrc)break;// get a source samplehr = pVideoAndAudioSource->GetSampleBuffer(lStream, 2000, &pmsDst);if (hr == E_FAIL)hr = pVideoAndAudioSource->GetSampleBuffer(lStream, 3000, &pmsDst);// copy the data to the source sampleVARIANT vBuffer;long lActualDataLength;hr = pmsSrc->get_Buffer(&vBuffer);hr = pmsSrc->get_ActualDataLength(&lActualDataLength);hr = pmsDst->SetData(lActualDataLength, vBuffer);// copy the sample timepmsSrc->GetTime(&lStartTimeHi, &lStartTimeLo, &lStopTimeHi, &lStopTimeLo);pmsDst->SetTime(lStartTimeHi, lStartTimeLo, lStopTimeHi, lStopTimeLo);// copy the other flagsVARIANT_BOOL vBool;pmsSrc->get_Discontinuity(&vBool);pmsDst->put_Discontinuity(vBool);pmsSrc->get_Preroll(&vBool);pmsDst->put_Preroll(vBool);pmsSrc->get_SyncPoint(&vBool);pmsDst->put_SyncPoint(vBool);//release the source samplepmsSrc->Release();pmsSrc = NULL;// deliver the source samplepVideoAndAudioSource->DeliverSample(lStream, 2000, pmsDst);// release the source samplepmsDst->Release();pmsDst = NULL;} while (1);// stop the conversion in the source controlpSourceCtrl->StopConvert();// deliver end of stream for the current source stream//hr = pVideoAndAudioSource->DeliverEndOfStream(lStream, 5000);}void Convert(CString strVid, CString strAud, CString strDest){IltmmConvert* pVideoConvert;IltmmConvert* pAudioConvert;IltmmConvert* pDestConvert;pVideoConvert = NULL;pAudioConvert = NULL;pDestConvert = NULL;CoInitialize(NULL);HRESULT hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &pVideoConvert);if(FAILED(hr)){AfxMessageBox(_T("Can't instantiate convert library"));}hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &pAudioConvert);if(FAILED(hr)){AfxMessageBox(_T("Can't instantiate convert library"));}hr = CoCreateInstance(CLSID_ltmmConvert, NULL, CLSCTX_INPROC_SERVER, IID_IltmmConvert, (void**) &pDestConvert);if(FAILED(hr)){AfxMessageBox(_T("Can't instantiate convert library"));}IltmmSampleTarget* pVideoTarget = NULL;IltmmSampleTarget* pAudioTarget = NULL;IltmmMultiStreamSource *pVideoAndAudioSource = NULL;hr = CoCreateInstance(CLSID_ltmmMultiStreamSource, NULL, CLSCTX_INPROC_SERVER, IID_IltmmMultiStreamSource, (void**) &pVideoAndAudioSource);if(FAILED(hr)){AfxMessageBox(_T("Can't instantiate convert library"));}pVideoAndAudioSource->put_StreamCount(2);/*Prepare pVideoConvert and set its source file to VideoFile.Convert only the video streamCreate a new pVideoTarget object and set as the sample target object in pVideoConvertSet the stream media type for pin 0 of VideoAndAudioSource to be the same as the video media typeAlso starts the conversion in pVideoConvert*/PrepareSampleAndTarget(pVideoConvert, strVid.GetBuffer(), ltmmMEDIATYPE_Video, &pVideoTarget, pVideoAndAudioSource, 0);/*Prepare AudioCtrl and set its source file to AudioFile.Convert only the audio streamCreate a new AudioTarget object and set as the sample target object in AudioCtrlSet the stream media type for pin 1 of VideoAndAudioSource to be the same as the audio media typeAlso starts the conversion in AudioCtrl*/PrepareSampleAndTarget(pAudioConvert, strAud.GetBuffer(), ltmmMEDIATYPE_Audio, &pAudioTarget, pVideoAndAudioSource, 1);// prepare the destination filepDestConvert->put_SourceObject(pVideoAndAudioSource);pDestConvert->put_TargetFormat(ltmmConvert_TargetFormat_Avi);BSTR bstr = strDest.AllocSysString();pDestConvert->put_TargetFile(bstr);SysFreeString(bstr);pDestConvert->StartConvert();// Copy all the video samples from the Video file to the destination fileCopySamples(pVideoConvert, pVideoTarget, pVideoAndAudioSource, 0);// Copy all the audio samples from the Audio file to the destination fileCopySamples(pAudioConvert, pAudioTarget, pVideoAndAudioSource, 1);hr = pVideoAndAudioSource->DeliverEndOfStream(0, 5000);hr = pVideoAndAudioSource->DeliverEndOfStream(1, 5000);// wait for the destination control to stop the conversionlong lState;pDestConvert->get_State(&lState);while (lState == ltmmConvert_State_Running){Sleep(5000);pDestConvert->get_State(&lState);pDestConvert->StopConvert();}pDestConvert->ResetSource();if (pVideoConvert){pVideoConvert->Release();pVideoConvert = NULL;}if (pAudioConvert){pAudioConvert->Release();pAudioConvert = NULL;}if (pDestConvert){pDestConvert->Release();pDestConvert = NULL;}if (pVideoAndAudioSource){pVideoAndAudioSource->Release();pVideoAndAudioSource = NULL;}CoUninitialize();}
Help Collections
Raster .NET | C API | C++ Class Library | HTML5 JavaScript
Document .NET | C API | C++ Class Library | HTML5 JavaScript
Medical .NET | C API | C++ Class Library | HTML5 JavaScript
Medical Web Viewer .NET
Multimedia
Direct Show .NET | C API | Filters
Media Foundation .NET | C API | Transforms
Supported Platforms
.NET, Java, Android, and iOS/macOS Assemblies
Imaging, Medical, and Document
C API/C++ Class Libraries
Imaging, Medical, and Document
HTML5 JavaScript Libraries
Imaging, Medical, and Document
