在C ++ DirectShow中将AVI文件转换为WMV; GraphEdit和源代码

时间:2018-04-04 16:01:28

标签: c++ video com directshow

我想制作一个工具来获取AVI文件作为输入并将其转换为WMV文件并将其保存在我的硬盘上。 在GraphEdit中,我成功地完成了它,你可以在

中看到

enter image description here

我开始为同一个任务制作COM C ++代码以产生相同的效果,但我没有得到问题,阻止我拥有最终转换后的文件。 这是我编写的代码(存在大量内存泄漏)。 我检查了所有返回的值,所有都是OKAY,所以调用中没有错误。

#include <dshow.h>
#pragma comment(lib,"Strmiids.lib") 
#define AVI_FILE_PATH L"C:\\...\\Desktop\\oo.avi"
#define WMV_FILE_PATH L"C:\\...\\Desktop\\BRGMovie.wmv"

int main() {
    IGraphBuilder* pGraph             = NULL;
    IBaseFilter*   pSource            = NULL;
    IBaseFilter*   pAviSplitter       = NULL;
    IBaseFilter*   pMjpegDecompressor = NULL;
    IBaseFilter*   pSmartTee          = NULL;
    IBaseFilter*   pAviMux            = NULL;
    IBaseFilter*   pFileWriter        = NULL;
    ICaptureGraphBuilder2* pBuild     = NULL;
    IFileSourceFilter*  FileSource    = NULL;

    CoInitialize(NULL);
    CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&pGraph            );
    CoCreateInstance(CLSID_AsyncReader, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pSource           );
    CoCreateInstance(CLSID_AviSplitter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pAviSplitter      );
    CoCreateInstance(CLSID_MjpegDec,    NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pMjpegDecompressor);
    CoCreateInstance(CLSID_SmartTee,    NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pSmartTee         );
    CoCreateInstance(CLSID_AviDest,     NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pAviMux           );
    CoCreateInstance(CLSID_FileWriter,  NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter,   (void**)&pFileWriter       );
    CoCreateInstance(CLSID_FileSource,  NULL, CLSCTX_INPROC_SERVER, IID_IFileSourceFilter, (void**)&FileSource);
    CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuild);

    pGraph->AddFilter(pSource,            NULL);
    pGraph->AddFilter(pAviSplitter,       NULL);
    pGraph->AddFilter(pMjpegDecompressor, NULL);
    pGraph->AddFilter(pSmartTee,          NULL);
    pGraph->AddFilter(pAviMux,            NULL);
    pGraph->AddFilter(pFileWriter,        NULL);

    pSource->QueryInterface(IID_IFileSourceFilter, (void**)&FileSource);
    FileSource->Load(AVI_FILE_PATH, NULL);

    pBuild->SetOutputFileName(&MEDIASUBTYPE_Asf, WMV_FILE_PATH, &pAviMux, NULL);
    pBuild->RenderStream(NULL, &MEDIATYPE_Video, pSource, 0, pAviMux);

    IEnumPins*  EnumPins = NULL;
    IPin*       OutPin   = NULL;
    IPin*       InPin    = NULL;
    ULONG       fetched  = NULL;
    PIN_INFO    pinfo;

    // find source output
    pSource->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &OutPin, &fetched);
    EnumPins->Release();

    // find AviSplitter input
    pAviSplitter->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &InPin, &fetched);
    InPin->QueryPinInfo(&pinfo);
    pinfo.pFilter->Release();       
    pGraph->Connect(OutPin, InPin);
    InPin->Release();
    OutPin->Release();

    // find AviSplitter upper output
    EnumPins->Reset();
    EnumPins->Next(1, &OutPin, &fetched);
    OutPin->QueryPinInfo(&pinfo);
    pinfo.pFilter->Release();

    EnumPins->Skip(1);
    EnumPins->Release();

    // find MjpegDecompressor input
    pMjpegDecompressor->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &InPin, &fetched);    

    pGraph->Connect(OutPin, InPin);
    InPin->Release();
    OutPin->Release();

    // find MjpegDecompressor output
    EnumPins->Reset();
    EnumPins->Next(1, &OutPin, &fetched);
    OutPin->QueryPinInfo(&pinfo);
    pinfo.pFilter->Release();
    EnumPins->Release();

    // find SmartTee input
    pSmartTee->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &InPin, &fetched);

    pGraph->Connect(OutPin, InPin);
    InPin->Release();
    OutPin->Release();

    // find SmartTee output
    EnumPins->Reset();
    EnumPins->Next(1, &OutPin, &fetched);
    OutPin->QueryPinInfo(&pinfo);
    pinfo.pFilter->Release();

    EnumPins->Skip(1);

    // find AVIMux input
    pAviMux->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &InPin, &fetched);

    pGraph->Connect(OutPin, InPin);
    InPin->Release();
    OutPin->Release();

    EnumPins->Skip(1);

    // find AVIMux output
    EnumPins->Reset();
    EnumPins->Next(1, &OutPin, &fetched);
    OutPin->QueryPinInfo(&pinfo);
    pinfo.pFilter->Release();   

    // find FileWriter input
    pFileWriter->EnumPins(&EnumPins);
    EnumPins->Reset();
    EnumPins->Next(1, &InPin, &fetched);
    EnumPins->Release();
    pGraph->Connect(OutPin, InPin);
    InPin->Release();
    OutPin->Release();
    FileSource->Release();
    CoUninitialize();
    return 0;
}

编辑:

int main() {
    IGraphBuilder* pGraph = NULL;
    IBaseFilter*   pASFWriter = NULL;
    IMediaControl *pControl = NULL;
    IMediaEvent   *pEvent = NULL;
    ICaptureGraphBuilder2* pBuild = NULL;

    HRESULT hr = CoInitialize(NULL);
    hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
    hr = CoCreateInstance(CLSID_WMAsfWriter, NULL, CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void **)&pASFWriter);
    hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&pBuild);
    hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
    hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
    hr = pBuild->SetOutputFileName(&MEDIASUBTYPE_Asf, WMV_FILE_PATH, &pASFWriter, NULL);
    hr = pGraph->RenderFile(AVI_FILE_PATH, NULL);
    hr = pControl->Run();
    long evCode;
    pEvent->WaitForCompletion(INFINITE, &evCode);

}

1 个答案:

答案 0 :(得分:0)

提供的过滤器图表不会生成WMV(也称为ASF)文件。相反,您正在尝试创建扩展名为.WMV的AVI文件(您使用的是AVI Mux!)。

第二个问题是使用Smart Tee过滤器,它不应该用在这样的转码图中。 Smart Tee有自己的(并有文件证明)名称。

要生成WMV文件,您需要将MJPEG解码器输出连接到WM ASF Writer Filter实例的输入。需要配置过滤器并附加配置文件。

有关详细信息,请参阅Creating ASF Files in DirectShow

您的问题也是from MSDN