convert mpg to avi in directshow
-
Recently I try to convert mpg file to avi programmly, but it doesn't work , i am a beginner:) ,the code list below, anyone can give me suggestion? thanks. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); IBaseFilter *pSrcFilter = NULL; hr = pGraph->AddSourceFilter(lpMpgFile, L"Source Filter", &pSrcFilter); hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuilder); pBuilder->SetFiltergraph(pGraph); hr = pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, lpOutputAviFile, &pMux, NULL); hr = pGraph->AddFilter(pMux, L"Mux Filter"); IBaseFilter *pMpeg2Splitter= NULL; hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pMpeg2Splitter); hr = pGraph->AddFilter(pMpeg2Splitter, L"Mpeg2 Splitter"); IMpeg2Demultiplexer *pDemux = NULL; hr = pMpeg2Splitter->QueryInterface(IID_IMpeg2Demultiplexer, (void**)&pDemux); AM_MEDIA_TYPE pMediaType; memset(&pMediaType, 0x0, sizeof(pMediaType)); pMediaType.majortype = MEDIATYPE_Video; pMediaType.subtype = MEDIASUBTYPE_MPEG2_VIDEO; pMediaType.formattype = FORMAT_MPEG2Video; pMediaType.bFixedSizeSamples = FALSE; pMediaType.bTemporalCompression = TRUE; pMediaType.lSampleSize = 0; MPEG2VIDEOINFO video_info; memset(&video_info, 0x0, sizeof(MPEG2VIDEOINFO)); pMediaType.cbFormat = sizeof(MPEG2VIDEOINFO); pMediaType.pbFormat = (BYTE *)&video_info; AM_MEDIA_TYPE pMediaAudioType; memset(&pMediaAudioType, 0x0, sizeof(pMediaAudioType)); pMediaAudioType.majortype = MEDIATYPE_Audio; pMediaAudioType.subtype = MEDIASUBTYPE_MPEG1Payload; pMediaAudioType.formattype = FORMAT_WaveFormatEx; pMediaAudioType.bFixedSizeSamples = FALSE; pMediaAudioType.bTemporalCompression = FALSE; pMediaAudioType.lSampleSize = 0; MPEG1WAVEFORMAT audio_info; memset(&audio_info, 0x0, sizeof(MPEG1WAVEFORMAT)); pMediaAudioType.cbFormat = sizeof(MPEG1WAVEFORMAT); pMediaAudioType.pbFormat = (BYTE *)&audio_info; IPin *pPin0 = NULL; hr = pDemux->CreateOutputPin(&pMediaType, L"Video Output", &pPin0); IMPEG2StreamIdMap *pStreamIdMap = NULL; hr = pPin0->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap); hr = pStreamIdMap->MapStreamId(0xC0, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0); IPin *pPin1 = NULL; hr = pDemux->CreateOutputPin(&pMediaAudioType, L"Audio Output", &pPin1); IMPEG2StreamIdMap *pStreamIdMap1 = NULL; hr = pPin1->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap1); hr = pStreamIdMap1->MapStre
-
Recently I try to convert mpg file to avi programmly, but it doesn't work , i am a beginner:) ,the code list below, anyone can give me suggestion? thanks. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); IBaseFilter *pSrcFilter = NULL; hr = pGraph->AddSourceFilter(lpMpgFile, L"Source Filter", &pSrcFilter); hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuilder); pBuilder->SetFiltergraph(pGraph); hr = pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, lpOutputAviFile, &pMux, NULL); hr = pGraph->AddFilter(pMux, L"Mux Filter"); IBaseFilter *pMpeg2Splitter= NULL; hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pMpeg2Splitter); hr = pGraph->AddFilter(pMpeg2Splitter, L"Mpeg2 Splitter"); IMpeg2Demultiplexer *pDemux = NULL; hr = pMpeg2Splitter->QueryInterface(IID_IMpeg2Demultiplexer, (void**)&pDemux); AM_MEDIA_TYPE pMediaType; memset(&pMediaType, 0x0, sizeof(pMediaType)); pMediaType.majortype = MEDIATYPE_Video; pMediaType.subtype = MEDIASUBTYPE_MPEG2_VIDEO; pMediaType.formattype = FORMAT_MPEG2Video; pMediaType.bFixedSizeSamples = FALSE; pMediaType.bTemporalCompression = TRUE; pMediaType.lSampleSize = 0; MPEG2VIDEOINFO video_info; memset(&video_info, 0x0, sizeof(MPEG2VIDEOINFO)); pMediaType.cbFormat = sizeof(MPEG2VIDEOINFO); pMediaType.pbFormat = (BYTE *)&video_info; AM_MEDIA_TYPE pMediaAudioType; memset(&pMediaAudioType, 0x0, sizeof(pMediaAudioType)); pMediaAudioType.majortype = MEDIATYPE_Audio; pMediaAudioType.subtype = MEDIASUBTYPE_MPEG1Payload; pMediaAudioType.formattype = FORMAT_WaveFormatEx; pMediaAudioType.bFixedSizeSamples = FALSE; pMediaAudioType.bTemporalCompression = FALSE; pMediaAudioType.lSampleSize = 0; MPEG1WAVEFORMAT audio_info; memset(&audio_info, 0x0, sizeof(MPEG1WAVEFORMAT)); pMediaAudioType.cbFormat = sizeof(MPEG1WAVEFORMAT); pMediaAudioType.pbFormat = (BYTE *)&audio_info; IPin *pPin0 = NULL; hr = pDemux->CreateOutputPin(&pMediaType, L"Video Output", &pPin0); IMPEG2StreamIdMap *pStreamIdMap = NULL; hr = pPin0->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap); hr = pStreamIdMap->MapStreamId(0xC0, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0); IPin *pPin1 = NULL; hr = pDemux->CreateOutputPin(&pMediaAudioType, L"Audio Output", &pPin1); IMPEG2StreamIdMap *pStreamIdMap1 = NULL; hr = pPin1->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap1); hr = pStreamIdMap1->MapStre
why doen't it work ? what error do you get ?
[VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]
-
Recently I try to convert mpg file to avi programmly, but it doesn't work , i am a beginner:) ,the code list below, anyone can give me suggestion? thanks. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); IBaseFilter *pSrcFilter = NULL; hr = pGraph->AddSourceFilter(lpMpgFile, L"Source Filter", &pSrcFilter); hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuilder); pBuilder->SetFiltergraph(pGraph); hr = pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, lpOutputAviFile, &pMux, NULL); hr = pGraph->AddFilter(pMux, L"Mux Filter"); IBaseFilter *pMpeg2Splitter= NULL; hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pMpeg2Splitter); hr = pGraph->AddFilter(pMpeg2Splitter, L"Mpeg2 Splitter"); IMpeg2Demultiplexer *pDemux = NULL; hr = pMpeg2Splitter->QueryInterface(IID_IMpeg2Demultiplexer, (void**)&pDemux); AM_MEDIA_TYPE pMediaType; memset(&pMediaType, 0x0, sizeof(pMediaType)); pMediaType.majortype = MEDIATYPE_Video; pMediaType.subtype = MEDIASUBTYPE_MPEG2_VIDEO; pMediaType.formattype = FORMAT_MPEG2Video; pMediaType.bFixedSizeSamples = FALSE; pMediaType.bTemporalCompression = TRUE; pMediaType.lSampleSize = 0; MPEG2VIDEOINFO video_info; memset(&video_info, 0x0, sizeof(MPEG2VIDEOINFO)); pMediaType.cbFormat = sizeof(MPEG2VIDEOINFO); pMediaType.pbFormat = (BYTE *)&video_info; AM_MEDIA_TYPE pMediaAudioType; memset(&pMediaAudioType, 0x0, sizeof(pMediaAudioType)); pMediaAudioType.majortype = MEDIATYPE_Audio; pMediaAudioType.subtype = MEDIASUBTYPE_MPEG1Payload; pMediaAudioType.formattype = FORMAT_WaveFormatEx; pMediaAudioType.bFixedSizeSamples = FALSE; pMediaAudioType.bTemporalCompression = FALSE; pMediaAudioType.lSampleSize = 0; MPEG1WAVEFORMAT audio_info; memset(&audio_info, 0x0, sizeof(MPEG1WAVEFORMAT)); pMediaAudioType.cbFormat = sizeof(MPEG1WAVEFORMAT); pMediaAudioType.pbFormat = (BYTE *)&audio_info; IPin *pPin0 = NULL; hr = pDemux->CreateOutputPin(&pMediaType, L"Video Output", &pPin0); IMPEG2StreamIdMap *pStreamIdMap = NULL; hr = pPin0->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap); hr = pStreamIdMap->MapStreamId(0xC0, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0); IPin *pPin1 = NULL; hr = pDemux->CreateOutputPin(&pMediaAudioType, L"Audio Output", &pPin1); IMPEG2StreamIdMap *pStreamIdMap1 = NULL; hr = pPin1->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap1); hr = pStreamIdMap1->MapStre
A few potential problems... SetOutputFileName() adds the AVI mux filter to the graph - you don't need to add it manually. I'm not sure why you're using the MPEG2 splitter AND muxer. Regardless, you'll need an mpeg2 decoder for it to work (I don't think there's one included with Windows). I would try simplifying your graph building first. Compare your method to the method shown here: Building the Recompression Graph[^] Also testing it in graphedit.exe can help to make sure you're using the correct filters. MArk
Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the letter Z
-
Recently I try to convert mpg file to avi programmly, but it doesn't work , i am a beginner:) ,the code list below, anyone can give me suggestion? thanks. hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph); IBaseFilter *pSrcFilter = NULL; hr = pGraph->AddSourceFilter(lpMpgFile, L"Source Filter", &pSrcFilter); hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2, (void **)&pBuilder); pBuilder->SetFiltergraph(pGraph); hr = pBuilder->SetOutputFileName(&MEDIASUBTYPE_Avi, lpOutputAviFile, &pMux, NULL); hr = pGraph->AddFilter(pMux, L"Mux Filter"); IBaseFilter *pMpeg2Splitter= NULL; hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, 0, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pMpeg2Splitter); hr = pGraph->AddFilter(pMpeg2Splitter, L"Mpeg2 Splitter"); IMpeg2Demultiplexer *pDemux = NULL; hr = pMpeg2Splitter->QueryInterface(IID_IMpeg2Demultiplexer, (void**)&pDemux); AM_MEDIA_TYPE pMediaType; memset(&pMediaType, 0x0, sizeof(pMediaType)); pMediaType.majortype = MEDIATYPE_Video; pMediaType.subtype = MEDIASUBTYPE_MPEG2_VIDEO; pMediaType.formattype = FORMAT_MPEG2Video; pMediaType.bFixedSizeSamples = FALSE; pMediaType.bTemporalCompression = TRUE; pMediaType.lSampleSize = 0; MPEG2VIDEOINFO video_info; memset(&video_info, 0x0, sizeof(MPEG2VIDEOINFO)); pMediaType.cbFormat = sizeof(MPEG2VIDEOINFO); pMediaType.pbFormat = (BYTE *)&video_info; AM_MEDIA_TYPE pMediaAudioType; memset(&pMediaAudioType, 0x0, sizeof(pMediaAudioType)); pMediaAudioType.majortype = MEDIATYPE_Audio; pMediaAudioType.subtype = MEDIASUBTYPE_MPEG1Payload; pMediaAudioType.formattype = FORMAT_WaveFormatEx; pMediaAudioType.bFixedSizeSamples = FALSE; pMediaAudioType.bTemporalCompression = FALSE; pMediaAudioType.lSampleSize = 0; MPEG1WAVEFORMAT audio_info; memset(&audio_info, 0x0, sizeof(MPEG1WAVEFORMAT)); pMediaAudioType.cbFormat = sizeof(MPEG1WAVEFORMAT); pMediaAudioType.pbFormat = (BYTE *)&audio_info; IPin *pPin0 = NULL; hr = pDemux->CreateOutputPin(&pMediaType, L"Video Output", &pPin0); IMPEG2StreamIdMap *pStreamIdMap = NULL; hr = pPin0->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap); hr = pStreamIdMap->MapStreamId(0xC0, MPEG2_PROGRAM_ELEMENTARY_STREAM, 0, 0); IPin *pPin1 = NULL; hr = pDemux->CreateOutputPin(&pMediaAudioType, L"Audio Output", &pPin1); IMPEG2StreamIdMap *pStreamIdMap1 = NULL; hr = pPin1->QueryInterface(IID_IMPEG2StreamIdMap, (void **)&pStreamIdMap1); hr = pStreamIdMap1->MapStre
Do you get any error?
WhiteSky
-
A few potential problems... SetOutputFileName() adds the AVI mux filter to the graph - you don't need to add it manually. I'm not sure why you're using the MPEG2 splitter AND muxer. Regardless, you'll need an mpeg2 decoder for it to work (I don't think there's one included with Windows). I would try simplifying your graph building first. Compare your method to the method shown here: Building the Recompression Graph[^] Also testing it in graphedit.exe can help to make sure you're using the correct filters. MArk
Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the letter Z
Thanks you very much! I get the root of problem by using graphedit.exe: the input mpg file is a mpeg-1 file, so the file source filer outputs mpeg1-stream, but mpeg2demultiplexer requires mpeg2-stream, when it's running, it popup many DCDSPFilter.ax window and show a error message: These filters cannot agree on a connection, verify type compatibility of input pin and output pin. after I change the mpeg2demultiplexer to mpeg1streamsplitter and add the mpeg1 decoder, it runs OK. But this remains a problem: how can I get the type of mpeg file? I have to identify the file type(mpeg1 or mpeg2) programmly so that I can add the according filter. ???
-
Thanks you very much! I get the root of problem by using graphedit.exe: the input mpg file is a mpeg-1 file, so the file source filer outputs mpeg1-stream, but mpeg2demultiplexer requires mpeg2-stream, when it's running, it popup many DCDSPFilter.ax window and show a error message: These filters cannot agree on a connection, verify type compatibility of input pin and output pin. after I change the mpeg2demultiplexer to mpeg1streamsplitter and add the mpeg1 decoder, it runs OK. But this remains a problem: how can I get the type of mpeg file? I have to identify the file type(mpeg1 or mpeg2) programmly so that I can add the according filter. ???
liur17 wrote:
how can I get the type of mpeg file? I have to identify the file type(mpeg1 or mpeg2) programmly so that I can add the according filter. ???
2 possible ways I can think of: 1) After the successful AddSourceFilter() call, obtain a IFileSourceFilter interface from the source filter and use the IFileSourceFilter::GetCurFile() method to get the media type (looking for MPEG subtypes). 2) Let intelligent connect insert the necessary filters for you. Use AddSourceFilter() and SetOutputFileName() like you're doing, add the compressor filter you want for the destination AVI file, and use RenderStream() to connect the filters added by AddSourceFilter() and SetOutputFileName(), with the intermediate compressor filter. If you have suitable MPEG filters installed, intelligent connect will insert them for you. If not, RenderStream() will fail. A second call to RenderStream() will connect an audio stream to the AVI muxer if it's available (again, see here[^] for an example. You can try this in graphedit by manually adding a filesource async filter, setting the input filename, add a filewriter filter, set the output filename, add an AVI Mux filter, and connect the filesource to the filewriter filter. Mark
Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the letter Z
-
liur17 wrote:
how can I get the type of mpeg file? I have to identify the file type(mpeg1 or mpeg2) programmly so that I can add the according filter. ???
2 possible ways I can think of: 1) After the successful AddSourceFilter() call, obtain a IFileSourceFilter interface from the source filter and use the IFileSourceFilter::GetCurFile() method to get the media type (looking for MPEG subtypes). 2) Let intelligent connect insert the necessary filters for you. Use AddSourceFilter() and SetOutputFileName() like you're doing, add the compressor filter you want for the destination AVI file, and use RenderStream() to connect the filters added by AddSourceFilter() and SetOutputFileName(), with the intermediate compressor filter. If you have suitable MPEG filters installed, intelligent connect will insert them for you. If not, RenderStream() will fail. A second call to RenderStream() will connect an audio stream to the AVI muxer if it's available (again, see here[^] for an example. You can try this in graphedit by manually adding a filesource async filter, setting the input filename, add a filewriter filter, set the output filename, add an AVI Mux filter, and connect the filesource to the filewriter filter. Mark
Mark Salsbery Microsoft MVP - Visual C++ This episode brought to you by the letter Z