Udostępnij za pośrednictwem


Getting started with the Expression Encoder Object Model (RTM Version)

This post is an update to the previous Getting started with the Expression Encoder Object Model post, but updated to work with the RTM version of Expression Encoder V2 as the progress event handler changed a little from Beta to RTM.

(This post assume you’ve already installed Expression Encoder V2 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. If you want Intellisense comments to appear and want to check out the help file, see this post.

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(object sender, EncodeProgressEventArgs 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.EncodeProgress 
    += new EventHandler<EncodeProgressEventArgs>(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.EncodeProgress 
                += new EventHandler<EncodeProgressEventArgs>(OnProgress);

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

        static void OnProgress(object sender, EncodeProgressEventArgs 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.

Note that 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.

Comments

  • Anonymous
    May 08, 2008
    PingBack from http://blogs.msdn.com/expressionencoder/archive/2008/04/22/8418128.aspx

  • Anonymous
    May 09, 2008
    What about live encoding? Is that possible with this object model?

  • Anonymous
    May 17, 2008
    NU AM MAI VORBIT DAR DORESC SA COMUNICAM PRIN INTERMEDIUL NET  . CE STUDII AI ?  UNDE LUCREZI  SI ASVREA SA SIU DE UNDE ESTI DIN CE TARA PROVII?ORASUL IN CARELOCUIESTI?

  • Anonymous
    May 17, 2008
    NU AM MAI VORBIT DAR DORESC SA COMUNICAM PRIN INTERMEDIUL NET  . CE STUDII AI ?  UNDE LUCREZI  SI ASVREA SA SIU DE UNDE ESTI DIN CE TARA PROVII?ORASUL IN CARELOCUIESTI?

  • Anonymous
    May 22, 2008
    Sorry, we don't currently support live encoding using the object model. Regards Dean.

  • Anonymous
    June 02, 2008
    The Expression Encoder 2 ReadMe states that the MPEG-2 decoder is not included in the Trial version? I downloaded the trial recently from MSDN. What do these users do if they want the MPEG-2 decoder? I especially don't want the buy the separate Encoder v2 version since I'll later be getting the Studio v2 as a licensed user of Visual Studio 2008 Team Suite.

  • Anonymous
    June 03, 2008
    Does the Job support async progress reporting?

  • Anonymous
    June 11, 2008
    You should be able to install a third party MPEG-2 encoder and have that work. Regards Dean.

  • Anonymous
    June 11, 2008
    Hi Andrew, The actual Encode method is synchronous but you can call it from another thread and get progress information. There is an example in the SDK (WPFEncode) that shows progress information as the Encode progresses. Regards Dean.

  • Anonymous
    January 12, 2009
    Hi is there a way to load the job preset xml file which I would save from the program so the encoding would be done with all the settings which are in the file?

  • Anonymous
    January 12, 2009
    Yes there is a method called ApplyPreset on the Job and the MediaItem classes. One applies it to the entire job and one applies it to the specific media item. Hope this helps. Dean.