Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Web Development

Automated Streaming Video


May01: Encoding Windows Media Video Files

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.