Skip to content

Instantly share code, notes, and snippets.

@LinArcX
Created May 24, 2021 10:53
Show Gist options
  • Save LinArcX/2326e37d329070c980244c2dd8f9c7a8 to your computer and use it in GitHub Desktop.
Save LinArcX/2326e37d329070c980244c2dd8f9c7a8 to your computer and use it in GitHub Desktop.
Mpeg-2 Encoder graph manager
int main()
{
// 0. Initialize
// 1. Create VideoCaptureFilter
// 2. Create SampleGrabber
// 3. Create MPEG-2 Encoder
// 4. Create FileWriter
// 0 Initialize
HRESULT hr = 0;
IGraphBuilder *pGraph = NULL;
IMediaControl *pMediaControl = NULL;
IMediaEventEx *pMediaEvent = NULL;
//
IBaseFilter *pMyVideoCaptureFilter = NULL;
//
IBaseFilter *pGrabberF = NULL;
ISampleGrabber *pSampleGrabber = NULL;
IBaseFilter *pMpeg2Encoder = NULL;
IBaseFilter *pBaseWriter = NULL;
IFileSinkFilter *pFileSinkWriter = NULL;
hr = CoInitialize(NULL);
if (FAILED(hr))
{
printf("Failed to initialize COM! hr=0x%x\n", hr);
return hr;
}
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&pGraph));
if (FAILED(hr))
{
printf("Failed to create FilterGraph! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pMediaControl));
if (FAILED(hr))
{
printf("Failed to find pMediaControl! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGraph->QueryInterface(IID_PPV_ARGS(&pMediaEvent));
if (FAILED(hr))
{
printf("Failed to find pMediaEvent! hr=0x%x\n", hr);
goto EXIT;
}
// 1. Create VideoCaptureFilter
hr = GetVideoInputFilter(&pMyVideoCaptureFilter, 0);
if (SUCCEEDED(hr)) {
hr = pGraph->AddFilter(pMyVideoCaptureFilter, L"Webcam Video Capture");
}
// 2. Create SampleGrabber
hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pGrabberF));
if (FAILED(hr))
{
printf("Failed to create pGrabberF! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGrabberF->QueryInterface(IID_PPV_ARGS(&pSampleGrabber));
if (FAILED(hr))
{
printf("Failed to find pSampleGrabber! hr=0x%x\n", hr);
goto EXIT;
}
// Set the Media Type
AM_MEDIA_TYPE mt;
ZeroMemory(&mt, sizeof(mt));
mt.majortype = MEDIATYPE_Video;
mt.subtype = MEDIASUBTYPE_RGB24;
hr = pSampleGrabber->SetMediaType(&mt);
if (FAILED(hr))
{
printf("Failed to SetMediaType for SampleGrabber! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGraph->AddFilter(pGrabberF, L"Sample Grabber");
if (FAILED(hr))
{
printf("Failed to add pGrabberF to GraphBuilder! hr=0x%x\n", hr);
goto EXIT;
}
// 3. Create MPEG-2 Encoder
/*hr = CoCreateInstance(CLSID_CMPEG2EncoderDS, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pMpeg2Encoder));
if (FAILED(hr))
{
printf("Failed to create pMpeg2Encoder! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGraph->AddFilter(pMpeg2Encoder, L"MPEG-2 Encoder");
if (FAILED(hr))
{
printf("Failed to add pMpeg2Encoder to GraphBuilder! hr=0x%x\n", hr);
goto EXIT;
}*/
// 4. Create FileWriter
//hr = AddFilterByCLSID(pGraph, CLSID_FileWriter, L"File Writer", &pWriter);
hr = CoCreateInstance(CLSID_FileWriter, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pBaseWriter));
if (FAILED(hr))
{
printf("Failed to create pFileWriter! hr=0x%x\n", hr);
goto EXIT;
}
// Set the file name.
hr = pBaseWriter->QueryInterface(IID_IFileSinkFilter, (void**)&pFileSinkWriter);
hr = pFileSinkWriter->SetFileName(L"C:\\MyWavFile.wav", NULL);
if (FAILED(hr))
{
printf("Failed to SetFileName! hr=0x%x\n", hr);
goto EXIT;
}
hr = pGraph->AddFilter(pBaseWriter, L"File Writer");
if (FAILED(hr))
{
printf("Failed to add pFileWriter to GraphBuilder! hr=0x%x\n", hr);
goto EXIT;
}
//*** Connect Filters ***//
hr = ConnectFilters(pGraph, pMyVideoCaptureFilter, pGrabberF);
if (FAILED(hr))
{
printf("Failed to Connect pGrabberF with pMyVideoCapturerFilter! hr=0x%x\n", hr);
goto EXIT;
}
//hr = ConnectFilters(pGraph, pBaseWriter, pMyVideoCaptureFilter);
//if (FAILED(hr))
// {
// printf("Failed to Connect pFileWriter with pMyVideoCaptureFilter! hr=0x%x\n", hr);
// goto EXIT;
// }
SaveGraphFile(pGraph, L"C:\\MyGraph.GRF");
EXIT:
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment