## Recompress an AVI File

The following sample code shows how to recompress the file c:\Foo.avi to c:\Bar.avi, where the output file will use Cinepak compression and will include CD-quality audio. Recompression is useful to change the format of a file from one compression scheme to another. The exact benefits of recompression depend on the different compressors used to compress the source and output files and often include producing a smaller output file. In this example the source file, Foo.avi, might be in a format such as uncompressed RGB with 22 kilohertz (kHz) sound.

The AMCap Sample (DirectShow Capture Application) sample demonstrates a capture application and uses many of the same concepts as the following code.

Note This sample code fragment introduces concepts only and is not designed to compile. See the AMCap Sample (DirectShow Capture Application) sample for actual code. The code fragment does not perform error checking for the sake of brevity.

        // Create a graph builder object.
        hr = CoCreateInstance((REFCLSID)CLSID_CaptureGraphBuilder,
                      NULL, CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder,
                      (void **)&pBuild);

        // Create a filter graph, and tell the builder what it is.
        hr = CoCreateInstance((REFCLSID)CLSID_FilterGraph,
                      NULL, CLSCTX_INPROC, (REFIID)IID_IGraphBuilder,
                      (void **)&pFg);
        hr = pBuild->SetFiltergraph(pFg);

        // Obtain a source for c:\foo.avi.
        hr = CoCreateInstance((REFCLSID)CLSID_AsyncReader,
                      NULL, CLSCTX_INPROC, (REFIID)IID_IBaseFilter,
                      (void **)&pSrc);
        hr = pSrc->QueryInterface(IID_IFileSourceFilter, (void **)&pI);
        hr = pI->Load(L"c:\\foo.avi", NULL);
        pI->Release();
        hr = pFg->AddFilter(pSrc, NULL);

        // Create a rendering section to create the c:\bar.avi output file.
        hr = pBuild->SetOutputFileName(&MEDIASUBTYPE_Avi, L"c:\\bar.avi", &pRender,
                                       NULL);

    // [...Enumerate the audio compressors with the category CLSID_AudioCompressorCategory
    // and pick one. See the Amcap.cpp file in the capture sample directory for an example of
    // how to enumerate a category...]

        // Render the recompressed audio stream
        hr = pBuild->RenderStream(NULL, pSrc, pCAud, pRender);

    // [...Enumerate a video compressor using the CLSID_VideoCompressorCategory enum,
    // as seen in Amcap.cpp...]

        pCVid = [...IBaseFilter pointer of the chosen Cinepak compressor...]
        hr = pFg->AddFilter(pCVid, NULL);

        // Tell it to compress at 100k/second data rate.
        // Use the current format to set the data rate, but change the data
        // rate item in the media type.
        hr = pBuild->FindInterface(NULL, pCVid, IID_IAMStreamConfig,
                                   (void **)&pVSC);
        hr = pVSC->GetFormat(&cmt);
        ((VIDEOINFOHEADER)(cmt.Format()))->dwBitRate = 100000;
        hr = pVSC->SetFormat(&cmt);
        pVSC->Release();

        // Request key frames every 4 frames.
        hr = pBuild->FindInterface(NULL, pCVid, IID_IAMVideoCompression,
                                   (void **)&pVC);
        hr = pVC->put_KeyFrameRate(4);
        pVC->Release();

        // Render the recompressed video stream.
        hr = pBuild->RenderStream(NULL, pSrc, pCVid, pRender);

        // All done with these objects now.
        pSrc->Release();
        pRender->Release();
        pCAud->Release();
        pCVid->Release();

        // Run the graph.
        hr = FG->QueryInterface(IID_IMediaControl, &MC);
        MC->Run();

        // Wait for EC_COMPLETE, and it's all done!
        // [...wait for EC_COMPLETE event... see AMCap sample...]

     pGraphBuilder->FindInterface(pRender, IID_IMediaSeeking, pMS);
    // [...While waiting for complete, use IMediaSeeking methods to get
    // the percentage complete... IMediaSeeking->GetCurrentPosition divided by
    // GetDuration * 100 will tell you the percent complete at any time...]

        pFg->Release();
        pBuild->Release();

© 1997 Microsoft Corporation. All rights reserved. Terms of Use.