Previously I’ve summarised the media upload process from client apps into a video on-demand service that uses Azure Media Services.
In this blog post I’ll summarise how to incorporate Media Services’ encoding and processing functionality into an app. To do this you create processing jobs that enable you to schedule and automate the encoding and processing of assets.
Encoding and processing media
Media services provides a number of media processors that enable media to be processed. Media processors handle a specific processing task, such as encoding, format conversion, encrypting, or decrypting content. Encoding video is the most common Media Services processing operation, and is performed by the Azure Media Encoder. The Media Encoder is configured using encoder preset strings, with each preset specifying a group of settings required for the encoder. For a list of all the presets see Azure Media Encoder Presets.
Media Services supports progressive download of video and streaming. When encoding for progressive download you encode to a single bitrate. To be able to stream content it must first be converted into a streaming format. There are two types of streaming offered by Media Services:
- Single bitrate streaming
- Adaptive bitrate streaming
With single bitrate streaming a video is encoded to a single bitrate stream and divided into chunks. The stream is delivered to the client one chunk at a time. The chunk is displayed and the client then requests the next chunk. When encoding for adaptive bitrate streaming you encode to an MP4 bitrate set that creates a number of different bitrate streams. These streams are also broken into chunks. However, adaptive bitrate technologies allow the client to determine network conditions and select from among several bitrates. When network conditions degrade, the client can select a lower bitrate allowing the video to continue to play at a lower quality. Once network conditions improve the client can switch back to a higher bitrate with improved quality.
Media Services supports three adaptive bitrate streaming technologies:
- Smooth streaming, created by Microsoft
- HTTP Live Streaming (HLS), created by Apple
- MPEG-DASH, an ISO standard
Accessing media processors
Processing jobs involve calling a specific media process to process the job. Media Services supports the following media processors:
Media processor | Description |
Azure Media Encoder | Allows you to run encoding tasks using the Media Encoder, |
Azure Media Packager | Allows you to convert media assets from MP4 to Smooth Streaming format, and Smooth Streaming assets to HLS format.. |
Azure Media Encryptor | Allows you to encrypt media assets using PlayReady protection. |
Storage Decryption | Allows you to decrypt media assets that were encrypted using storage encryption. |
To use a specific media processor you should pass the name of the processor into the GetLatestMediaProcessorByName method.
private IMediaProcessor GetLatestMediaProcessorByName(string mediaProcessorName)
{
var processor = this.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: {0}", mediaProcessorName));
}
return processor;
}
This method retrieves the specified media processor and returns an instance of it. It can be invoked as follows:
IMediaProcessor mediaProcessor = this.GetLatestMediaProcessorByName(MediaProcessorNames.WindowsAzureMediaEncoder);
Creating encoding jobs
After media has been uploaded into Media Services it can be encoded into one of the formats supported by the Media Services Encoder. The Media Services Encoder supports encoding using the H.264 and VC-1 codecs, and can generate MP4 and Smooth Streaming content. However, MP4 and Smooth Streaming content can be converted to HLS v3 or MPEG-DASH by using dynamic packaging.
Encoding jobs are created and controlled using a Job. Each Job contains metadata about the processing to be performed, and contains one or more Tasks that specify a processing task, its input Assets, output Assets, and a media processor and its settings. Tasks within a Job can be chained together, where the output asset of one task is given as the input asset to the next task. By following this approach one Job can contain all of the processing required for a media presentation.
The following diagram shows a high-level overview of the media encoding process used in the Building an On-Demand Video Service with Microsoft Azure Media Services project.
The EncodingService class retrieves the asset details from the CMS database and passes the encoding job to Media Services, where it’s submitted to the Azure Media Encoder. The encoding job and video details are saved to the CMS database while the Media Encoder processes the job, retrieving the input asset from Azure Storage, and writing the output assets to Azure Storage. When encoding is complete Media Services notifies the EncodingService class, which generate locator URLS to the output assets in Azure Storage, and updates the encoding job and video details in the CMS database. For a code walkthrough of this process see Encoding process in the Contoso Azure Media Services web service.
By default, each Media Services account can have one active encoding task at a time. However, you can reserve encoding units that allow you to have multiple encoding tasks running concurrently. For more information see How to Scale a Media Service.
Accessing encoded media
Accessing content in Media Services requires a locator, which combines the URL to the media file with a set of time-based access permissions. There are two types of locators – shared access signature (SAS) locators and on-demand origin locators.
A SAS locator grants access rights to a specific media asset through a URL. By using the URL you are granting users who have the URL access to a specific resource for a period of time, in addition to specifying what operations can be performed on the resource.
On-demand origin locators are used when streaming content to a client app, and are exposed by the Media Services Origin Service which pulls the content from Azure Storage and delivers it to the client. An on-demand origin locator URL will point to a streaming manifest file in asset. For more information about the Origin Service see Origin Service.
Summary
This blog post has summarised how to use the Azure Media Encoder to encode uploaded media for delivery to client apps.To do this you create a media processing job that enables you to schedule and automate the encoding of assets. For more info see Building an On-Demand Video Service with Microsoft Azure Media Services.
In my next blog post I’ll discuss the final step in the Media Services workflow – delivering and consuming media.