Export (0) Print
Expand All

Getting started

With Microsoft Expression Encoder, you can work with video by using the Expression Encoder object model (OM), which is based on the Microsoft .NET Object Model Framework. To work with the Expression Encoder OM, you must have both Expression Encoder and Microsoft Visual Studio 2005 or Microsoft Visual Studio 2008 installed on your computer.

NoteNote

You can use any integrated development environment (IDE) or text editor with a compiler to create applications that use the Expression Encoder OM.

You can access most Expression Encoder features through the OM. The OM is designed to make the functionality of Expression Encoder available without having to write much code. For example, the following C# code uses the Expression Encoder OM to create a job, add a video file to the job, and then encode the video.

static void Main(string[] args)
{
    MediaItem mediaItem = new MediaItem(@"C:\videoInput\video.wmv");

    //create job and media item for video to encode
    Job job = new Job();
    job.MediaItems.Add(mediaItem);

    //set output directory
    job.OutputDirectory = @"C:\videoOutput";

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

    //encode
    job.Encode();
}

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

If you follow the comments in the code, you can see the outline of the steps for encoding a video by using C# and the Expression Encoder OM. The preceding code has five steps:

  1. Identify the media sources that you want to process.

  2. Create a job to process the media sources, and then add the media sources.

  3. Optionally, add a progress callback function to view the progress of the file encoding.

  4. Identify the location for the output.

  5. Execute the project.

Before you test this code for yourself, make sure that both Expression Encoder and either Visual Studio 2005 or Visual Studio 2008 are installed on your computer.

To create a project in Visual Studio

  1. In Visual Studio, click File, point to New, and then click Project.

  2. For this example, in the Project dialog box, under Visual C# in the Project Types list, click Windows. Under Templates, click Console Application.

  3. In the Name text box, type a name for your project. For this example, use MyEncoderApplication.

  4. Click OK.

Before you can use the Expression Encoder OM in Visual Studio, you must add references to the Expression Encoder assemblies.

To add the Expression Encoder assemblies

  1. In Visual Studio, click Project, and then click Add Reference.

  2. In the Add Reference dialog box, press and hold the CTRL key, and then click Microsoft.Expression.Encoder, Microsoft.Expression.Encoder.Types, Microsoft.Expression.Encoder.Utilities, and WindowsBase.

    NoteNote

    Because some Expression Encoder types derive from the ObservableCollection, you must also add a reference to WindowsBase.

  3. Click OK.

Now that you have added the references to the Expression Encoder assemblies, you are ready to start coding.

In Visual Studio, use the using statement to declare that the Expression Encoder namespace is being used. At the top of the file that you just created, locate the other using statements, and then type using Microsoft.Expression.Encoder. The code at the top of the page should now resemble the following.

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

Next, create an instance of a MediaItem class. The MediaItem class passes the file name to the constructor, which enables you to locate a video or audio file, extract relevant information about the file, and encode it. Locate the following code.

namespace MyEncoderApplication
{
    class Program
    {
        static void Main(string[] args)
        {

After the curly brace, add the following declaration, replacing the generic path with the path of the video that you want to encode.

MediaItem mediaItem = new MediaItem(@"c:\[file path]\[file name]");
NoteNote

Now that you have declared the MediaItem class, you can access the properties and methods associated with that item. Type mediaItem followed by a period (.) to access the IntelliSense list of properties and methods associated with the file, such as FileDuration, OriginalAspectRatio, and OriginalVideoSize.

Now that you have declared the MediaItem class, your next steps are to create a job and then add the media file to the job. After the MediaItem declaration, add the following code.

//create job and media item for video to encode
Job job = new Job();
job.MediaItems.Add(mediaItem);

If you want to encode multiple items, you must declare multiple MediaItem classes and then repeat job.MediaItems.Add(mediaItem); until you add all the files that you want to encode. Each MediaItem class will need a unique name, such as mediaItem1 and mediaItem2.

Before you begin encoding the files, set the directory in which to save the encoded files by setting the OutputDirectory property.

In your code, after the list of media items that include your job, add the following code.

//set output directory
job.OutputDirectory = @"C:\[file path]";

By default, Expression Encoder creates subfolders in the directory in which it stores the encoded files. You can stop Expression Encoder from creating subfolders (and therefore saving the files in the directory itself) by changing the CreateSubfolder property. To change the CreateSubfolder property, type job.CreateSubfolder = false;. For this example, use the default setting, "true."

If you want to show the progress of your encoding project, you can add a progress event handler function. Define the function by adding the following code after the job.Encode function and the closing curly brace.

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

Scroll up several lines in the code and add a call to this function after the job is created. The following call to the function appears after the output directory and before the call to encode.

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

Now you're ready to encode the file. Add the following code after the OutputDirectory declaration (and the CreateSubfolder declaration, if you chose to include it).

//encode
job.Encode();

Now you're ready to compile and run the application. On the Debug menu, click Start Without Debugging. The Command Prompt window appears. When the command prompt in the window reads Press any key to continue, the encoding is finished. If you open the output directory, you'll see a new file that has the same name as the original, but with a .wmv file name extension.

NoteNote

If the application fails, double-check the file name and file path for the video that you want to encode. If the file path and file name are correct, you can debug your application by clicking Start Debugging on the Debug menu.

The final code resembles the code at the start of this topic.

static void Main(string[] args)
{
    MediaItem mediaItem = new MediaItem(@"C:\videoInput\video.wmv");

    //create job and media item for video to encode
    Job job = new Job();
    job.MediaItems.Add(mediaItem);

    //set output directory
    job.OutputDirectory = @"C:\videoOutput";

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

    //encode
    job.Encode();
}

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

This time, when you run the application, you will see the progress of the project being encoded in the Command Prompt window.

Progress displayed in Command Window

You have just encoded your video by using the Expression Encoder OM.

   © 2010 Microsoft Corporation. All rights reserved.
Show:
© 2014 Microsoft