The Windows Media Encoder SDK is the latest and the easiest API to build media applications. Many different applications can be developed using the Windows Media Encoder SDK, such as video e-mails, security surveillance archiving, encoders, screen capture utilities, Microsoft PowerPoint and Microsoft Office add-ins, digital recorders, and custom encoding applications. It is built on the Windows Media Format SDK, which provides the lower-level functionality. Unlike the Windows Media Format SDK, which could be used only from C, C++, the Windows Media Encoder SDK supports not only .NET languages, but scripting languages like Visual Basic Scripting Edition, Microsoft JScript, and Microsoft Windows Script Host.
Windows Media Video (WMV) and Windows Media Audio (WMA) are generic names for the set of streaming video and audio technologies developed and controlled by Microsoft. It is part of the Windows Media framework. The DivX video format is originally based on the hacked WMV codec. In this article, I will show how easily you can convert different video/audio formats to WMV and WMA formats. The samples demonstrate AVI, Wav, MP3, MPG, WMA, and WMV conversion to WMA and WMV.
Why Windows Media Formats?
WMV and WMA are designed to handle all types of video and audio contents, respectively. The files can be highly compressed, and can be delivered as a continuous flow of data (on-line radio). WMV and WMA files can be of any size, and be compressed to match many different bandwidths (connection speeds). Other Microsoft media formats like ASF files are similar to the WMV format. Most of all these are Microsoft standard formats.
Requirements for Running the Samples
- Microsoft .NET Framework
- Windows Media Encoder 9 Series
- Interop.WMEncoderLib.dll and Interop.WMPREVIEWLib.dll present with the EXE files
The code contains five projects:
- A command-line utility to convert a video file. You give it a source file and a destination file. I made this basically for two purposes:
A batch conversion utility that automates the process of encoding a group of files without having to configure the encoding session after each file, eliminating the need to wait until encoding is completed before setting properties for the next file.Joining of two files into one media file. Limitations: It joins only files of the same size and type. Tested on the files present with the project: Source1.avi and Source2.avi.Splitting a media file into multiple media files. Limitations: Tested on the file present with the project: Source1.avi.Join 12 images to form a movie. The images are joined with a one second interval. So, in total, the resultant is a 12 sec. movie.
- The other application deals with batch conversion, so it is difficult to understand.
- Many a times, you need an application to do conversion on command line. You can use this application for that, or you can change it to a small library.
What are Profiles?
A profile specifies which codec to use for compression, and determines the number of output streams and their bit rates. Each profile targets a specific audience and destination. For example, one profile might use a quality based variable bit rate (VBR) for a file download, while another profile might use a constant bit rate (CBR) for streaming. Each profile is used for a specific number and type of source streams. Only one profile can be associated with a source group at a time, and the profile must be the same for all source groups in the encoding session.
By default, profiles are present in "C:\Program Files\Windows Media Components\Encoder\Profiles". You may create your own profiles for different audience and ship them with your application. Here is a sample on how to create a custom profile: MSDN.
What are DRM profiles?
DRM (Digital Rights Management) is used to for protecting your encoded content. Content is encoded and then encrypted with a key, and then a license is required for users to play the content. This license contains the key to unlock the content, and the rights that govern its use. For example, the license determines the number of times the content can be played or whether the license expires. See how to create/modify DRM profiles in MSDN.
What are Video Preprocessing Modes?
There are video preprocessing modes such as standard, deinterlacing, inverse telecine, and process interlacing.
What is Two-pass Encoding?
If the selected profile allows it, two-pass encoding can be used to improve the quality of the encoded content.
Running the Code
The console application, split file, and join file examples are fairly simple. So, I will mainly explain the batch encoding part.
- Select one or multiple files to encode, say C:\WINDOWS\clock.avi in the sources listbox. Give the path of the output folder. I have used
SHGetPathFromIDList functions from "shell32" for choosing a directory.
- Provide a prefix string in the output string textbox to identify all the batch outputs for this conversion.
- Choose a pre-defined profile from the "C:\Program Files\Windows Media Components\Encoder\Profiles" folder.
- Choose a preprocessor, Standard in most cases, unless the file format supports a different preprocessor.
- Choose a DRM profile, if available, or make a custom one using the above mentioned links.
- Enable cropping, if required.
- Add information on files, like author, description etc.
- Enable two-pass encoding, if required.
- Now, click the ADD button over the datagrid to add the sources for encoding.
- Click the START button to start encoding.
- If everything goes OK, the sources present in the datagrid will start encoding one after the other.
- You may check for any errors in the ErrorLog.xml file after checking Tools -> Log Errors.
Below is a high level process flow of how the major components interact with each other. It is fairly simple, and depicts the major components used and their interaction.
Using the Code
Batch encoding application
I have used a struct named
strucEncodeInfo for the input parameter of
sEncodeFile() to encode a batch. It contains the source file, destination file, the profile to be used, and the DRM profile for any information of the file like title, description, author and any copyright information. It also contains information on whether the encoded output needs to be cropped from any side, or if any kind of video preprocessing is required. Two-pass encoding is done if the profiles support and the boolean is true.
private struct strucEncodeInfo
public string Source;
public string Destination;
public string Profile;
public string DRMProfile;
public string Title;
public string Description;
public string Author;
public string Copyright;
public bool Crop;
public long CropLeft;
public long CropTop;
public long CropRight;
public long CropBottom;
public WMENC_VIDEO_OPTIMIZATION Preproc;
public bool TwoPass;
Create the global encoder object and attach the
OnStateChange event handler. So, when you start encoding, you will be informed via
enumState about the state of encoding:
WMEncoder glbEncoder = new WMEncoder();
sEnumDRMProfiles() finds out any DRM profiles present and fills the respective combobox.
sEnumPreprocess() fills the name of the preprocessors in the respective combobox. The encoding starts from the function
glbEncoder.Start(). Now it's time to check if it has been completed or not. The following
while loop waits for the
glbboolStartNext variable to be set to
true, and keeps the GUI responsive.
while (glbboolStartNext == false)
You may also save sessions and load them afterwards using the menu. You may also save a default session so that you do not need to specify the profiles, output path, and prefix again and again.
Command-line Single File Conversion
It consists of a minimal set of code from batch encoding.
Join Video Files
It consists of two sources in the Source group instead of one, and
srcGrp.SetAutoRollover (-1, "SG2"); tells the encoder to encode the second source after the first one is finished. Similarly, you can add as many sources as you want, and you may sequence them one after another or may overlap them.
The class which is used in this case is
WMEncBasicEdit. The input file is set in the
BasicEdit.MediaFile property and the output file is set in the
BasicEdit.MarkOut are set which indicate marking to cut a file from the starting and the ending position. Mark-in and Mark-out are calculated for different output files to split in equal parts.
Join Images to Form a Movie
It consists of n image sources in the Source group. As still images have no duration, I have used a timer to rollover from one source to another after a time interval. In this example, I have set a timer interval to 1 sec. For this example, I have used a lot of help from a post at microsoft.public.windowsmedia.encoder.
- This is the continuation of my previous article on Windows Media Encoder: Capture Activities on Screen in a Movie.
- Much help has been taken from a sample present in the Windows Media Encoder SDK in Visual Basic.
Side note: The code may not be foolproof, but it works.
- Added joining of files.
- Added splitting of file.
- Added flow diagram.
- Added Join Images to form a movie.