Click here to Skip to main content
15,880,725 members
Articles / Programming Languages / C#
Article

Convert MP3, MPEG, AVI to Windows Media Formats

Rate me:
Please Sign up or sign in to vote.
4.34/5 (27 votes)
30 May 2005CPOL8 min read 382.2K   11.8K   113   114
Video conversion has never been this simple. Use the new Windows Media Encoder to convert different video and audio formats to stream Microsoft audio video standard formats like WMV and WMA.

Image 1

Image 2

Introduction

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

Features

The code contains five projects:

  1. 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:
    • 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.
  2. 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.
  3. 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.
  4. Splitting a media file into multiple media files. Limitations: Tested on the file present with the project: Source1.avi.
  5. 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.

Main Terminologies

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 SHBrowseForFolder and 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.

Process Flow

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.

Image 3

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.

C#
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:

C#
WMEncoder glbEncoder = new WMEncoder();
glbEncoder.OnStateChange+=new 
  _IWMEncoderEvents_OnStateChangeEventHandler(this.Encoder_OnStateChange);

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 sEncodeFile() with 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.

C#
while (glbboolStartNext == false) 
{
    Application.DoEvents();
}

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.

Split File

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.OutputFile property. BasicEdit.MarkIn and 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.

References

  • 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.

Revision History

  • 25-05-2005
    • Added joining of files.
    • Added splitting of file.
    • Added flow diagram.
    • Added Join Images to form a movie.
  • 12-12-2004
    • Original article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionConverting an incoming stream Pin
mladen10-Jul-06 4:58
mladen10-Jul-06 4:58 
AnswerRe: Converting an incoming stream Pin
Armoghan Asif10-Jul-06 20:31
Armoghan Asif10-Jul-06 20:31 
GeneralRe: Converting an incoming stream Pin
mladen10-Jul-06 22:28
mladen10-Jul-06 22:28 
GeneralAdditional data Pin
vijay4all8-Apr-06 8:46
vijay4all8-Apr-06 8:46 
GeneralRe: Additional data Pin
Armoghan Asif9-Apr-06 19:32
Armoghan Asif9-Apr-06 19:32 
GeneralCOMException during WMEncoder creation Pin
wfrigerio6-Feb-06 21:04
wfrigerio6-Feb-06 21:04 
GeneralRe: COMException during WMEncoder creation Pin
Franklin van Velthuizen22-May-06 3:54
Franklin van Velthuizen22-May-06 3:54 
GeneralConverting MP3 to WMA Pin
Cutiepie27-Sep-05 19:17
Cutiepie27-Sep-05 19:17 
GeneralRe: Converting MP3 to WMA Pin
Armoghan Asif27-Sep-05 22:30
Armoghan Asif27-Sep-05 22:30 
GeneralRe: Converting MP3 to WMA Pin
Cutiepie27-Sep-05 23:46
Cutiepie27-Sep-05 23:46 
QuestionImages to Movie Pin
Mr. Naveed Butt6-Sep-05 20:08
professionalMr. Naveed Butt6-Sep-05 20:08 
AnswerRe: Images to Movie Pin
Armoghan Asif6-Sep-05 21:17
Armoghan Asif6-Sep-05 21:17 
GeneralRe: Images to Movie Pin
Mr. Naveed Butt7-Sep-05 20:49
professionalMr. Naveed Butt7-Sep-05 20:49 
GeneralRe: Images to Movie Pin
Armoghan Asif8-Sep-05 5:46
Armoghan Asif8-Sep-05 5:46 
GeneralConvertSingleFile.exe never finishes Pin
Member 221773830-Aug-05 7:06
Member 221773830-Aug-05 7:06 
GeneralRe: ConvertSingleFile.exe never finishes Pin
Armoghan Asif31-Aug-05 21:03
Armoghan Asif31-Aug-05 21:03 
GeneralASSERT Failed Pin
adeel223-Jul-05 11:13
adeel223-Jul-05 11:13 
GeneralNeed HELP Pin
adeel215-Jul-05 23:33
adeel215-Jul-05 23:33 
GeneralRe: Need HELP Pin
Armoghan Asif17-Jul-05 21:21
Armoghan Asif17-Jul-05 21:21 
GeneralRe: Need HELP Pin
adeel219-Jul-05 11:20
adeel219-Jul-05 11:20 
GeneralRe: Need HELP Pin
Armoghan Asif20-Jul-05 0:12
Armoghan Asif20-Jul-05 0:12 
GeneralMemory gets allocated but never released Pin
HocMan29-Jun-05 17:42
HocMan29-Jun-05 17:42 
GeneralRe: Memory gets allocated but never released Pin
Armoghan Asif7-Jul-05 18:31
Armoghan Asif7-Jul-05 18:31 
GeneralRe: Memory gets allocated but never released Pin
HocMan7-Jul-05 19:40
HocMan7-Jul-05 19:40 
General2 - pass encoding for wma files Pin
richard_brookes21-Jun-05 18:45
richard_brookes21-Jun-05 18:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.