Encoding assets in Microsoft Azure Media Services from .NET

Another task that provides Azure Media Services is encoding the assets we have in our account. As I said in the introductory post, it is possible to perform this task through the portal itself, using the bottom menu (ENCODE):

We can also perform this same task via .NET following these steps:

using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace EncodingAssets
{
    class Program
    {
        static void Main(string[] args)
        {
            //0. Constants
            const string accountName = "gismedia";
            const string accountKey = "HTT3sfeRvTXIGlGdJU7EVQVRWrKSfV0q0tHnvCHxrVA=";
            var assetName = "WOW_Mists_of_Pandaria_MP4";
            var OutputAssetName = "WOW_Mists_of_Pandaria_H264_Smooth";
            var encodingPreset = "H264 Smooth Streaming 720p";
            //1. Nuget: Install-Package windowsazure.mediaservices
            //2. Create context
            CloudMediaContext _context = new CloudMediaContext(accountName, accountKey);
            //3. Get asset
            var asset = _context.Assets.Where(a => a.Name == assetName).FirstOrDefault();
            //4. Declare a job
            var job = _context.Jobs.Create(string.Format("Encoding {0} to {1}", assetName, encodingPreset));
            //5. Get processor
            var processor = _context.MediaProcessors.Where(p => p.Name == "Windows Azure Media Encoder").ToList().OrderBy(wame => new Version(wame.Version)).LastOrDefault();
            //6. Create task
            var task = job.Tasks.AddNew("Encoding task", processor, encodingPreset, TaskOptions.None);
            //7. Specify the asset
            task.InputAssets.Add(asset);
            //8. Output asset with the result
            task.OutputAssets.AddNew(OutputAssetName, AssetCreationOptions.None);
            //9. Handle to check the progress
            job.StateChanged += StateChanged;
            job.Submit();
            //10. Check job execution and wait for job to finish
            Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
            progressJobTask.Wait();
            //11. Check final status
            Console.WriteLine("Job final state: {0}", job.State);
            Console.ReadLine();
        }
        static void StateChanged(object sender, JobStateChangedEventArgs e)
        {
            Console.WriteLine("Job stated change event:");
            Console.WriteLine("  Previous state: {0}", e.PreviousState);
            Console.WriteLine("  Current state: {0}", e.CurrentState);
        }
    }
}
  1. Constants: Azure Media Services account, asset name we want to code, name of the resulting asset, the preset we want to use (use the name as listed on the website when we perform the task from there:
    Presets
  2. Install windowsazure.mediaservices via Nuget
  3. Connect the application with the account.
  4. Get the asset that we want to encode. In this example, we get the asset through the name but the best way to recover it is through the Id, since the name of the assets can be repeated.
  5. Create a job: It will be responsible for managing the task.
  6. Get the processor Windows Azure Media Encoder that will perform the task of encoding.
  7. Create a task within the job. We can have multiple tasks within a job, which will be launched sequentially.
  8. Add to the task which is the asset on which we want to perform the operation.
  9. Choose a name for the final asset.
  10. Associate a handler to StateChanged event in order to know what is happening with the job.
  11. Wait to complete the task
  12. Check the result of the work once completed.

If you launch the example, we see the following output on the console:

Encoding output

From the portal you can see that in the CONTENT section have a new asset with the new name (in this case WOW_Mists_of_Pandaria_H264_Smooth) and, if we publish it through the bottom menu, you will see that the format of the URL corresponds with Smooth Streaming format:

http://[ACCOUNT_NAME].origin.mediaservices.windows.net/[LOCATOR_ID]/Mists_of_Pandaria.ism/Manifest

Hope this helps.

Happy encoding!