Permalink
Switch branches/tags
Nothing to show
Find file
fd83728 Aug 26, 2017
@Juliako @kiwhit @v-anpasi @tysonn
192 lines (148 sloc) 9.57 KB
title description services documentationcenter author manager editor ms.assetid ms.service ms.workload ms.tgt_pltfrm ms.devlang ms.topic ms.date ms.author
Encode an asset with Media Encoder Standard using .NET | Microsoft Docs
This topic shows how to use .NET to encode an asset using Media Encoder Strandard.
media-services
juliako
cfowler
03431b64-5518-478a-a1c2-1de345999274
media-services
media
na
na
article
07/31/2017
juliako;anilmur

Encode an asset with Media Encoder Standard using .NET

Encoding jobs are one of the most common processing operations in Media Services. You create encoding jobs to convert media files from one encoding to another. When you encode, you can use the Media Services built-in Media Encoder. You can also use an encoder provided by a Media Services partner; third party encoders are available through the Azure Marketplace.

This topic shows how to use .NET to encode your assets with Media Encoder Standard (MES). Media Encoder Standard is configured using one of the encoder presets described here.

It is recommended to always encode your source files into an adaptive bitrate MP4 set and then convert the set to the desired format using the Dynamic Packaging.

If your output asset is storage encrypted, you must configure asset delivery policy. For more information see Configuring asset delivery policy.

[!NOTE] MES produces an output file with a name that contains the first 32 characters of the input file name. The name is based on what is specified in the preset file. For example, "FileName": "{Basename}_{Index}{Extension}". {Basename} is replaced by the first 32 characters of the input file name.

MES Formats

Formats and codecs

MES Presets

Media Encoder Standard is configured using one of the encoder presets described here.

Input and output metadata

When you encode an input asset (or assets) using MES, you get an output asset at the successful completion of that encode task. The output asset contains video, audio, thumbnails, manifest, etc. based on the encoding preset you use.

The output asset also contains a file with metadata about the input asset. The name of the metadata XML file has the following format: <asset_id>_metadata.xml (for example, 41114ad3-eb5e-4c57-8d92-5354e2b7d4a4_metadata.xml), where <asset_id> is the AssetId value of the input asset. The schema of this input metadata XML is described here.

The output asset also contains a file with metadata about the output asset. The name of the metadata XML file has the following format: <source_file_name>_manifest.xml (for example, BigBuckBunny_manifest.xml). The schema of this output metadata XML is described here.

If you want to examine either of the two metadata files, you can create a SAS locator and download the file to your local computer. You can find an example on how to create a SAS locator and download a file Using the Media Services .NET SDK Extensions.

Download sample

You can get and run a sample that shows how to encode with MES from here.

.NET sample code

The following code example uses Media Services .NET SDK to perform the following tasks:

  • Create an encoding job.
  • Get a reference to the Media Encoder Standard encoder.
  • Specify to use the Adaptive Streaming preset.
  • Add a single encoding task to the job.
  • Specify the input asset to be encoded.
  • Create an output asset that will contain the encoded asset.
  • Add an event handler to check the job progress.
  • Submit the job.

Create and configure a Visual Studio project

Set up your development environment and populate the app.config file with connection information, as described in Media Services development with .NET.

Example

    using System;
    using System.Linq;
    using System.Configuration;
    using System.IO;
    using System.Threading;
    using Microsoft.WindowsAzure.MediaServices.Client;

    namespace MediaEncoderStandardSample
    {
        class Program
        {
            private static readonly string _AADTenantDomain =
                ConfigurationManager.AppSettings["AADTenantDomain"];
            private static readonly string _RESTAPIEndpoint =
                ConfigurationManager.AppSettings["MediaServiceRESTAPIEndpoint"];

            // Field for service context.
            private static CloudMediaContext _context = null;

            private static readonly string _supportFiles =
                Path.GetFullPath(@"../..\Media");

            static void Main(string[] args)
            {
                var tokenCredentials = new AzureAdTokenCredentials(_AADTenantDomain, AzureEnvironments.AzureCloudEnvironment);
                var tokenProvider = new AzureAdTokenProvider(tokenCredentials);

                _context = new CloudMediaContext(new Uri(_RESTAPIEndpoint), tokenProvider);

                // Get an uploaded asset.
                var asset = _context.Assets.FirstOrDefault();

                // Encode and generate the output using the "Adaptive Streaming" preset.
                EncodeToAdaptiveBitrateMP4Set(asset);

                Console.ReadLine();
            }

            static public IAsset EncodeToAdaptiveBitrateMP4Set(IAsset asset)
            {
                // Declare a new job.
                IJob job = _context.Jobs.Create("Media Encoder Standard Job");
                // Get a media processor reference, and pass to it the name of the 
                // processor to use for the specific task.
                IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");

                // Create a task with the encoding details, using a string preset.
                // In this case "Adaptive Streaming" preset is used.
                ITask task = job.Tasks.AddNew("My encoding task",
                    processor,
                    "Adaptive Streaming",
                    TaskOptions.None);

                // Specify the input asset to be encoded.
                task.InputAssets.Add(asset);
                // Add an output asset to contain the results of the job. 
                // This output is specified as AssetCreationOptions.None, which 
                // means the output asset is not encrypted. 
                task.OutputAssets.AddNew("Output asset",
                    AssetCreationOptions.None);

                job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
                job.Submit();
                job.GetExecutionProgressTask(CancellationToken.None).Wait();

                return job.OutputMediaAssets[0];
            }

            private static void JobStateChanged(object sender, JobStateChangedEventArgs e)
            {
                Console.WriteLine("Job state changed event:");
                Console.WriteLine("  Previous state: " + e.PreviousState);
                Console.WriteLine("  Current state: " + e.CurrentState);
                switch (e.CurrentState)
                {
                    case JobState.Finished:
                        Console.WriteLine();
                        Console.WriteLine("Job is finished. Please wait while local tasks or downloads complete...");
                        break;
                    case JobState.Canceling:
                    case JobState.Queued:
                    case JobState.Scheduled:
                    case JobState.Processing:
                        Console.WriteLine("Please wait...\n");
                        break;
                    case JobState.Canceled:
                    case JobState.Error:

                        // Cast sender as a job.
                        IJob job = (IJob)sender;

                        // Display or log error details as needed.
                        break;
                    default:
                        break;
                }
            }

            private static IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
            {
                var processor = _context.MediaProcessors.Where(p => p.Name == mediaProcessorName).
                ToList().OrderBy(p => new Version(p.Version)).LastOrDefault();

                if (processor == null)
                    throw new ArgumentException(string.Format("Unknown media processor", mediaProcessorName));

                return processor;
            }
        }
    }

Media Services learning paths

[!INCLUDE media-services-learning-paths-include]

Provide feedback

[!INCLUDE media-services-user-voice-include]

Next steps

How to generate thumbnail using Media Encoder Standard with .NET Media Services Encoding Overview