Click here to Skip to main content
15,902,634 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 384.7K   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

 
GeneralWMV Clipping Pin
gstaky2-Feb-05 7:43
gstaky2-Feb-05 7:43 
GeneralRe: WMV Clipping - Split Media files Pin
Armoghan Asif2-Feb-05 17:56
Armoghan Asif2-Feb-05 17:56 
GeneralEncoding Avi files in a pair!! Pin
desiboy75992-Jan-05 11:49
desiboy75992-Jan-05 11:49 
GeneralRe: Encoding Avi files in a pair!! Pin
Armoghan Asif6-Jan-05 18:58
Armoghan Asif6-Jan-05 18:58 
GeneralHow to Join / Merge Two AVI files Pin
6-Jan-05 21:36
suss6-Jan-05 21:36 
General:confused: Pin
Thomas F12-Dec-04 22:11
Thomas F12-Dec-04 22:11 
GeneralRe: :confused: Pin
Armoghan Asif13-Dec-04 1:35
Armoghan Asif13-Dec-04 1:35 
GeneralRe: :confused: Pin
Armoghan Asif19-Dec-04 17:12
Armoghan Asif19-Dec-04 17:12 
Features of WMV and WMA over other Multimedia standards

Windows Media Video 9 - WMV 9 features 15-30 percent performance and quality improvements over WMV 8, and three times the compression efficiency of MPEG-4. So how bad is MPEG-4, you ask? A 6 Mbps MPEG-4 movie, quality-wise, is comparable to a 2 Mbps WMV 9 movie, that's how bad. At 300 Kbps, WMV 9 makes QuickTime 6/MPEG-4 look silly. Even a 150 Kbps WMV 9 movie looks better than the same movie in 300 Kbps QT6 MPEG-4. It's embarrassing. Adding insult to injury, "Apple's MPEG-4 implementation is one of the worst out there," says Amir Majidimehr, the General Manager of Microsoft's Windows Digital Media Division. 'It is so bad we were worried that you [reviewers] would think we were faking it."

After three years of steady improvements, Microsoft has locked the WMV decoder syntax with this release (it locked WMA in version 8), ensuring that the next several generations of WMV will be backwards compatible. This means that consumer electronics devices will now appear with WMV support in silicon, including DVD players, Digital Video Recorders (DVRs) and the like. WMV 9 also adds video interlacing support, so that set-top boxes can get better compression, though PCs will still use higher-quality non-interlaced, progressive-scan technology; this happens on the fly and is automatic, Microsoft says.

A new WMV 9 feature called Video Smoothing further distances Microsoft's codec from the competition. What this technology does is pixel-by-pixel frame interpolation, which results in super-efficient video compression. As Majidimehr noted, Video Smoothing is a "non-trivial computer science problem," and Microsoft had to ensure that the performance and quality was substantial enough that they could drop bitrates and framerates while maintaining the same quality as the source material. Regardless of how they solved this problem, the results are amazing, letting content creators deliver high-quality video at very low bitrates. In a comparison of RealVideo at 24 FPS and WMV 9 at 12-24 FPS Variable Bit Rate(VBR), the RealVideo stream looked horrible (Or, "crap," as my notes read), while the WMV 9 video is only barely distinguishable from the 180 Kbps original. Same thing with 30 FPS RealVideo vs. 10-30 FPS VBR WMV. Game over.

Windows Media Video 9 Professional - WMV 9 Professional features software-only High-Definition (Hi-Def, 1080p) video playback on PCs for the first time. Before WMV 9 Pro, analysts predicted that Hi-Def quality video playback on PCs would require expensive new hardware components and new networking and storage technologies. No more: WMV 9 Pro delivers 1920 x 1080 (1080p) resolution video, which is about six times the resolution of today's DVD movies. And this content can be rendered and played back on mid-range Pentium 4 hardware, using just software. Microsoft demonstrated various Hi-Def-quality movies at the reviewers workshop, including clips from theatrical releases such as The Mummy Returns and Disney's Dinosaurs, which, yes, far surpassed any movie theatre experience audiences had watching these movies.

