Click here to Skip to main content
15,867,308 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 381.3K   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

 
Questionerror showig ..in split file...please tell how to correct that error Pin
vinaykn.rao17-May-12 21:17
vinaykn.rao17-May-12 21:17 
QuestionProblem in DRM profile and preprocessing selection and in browse button of output folder Pin
Member 78200334-Jan-12 9:34
Member 78200334-Jan-12 9:34 
GeneralAudio gets Compressed Pin
jugal_piet@indiatimes.com20-Jun-10 20:09
jugal_piet@indiatimes.com20-Jun-10 20:09 
GeneralFlash version of Movie Maker Pin
eiramave13-Apr-09 18:03
eiramave13-Apr-09 18:03 
GeneralRe: Flash version of Movie Maker Pin
Armoghan Asif14-Apr-09 21:21
Armoghan Asif14-Apr-09 21:21 
Generaltrouble encoding vob to wmv if it has audio Pin
davidluper15-Feb-09 14:41
davidluper15-Feb-09 14:41 
GeneralMarkin doesnt work. Pin
Member 47136735-Jan-09 22:59
Member 47136735-Jan-09 22:59 
GeneralNull refrence exception when run samples. Pin
Member 158232713-Nov-08 5:01
Member 158232713-Nov-08 5:01 
QuestionWhy is the entity of Windows Media Encoder required? Pin
Onur Guzel20-Oct-08 10:19
Onur Guzel20-Oct-08 10:19 
AnswerRe: Why is the entity of Windows Media Encoder required? Pin
Armoghan Asif22-Oct-08 3:32
Armoghan Asif22-Oct-08 3:32 
GeneralRe: Why is the entity of Windows Media Encoder required? Pin
Onur Guzel22-Oct-08 7:57
Onur Guzel22-Oct-08 7:57 
GeneralRe: Why is the entity of Windows Media Encoder required? Pin
Armoghan Asif27-Oct-08 23:23
Armoghan Asif27-Oct-08 23:23 
GeneralRe: Why is the entity of Windows Media Encoder required? Pin
Onur Guzel19-Jan-09 8:20
Onur Guzel19-Jan-09 8:20 
QuestionHow convert avi-file (sound) to WAV ? Pin
Vysokovskikh Sergey9-Oct-08 23:16
Vysokovskikh Sergey9-Oct-08 23:16 
QuestionImgs to movie - Problem when trying to use more then 1 frame per second Pin
Rossioli19-Aug-08 14:28
Rossioli19-Aug-08 14:28 
GeneralRe: Imgs to movie - Problem when trying to use more then 1 frame per second [modified] Pin
Rossioli19-Aug-08 15:27
Rossioli19-Aug-08 15:27 
QuestionHow to convert AVI and WMV to MPEG/MP4? Pin
Yiping Zou9-Jun-08 12:13
Yiping Zou9-Jun-08 12:13 
AnswerRe: How to convert AVI and WMV to MPEG/MP4? Pin
Armoghan Asif10-Jun-08 0:48
Armoghan Asif10-Jun-08 0:48 
GeneralJoining two videos Pin
alarkin7725-May-08 2:56
alarkin7725-May-08 2:56 
GeneralRe: Joining two videos Pin
Armoghan Asif25-May-08 21:52
Armoghan Asif25-May-08 21:52 
GeneralHi again Pin
circass28-Nov-07 13:38
circass28-Nov-07 13:38 
AnswerRe: Hi again Pin
Armoghan Asif30-Nov-07 1:15
Armoghan Asif30-Nov-07 1:15 
GeneralRe: Hi again Pin
circass1-Dec-07 3:38
circass1-Dec-07 3:38 
GeneralWMEncoder glbEncoder = new WMEncoder(); Pin
circass27-Jun-07 22:44
circass27-Jun-07 22:44 
GeneralRe: WMEncoder glbEncoder = new WMEncoder(); Pin
Armoghan Asif2-Jul-07 20:56
Armoghan Asif2-Jul-07 20:56 

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.