Encoding Windows Media Video Files

The steps required to encode Windows Media files are:

1. A WMEncoder object needs to be instantiated.

'Create a WMEncoder object

Dim encoder As WMEncoder

Set encoder = New WMEncoder

2. A Source Group has to be created. A Source Group holds the synchronized multimedia streams being encoded. It must contain an audio stream, and can contain a video stream and a script stream.

'Add a new source group to the source group collection

Dim SrcGrpColl As IWMEncSourceGroupCollection

Dim SrcGrp As IWMEncSourceGroup

Set SrcGrpCol = encoder.SourceGroup Collection

Set SrcGrp = SrcGrpCol.Add("SG_1")

3. Create an IWMEncSource object for each type of multimedia content in the source group. The IWMEncSource interface is used to either load streams from a file or capture streams from a device. Use the IWMEncSourceGroup interface to add streams to a source group.

'Create a video and an audio source object.

Set AudSrc = SrcGrp.AddSource(WMENC_AUDIO)

Set VidSrc = SrcGrp.AddSource(WMENC_VIDEO)

'Specify the .avi source file.

AudSrc.SetInput "C:\UserTemp\clip4.avi"

VidSrc.SetInput "C:\UserTemp\clip4.avi"

4. Select a profile for the encoding session. A profile specifies a codec, and identifies the number and bit rates of the encoded output streams. Only one profile can be assigned to all of the source groups in the encoding session.

'Iterating through all Windows Media Profiles on your System and setting the 'one you want

Dim ProColl As IWMEncProfileCollection

Dim Pro As IWMEncProfile

Set ProColl = encoder.ProfileCollection

For i = 0 To ProColl.Count - 1

Set Pro = ProColl.Item(i)

If Pro.Name = "Test01" Then

SrcGrp.Profile = Pro

Print "Profile: " & Pro.Name & vbCrLf

Exit For

End If

Next

5. Specify an output option; in this case, the name of the encoded Windows Media file.

'Specify the Output Windows Media File

Dim File As IWMEncFile

Set File = Encoder.File

File.LocalFileName = "C:\filename.wmv"

6. Before starting the actual encoding process, set some crucial parameters.

'Setting some crucial parameters.

Encoder.EnableAutoArchive = True

Encoder.AutoStop = True

Encoder.RemoteAdmin = True

Encoder.EnableAutoArchive = True

Encoder.PrepareToEncode True

7. Finally, start the Windows Media Encoder and stop the Encoder once it is finished. While the video clip is encoded, check on the RunState property of the Encoder object and call the Visual Basic DoEvents statement as long as RunState is not WMENC_ENCODER_STOPPED. DoEvents gives Windows a chance to do other chores while a clip is encoded.

'Start the encoder engine.

Encoder.Start

'Statistics Object required to determine if Encoding is finished

Dim Statistics As IWMEncStatistics

Dim IndexerStats As IWMEncIndexerStats

Set Statistics = encoder.Statistics

Set IndexerStats = Statistics.IndexerStats

'Loop until the Encoding process is finished.

While Encoder.RunState <> WMENC_ENCODER_STOPPED Do Events

Wend

While IndexerStats.FileCount <> 0

DoEvents

Wend

'Stop the encoder engine.

Encoder.Stop

Encoder.PrepareToEncode False

The server-side Visual Basic Application uses the described sequence to create Windows Media Video (WMV) files. Since WindowsMedia 7, Microsoft no longer uses the .ASF extension to describe WindowsMedia files, but uses the .WMV and .WMA (Windows Media Audio) extensions. As far as WindowsMedia Profiles is concerned, use the Microsoft WindowsMedia Encoder application to create the Profiles the encoding application expects. The encoding application requires profiles to comply with the following naming convention: SIZExHEIGHT_BITRATE1_BITRATE2_BITRATEX. For instance, 160X120_56_100 describes a profile that generates a WindowsMedia video clip that has a screen-size of 160X120, and contains a 56 Kbps and 100 Kbps stream. To generate a 320X240 file that contains a single 300 Kbps stream, the original video needs to be encoded with the profile 320X240_300.

— J.G.

Back to Article