IntroductionIn my last post about Smooth Streaming I explained the basic configuration that one needs to stream smooth videos. Just a quick reminder - all you need is Windows Server 2008, IIS, Media Services and Transform Manager to encode your video files to Smooth Streaming format. If you want to have more control over the encoding or need to incorporate it to a bespoke application, you need to go deeper.
Now, let's look into how to use the Expression Encoder Object Model.
Expression Encoder Object ModelUsing Expression Encoder Object Model is very simple and shouldn’t be a problem even for beginners. To encode a video I used two classes – MediaItem and Job from Microsoft.Expression.Encoder.dll assembly.
Assuming you already have an open Visual Studio project you have to add all necessary references, listed below (all files can be found in Expression Encoder installation directory).
Next write a simple conversion function – I named my Encode and you can check the listing below.
Since this is just a simple example I will only pass path to the video file and path to output directory as parameters. I highlighted some sections in the code, I will explain them below.
- Creating a new MediaItem object passing video file path as constructor parameter.
- Smooth Streaming must be enabled for selected Video Profile.
- Output streams are easy to define and add to video profile. In the example above I’m adding three bit rates with corresponding video dimensions. The more you add the longer the encoding will take but on the other hand with multiple streams you can offer the best possible viewing experience to the client.
- Expression Encoder can produce thumbnails.
- Job definition couldn’t be simpler – all needed is to add the MediaItem object, set the output directory and invoke job’s Encode function.
- Job object exposes several Events. I subscribed to EncodeCompleted event to know when the job is done. The handler may perform additional actions after job completion for example publish a page with encoded video in CMS or notify some external systems that conversion is over and that the video file is ready for viewing.