Silverlight Journal

December 8, 2009

My First Expression Encoder App

Filed under: Uncategorized — Tags: , , , , — bobbsmooth @ 8:59 pm

Problem Background

On my current project, I need to re-encode about 7000 videos into a Smooth Streaming format so that I can take advantage of the functionality in Silverlight’s MediaElement control. The last time I did a big batch of encoding, I simply scripted Windows Media Sever 9. With Expression Encoder 3, however, that’s not possible. Instead you must use the API and create an application to do the work. While not terribly difficult, this project definitely took me outside my comfort zone. There are very few references out there so I hope this helps shed some more light on the solutions that can be built.

Solution

I decided that the best thing to do first was to create a simple application that would encode one video. Then I could expand it to use something like a database or an XML file to script the encoding process.

The first step was to install Expression Encoder 3. I needed the full version because the free version doesn’t support Smooth Streaming.

Next, I started a new WPF solution and added references to all of the Microsoft.Expression.Encoder libraries.

Library References

Project References

In my Window1.asmx file, I created some simple controls for finding a file and starting the process.

<Window x:Class="SingleFileEncoder.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="557">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>
            <RowDefinition Height="25"></RowDefinition>

        </Grid.RowDefinitions>
        <StackPanel Orientation="Horizontal" Grid.Row="0">
            <TextBlock Text="Source Video:" VerticalAlignment="Center">
                <TextBlock.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBlock.Margin>
            </TextBlock>
            <TextBox Height="23" Name="txtFileName" VerticalAlignment="Center" Width="300" BorderThickness="1">
                
                <TextBox.BorderBrush>
                    <SolidColorBrush Color="Blue" ></SolidColorBrush>
                </TextBox.BorderBrush>
                <TextBox.Background>
                    <LinearGradientBrush StartPoint=".75,0" EndPoint="0,.5">
                        <GradientStop Color="#CCCCFF" Offset="1" />
                        <GradientStop Color="White" Offset=".25" />
                    </LinearGradientBrush>
                </TextBox.Background>
                <TextBox.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </TextBox.Margin>
            </TextBox>
            <Button Height="23" HorizontalAlignment="Right" Name="btnBrowse" VerticalAlignment="Center" Width="75" Click="btnBrowse_Click">Browse
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
            <Button Name="btnEncode" Click="btnEncode_Click">
                Encode
                <Button.Margin>
                    <Thickness>
                        <Thickness.Left>5</Thickness.Left>
                    </Thickness>
                </Button.Margin>
            </Button>
        </StackPanel>
        <ProgressBar Grid.Row="1" Name="progressBar" Minimum="0" Maximum="100"></ProgressBar>
        <TextBlock Name="txtMessage" Grid.Row="2"></TextBlock>
    </Grid>
</Window>

In the code behind file, I added using statements for the Encoder namespaces.

 using System;
 using System.Windows;
 using Microsoft.Expression.Encoder;
 using Microsoft.Expression.Encoder.Profiles;
 using Microsoft.Win32;
 

I used the Win32 OpenFileDialog to find a file when the user clicks the Browse button.

private void btnBrowse_Click(object sender, RoutedEventArgs e)
 {
 OpenFileDialog dialog = new OpenFileDialog();
 dialog.ShowDialog();
 txtFileName.Text = dialog.FileName;
 }

In the encode button’s even handler, I assigned that file to a new MediaItem.
MediaItem mediaItem;
mediaItem = new MediaItem(txtFileName.Text);

Next, I created the output profile for my MediaItem. This technique is new to Encoder 3.

 AdvancedVC1VideoProfile videoProfile = new AdvancedVC1VideoProfile
 {
 Bitrate = new VariableConstrainedBitrate(403, 600),
 Complexity = VideoComplexity.Fastest,
 SmoothStreaming = true,
 Size = new System.Drawing.Size(236, 176),
 KeyFrameDistance = new TimeSpan(0, 0, 2),
 InLoopFilter = true
 };
mediaItem.OutputFormat = new WindowsMediaOutputFormat()
 {
 VideoProfile = videoProfile
 };

Next, I created a Job and assigned the MediaItem to it.

 Microsoft.Expression.Encoder.Job job = new Job();
 job.MediaItems.Add(mediaItem);
 job.OutputDirectory = @"C:\EncodedContent\";
 job.CreateSubfolder = false;

I assigned a couple of event handlers that would let me see progress as it was made and notify me when the encoding was complete.

job.EncodeProgress += new EventHandler<EncodeProgressEventArgs>(job_EncodeProgress);
 job.EncodeCompleted += new EventHandler<EncodeCompletedEventArgs>(job_EncodeCompleted);

Finally, I fire the encode process for the Job.

job.Encode();

Advertisement

1 Comment »

  1. To do multiple bitrates for video, add this code after initializing the VideoProfile:

    videoProfile.Streams.Add(new VariableConstrainedBitrate(708, 800), new System.Drawing.Size(364, 272));

    Comment by bobbsmooth — December 8, 2009 @ 11:13 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.