Another interesting use of WMV 9 is its slightly lower quality 720p resolution (1280 x 720) movie delivery with 5.1 discrete channels of surround sound (we'll hit the audio part of this in just a bit, below). Content creators can fit two such movies on a single DVD, significantly expanding the amount of content that can be delivered per disc. And the resulting movies, of course, are of even higher quality than the DVD movies you see today. Amazing? Yes, I think so.

Windows Media Video 9 Screen - Optimized for PC screen capture, WMV 9 Screen offers improved picture quality and CPU usage. "This is a new compression technology," Majidimehr said, "and we worked it up because compression has historically [underperformed] for training scenarios. If computer output compresses it looks blocky, and it's useless." WMV 9 Screen supports the limited palettes found on typical computer screens with lossless, streamable output at low resolutions. "It will not drop a pixel," Majidimehr noted. It's so good, in fact, that WMV 9 Screen movies featuring Web sites and photos work fairly well, an impossibility with lossy screen capture solutions like Camtasia.

Windows Media Video 9 Image - This is a new codec which enables still images to be transformed into full-motion video using pan and zoom effects. "This is cross-over technology," Majidimehr said. "We take a still imagine and transform it into full-motion video with pan and zoom effects, and cross-dissolve transitions that can be added between clips, using bit-rates as low as 20 Kbps." This is an awesome addition to the WMV arsenal, and a feature similar to one I've used on the Macintosh using a third party tool for QuickTime. Potential uses are enormous, but I've used this technology to create unique photo-based slideshows, which can be quite compelling.

Windows Media Audio 9 - On the audio side, WMA 9 features 20 percent performance and quality improvements over WMA 8 while maintaining the same decoded stream syntax. You might recall that, with WMA 8, you could realistically expect the quality of a 128 Kbps MP3 file in just 64 Kbps, or about half the encoding rate (and one-third to one-half the file size). With version 9, this has been improved to 48 Kbps. In fact, Microsoft claims that WMA 9 can deliver CD quality audio in less than 64 Kbps, about 1/20th the size of the CD original. This feat remains unmatched by the competition.

WMA 9 also supports VBR (variable bit recording) for the first time, as well. This lets users copying audio CD content in WMA format choose from simple quality levels rather than more technical-sounding encoding rates: The encoder will determine, on the fly, which encoding rate is the most appropriate, given the content that's being recorded, and the quality level that was requested.

But the most important point for existing WMA users is that WMA 9 is backwards compatible with today's players, devices, and any other decoders that support WMA 8, so there's no need to worry about upgrading to access new content.

Windows Media Audio 9 Professional - WMA 9 Professional is a new high-performance audio codec that supports better-than-CD quality resolution and targets 5.1 and 7.1 channel surround sound playback, WMA 9 Pro raises the bar for PC- and device-based audio playback. Microsoft says that it had to add multi-channel audio support to WMA because of the limitations of Dolby Digital, even in broadband scenarios. "Sony's Dolby Digital requires 384 Kbps of bandwidth [just for the sound], which doesn't leave much left over for the video stream on a 600 Kbps broadband connection," said Majidimehr. "So we had to design something more efficient. The target was to go down as low as we could, and deliver six discrete audio channels at 128 Kbps. That's about 20 Kbps per channel."

But then Microsoft looked at Dolby EX, which offers 6.1 channels of surround sound, so they went even higher and introduced a new WMA Professional mode with 7.1 discrete audio channels. But even 7.1 support is somewhat artificial, as WMA 9 Pro can technically expand beyond that as well. "There's not a lot of consumer demand beyond 7.1," Majidimehr said, "but we can have an unlimited number of channels. We just stopped at the edge of where we thought people cared." Cocky? Yeah. Exciting? Oh yeah.

To demonstrate WMA 9 Pro, Microsoft brought out a 192 Kbps MP3 copy of The Corrs' hit "Breathless," which was compared to a 6 channel WMA Pro mix of the same song. No contest. And a 7.1 version of Queen's epic "Bohemian Rhapsody"--"we did this mix ourselves," Majidimehr noted--was simply amazing.

Windows Media Audio 9 Lossless - WMA 9 Lossless is a new codec that supports mathematically lossless audio compression for audiophiles worried about ever-improving codecs. It's never getting any better than this, Microsoft notes. "Good enough ears will know the difference," Majidimehr said, "and those people don't care about small files. It's for peace of mind: Five years from now, their music doesn't have to be ripped again when a new format is released. Plus, we have huge hard drives now. You can fit 200 CDs worth of music in 50 GB using this codec."

WMA 9 Lossless features 24-bit, 96 KHz sound with 2:1 compression when compared to the CD original. And it's integrated right into Windows Media Player 9 so true audiophiles can get busy ripping CDs now. WMP 9 is the only media player to support a lossless CD ripping format.

Windows Media Audio 9 Voice - A unique new hybrid voice and music codec for AM radio-style audio streams over low-bandwidth networks, WMA 9 Voice is designed to do something that no other audio codec does well today: Play back streams that are primarily voice, but feature some music (through advertisements and intro pieces, primarily). Previous to WMA 9, Microsoft shipped the same proprietary voice codec that RealNetworks markets as RealVoice, but WMA 9 Voice offers a 20 percent compression improvement over that technology, and better sound quality. "Today's voice compressors butcher music," Majidimehr said, "And music compressors don't work on voice, s's get lispy, and there is echo." What WMA 9 Voice offers is a tiny 8 Kbps stream with AM-quality audio that sounds great with voice or music. This codec will help AM radio stations and other sources of voice content to get online quickly and cheaply.

ref:
http://www.winsupersite.com/reviews/wm9series.asp[^]
GeneralRe: :confused: Pin
Aaron Eldreth22-Dec-04 4:14
Aaron Eldreth22-Dec-04 4:14 

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.