(UPDATE: This post was written against the Beta of Expression Encoder v2. Now that the RTM version is out we recommend that you install that version and check out the RTM version of this post instead)

(This post assume you’ve already installed Expression Encoder Beta 2 available from here, and that you have Visual Studio 2005 or 2008 installed.)

First of all start Visual Studio and select File… New Project… You can choose which type of application you would prefer, like a WPF or Windows Forms application. For my test here, I’m just going to choose a Console Application. Give it a suitable name.

image

Go ahead and click OK.

Before you can start coding against the Expression Encoder object model you need to add references to the applicable assemblies. Select Project…Add Reference from the menu. When the dialog comes up you need to select

Microsoft.Expression.Encoder
Microsoft.Expression.Encoder.Types
Microsoft.Expression.Encoder.Utilites

and because some of our types derive from ObservableCollection you also need to select

WindowsBase.

You can select them one at a time or hold down CTRL and multi-select each one before clicking OK.

image

Now we're ready to do some coding.

To make it easier to pick up the Encoder classes let's first of all add a using statement. Just add the following near the top of the file where the other using statements are.

using Microsoft.Expression.Encoder;

One of the main classes that you'll end up dealing with in Expression Encoder is the MediaItem class. This allows you to find out information about a video or audio file and ultimately allow you to encode it. To create one of these you just pass the filename of the media file you want.to the constructor.

e.g.
MediaItem mediaItem = new MediaItem(@"C:\input\video.avi");

Obviously, you'll change the filename to the one you want to use.

Once this has been created you'll be able to access a bunch of information about the file

e.g.
FileDuration
OriginalAspectRatio
OriginalVideoSize

This is also the way you'll control how the video is encoded. If you type mediaItem followed by a "." you should trigger the Intellisense drop down which shows you all the properties and methods on this item. You should see a lot of properties that correspond to options in the UI of the main Encoder application.

For now we'll just use the defaults.

Before we can encode the file we need to create and add it to a Job.

This is simply done by the following statements.

Job job = new Job();
job.MediaItems.Add(mediaItem);

If you want to encode multiple files in the same job you can go ahead and create multiple MediaItem's and add them to the job.

Before you encode you need to set the directory where the output will go. This is done by setting the OutputDirectory property.

job.OutputDirectory = @"C:\output";

By default Expression Encoder will create output in sub folders named using the Job Id. These will be something along the lines of your machine name followed by the current date and time, e.g. "DEAN 4-21-2008 11.00.47 PM".

You can turn this feature off by setting the CreateSubfolder property.

job.CreateSubfolder = false;

Then the output will be created in the output directory directly.

For the simplest case, that’s all you need before encoding the job. This is done by just calling the Encode method.

job.Encode();

Putting it all together your program should look something like this.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Expression.Encoder;

namespace MyEncoderApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MediaItem mediaItem = new MediaItem(@"C:\input\video.avi");


// Create a job and the media item for the video we wish // to encode.
Job job = new Job(); job.MediaItems.Add(mediaItem);
            // Set the output directory and encode
            job.OutputDirectory = @"C:\output";
            job.Encode();
        }
    }
}

Now you should be able to compile and run the application. After it's finished running, you should have the encoded output file underneath the output directory. It should have a filename that matches the original, but with a .wmv file extension.

If you run the application and it crashes, then that is probably because an exception was thrown that is not currently being caught. For example if your MediaItem points to a file that doesn't exist or isn't a valid media file, you will see that an InvalidMediaFileException is thrown. You can then run it under the debugger to get more details.

Assuming everything worked OK then for your next step you'll probably want to show some progress during the encode. First of all you need to a add a progress event handler function. Something like the following

static void OnProgress(objectsender, PublishProgressEventArgs e)
{
    Console.WriteLine(e.Progress);
}

Obviously here we're just dumping the progress to the screen on each line. If you were writing a GUI app you would probably update a progress bar or something similar.

To tell Encoder to call this function you need the following line when you're creating the job.

job.PublishProgress +=
    new EventHandler<PublishProgressEventArgs>(OnProgress);

Now you’re code should look something like the following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Expression.Encoder;

namespace MyEncoderApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            MediaItem mediaItem = new MediaItem(@"C:\input\video.avi");
 
            // Create a job and the media item for the video we wish 
            // to encode.
            Job job = new Job();
            job.MediaItems.Add(mediaItem);

            // Set up the progress callback function
            job.PublishProgress +=
                new EventHandler<PublishProgressEventArgs>(OnProgress);

            // Set the output directory and encode.
            job.OutputDirectory = @"C:\output";
            job.Encode();
        }

        static void OnProgress(object sender, 
            PublishProgressEventArgs e)
        {
            Console.WriteLine(e.Progress);
        }
    }
}

When you compile and run the application you should now see a bunch of numeric values going to 100, something like the following.

clip_image002

That’s it for a basic encoding app. From here you can now try experimenting with various properties on the MediaItem. For example, if you want to try tweaking the output profile, you can look at this post – Changing the output profile.

UPDATE: If you’re running a 64-bit OS then you may also need to change your Target Platform within Visual Studio from “Any CPU” to "x86” as Expression Encoder is 32-bit.