Expression Encoder

The team blog for Expression Encoder.

May, 2008

Posts
  • Expression Encoder

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

    • 11 Comments

    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.

  • Expression Encoder

    Help and Intellisense for the Expression Encoder Object Model.

    • 3 Comments

    We're busy working on a more official SDK and some samples that use the Expression Encoder object model, but in the meantime here's a link to a zip file that contains a couple of XML files that will enable Intellisense comments and a CHM help file.

    ExpressionEncoderOM.zip

     (UPDATE: I've also added the file as an attachment to this post in case the server that contains the first link isn't up).

    Once you've installed Expression Encoder 2 and downloaded and extracted the zip file, copy the two XML files to your "\Program Files\Microsoft Expression\Encoder 2\SDK" directory. The next time you create or load a project that uses the object model you should get descriptions for the methods and properties like below.

    Intellisense

    The CHM file is a help file that was actually built from the source code using Sandcastle. If you open the file and it just says "Navigation to the webpage was canceled" in the right pane you need to "Unblock" the file first. Right click the CHM file, select Properties and on the General tab click the Unblock button near the bottom right. Now reopen the file and everything should be fine.

    Help

    Hope you find these useful and let us know if you have any questions.

Page 1 of 1 (2 items)