Share via


Using a File as a Source

Windows Media Encoder SDK banner art

Audio and video files can be used as an encoding source. Multiple file plug-ins are included with the Windows Media Encoder SDK, one plug-in for each type of file (.wma, .wmv, .asf, .avi, .wav, .mp3, .mpg, .bmp, and .jpg) that can be encoded. Sourcing from a file with an .mpg file name extension requires a compatible MPEG-2 decoder on the encoding computer.

The correct plug-in is automatically chosen for a specific file, so including the FILE scheme type is optional. You must specify the full path and name of the file.

Important Considerations

To avoid errors or loss of data, be aware of the following issues:

  • Video images with one or two odd-sized dimensions are not supported. For example, an image that is 300 by 321 pixels generates an error, but an image that is 300 by 320 pixels does not.
  • When using a Windows Media file as a source, anything stored in the header (such as display information, custom attributes, scripts, or markers) will be lost in the output. You can add the information back to the file before encoding and during postprocessing.

Encoding DVD content

It is possible to source from an unencrypted DVD file. To do so, you must have one of the following approved MPEG-2 decoders installed on the encoding computer:

  • Intervideo WinDVD
  • Cyberlink PowerDVD
  • Ravisent CinePlayer DVD

For more information about obtaining an MPEG-2 decoder, see the Microsoft Web site.

The following examples show how to use files as a source. For end-to-end code examples, see Complete Code Examples.

Visual Basic Example

' Declare objects and variables.
  Dim Encoder as WMEncoder
  Dim SrcGrpColl As IWMEncSourceGroupCollection
  Dim SrcGrp As IWMEncSourceGroup
  Dim SrcAud As IWMEncSource
  Dim SrcVid As IWMEncVideoSource

' Retrieve the source group collection.
  Set SrcGrpColl = Encoder.SourceGroupCollection

' Create a source group called SG_1.
  Set SrcGrp = SrcGrpColl.Add("SG_1")

' Create an audio and a video source object.
  Set SrcAud = SrcGrp.AddSource(WMENC_AUDIO)
  Set SrcVid = SrcGrp.AddSource(WMENC_VIDEO)

' Specify the path of the source file.
  SrcAud.SetInput "c:\audio.wav"
  SrcVid.SetInput "c:\video.avi"

C++ Example

// Include libraries.
#include <windows.h>
#include <atlbase.h>
#include <comdef.h>
#include "C:\WMSDK\WMEncSDK9\include\wmencode.h"

    // Declare variables.
    HRESULT hr;
    IWMEncoder* pEncoder;
    IWMEncFile* pFile;

    // Initialize the COM library and retrieve a pointer to an IWMEncoder interface.
    hr = CoInitialize(NULL);
    if ( SUCCEEDED( hr ) )
    {
        hr = CoCreateInstance(CLSID_WMEncoder,
            NULL,
            CLSCTX_INPROC_SERVER,
            IID_IWMEncoder,
            (void**) &pEncoder);
    }

    // Specify a file object in which to save encoded content.
    if ( SUCCEEDED( hr ) )
    {
        hr = pEncoder->get_File(&pFile);
    }
    if ( SUCCEEDED( hr ) )
    {
        hr = pFile->put_LocalFileName(CComBSTR("C:\\OutputFile.wmv"));
    }

    // Release pointers.
    if ( pFile )
    {
        pFile->Release();
        pFile = NULL;
    }
    }
    if ( pEncoder )
    {
        pEncoder->Release();
        pEncoder = NULL;
    }

C# Example

try
{
   // Create a Windows Media Encoder object.
   WMEncoder Encoder = new WMEncoder();

   // Retrieve the source group collection.
   IWMEncSourceGroupCollection SrcGrpColl = Encoder.SourceGroupCollection;

   // Create a source group called SG_1.
   IWMEncSourceGroup SrcGrp = SrcGrpColl.Add("SG_1");

   // Create an audio and a video source object.
   IWMEncSource SrcAud = SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_AUDIO);
   IWMEncVideoSource SrcVid = (IWMEncVideoSource)SrcGrp.AddSource(WMENC_SOURCE_TYPE.WMENC_VIDEO);

   // Specify the path of the source file.
   SrcAud.SetInput("C:\\audio.wav", "", "");
   SrcVid.SetInput("C:\\video.avi", "", "");
}

catch (Exception e) 
{  
   // TODO: Handle exceptions.
}

See Also