Eksportér (0) Udskriv
Udvid alt
EN
Dette indhold er ikke tilgængeligt på dit sprog, men her er den engelske version.

Creating an Encoding Job with the Media Services SDK for .NET

Updated: July 8, 2014

In order to deliver digital video over the internet you must compress the media. Digital video files are quite large and may be too big to deliver over the internet or for your customers’ devices to display properly. Encoding is the process of compressing video and audio so your customers can view your media.

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. You can specify the details of encoding tasks by using preset strings defined for your encoder, or by using preset configuration files. To see the types of presets that are available, see Task Presets for Azure Media Services. If you used a third party encoder, you should validate your files.

When working with Media Services, one of the common scenarios is:

  1. Upload an input file (called a mezzanine file). For example, H.264, MP4, or WMV (for the list of supported formats see Formats Supported by the Media Services Encoder).

  2. Encode your mezzanine file to H.264 MP4 adaptive bitrate sets.

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

    If you want to encode your mezzanine file directly to Smooth Streaming, you can use any of “Smooth Streaming” presets described in Media Services Encoder System Presets. However, it is not recommended unless your mezzanine file is very small.

    If you want to convert a WMV file to Smooth Streaming, you must first encode your file to H.264. WMV is a video codec that typically has an ASF container format. H.264 is a video codec that can be used with the MP4 container format. Smooth Streaming uses a variant of MP4 called fragmented MP4 or f-mp4. Smooth streaming is an adaptive streaming format that requires a set of files of different bitrates and all encoded with fragments that are aligned across the bitrates. A set of MP4 files that are encoded with aligned fragments can be converted to f-mp4 without requiring a re-encode. This is not the case with WMV files. For more information, see Smooth Streaming Technical Overview.  

  3. Publish the asset that contains the adaptive bitrate MP4 set by creating the On-Demand Locator.

  4. Deliver your content to clients. Use Dynamic Packaging to deliver appropriate formats of your content (for example, MPEG DASH, Smooth Streaming, and HLS).

    If you want to protect your content, see the topics in the Securing Media section.

This topic shows how to upload an MP4 file and encode it into H.264 MP4 adaptive bitrate sets. It also shows how to create MPEG DASH, HLS, and Smooth Streaming URLs.

Set up your project

The following topics show how to setup for Media Services development.

  1. Setup for Development with the Media Services SDK for .NET

    Note that code in this topic uses Azure Media Services .NET SDK Extensions.  The Media Services .NET SDK Extensions is a set of extension methods and helper functions that simplify your code and make it easier to develop with Media Services.

  2. Connect to Media Services Using the Media Services SDK

Upload an MP4 file and produce H.264 MP4 adaptive bitrate sets using the Azure Media Encoder

The following example shows how to create an encoding job based on the "H264 Adaptive Bitrate MP4 Set 720p" preset string. You can find a list of preset strings in the topic Media Services Encoder System Presets.

The code in this section does the following:

  1. Uploads a single .mp4 file.

  2. Encodes the .mp4 file into a set of adaptive bitrate MP4s.

    Once you encoded your asset to H.264 adaptive bitrate set, you can take advantage of dynamic packaging. Dynamic Packaging allows you to deliver streams in the specified protocol without further packaging.

  3. Publishes the asset by creating the On-Demand Locator.

  4. Gets MPEG DASH, Smooth Streaming, and HLS streaming URLs. For more information, see Delivering Assets with the Media Services SDK for .NET.

Make sure to update the code to point to the folder where your input MP4 file is located.

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

private static readonly string _singleMP4File =
    Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

This sample uses Azure Media Services .NET SDK Extensions.  

using Microsoft.WindowsAzure.MediaServices.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace MediaServicesPackaging
{
    class Program
    {
        private static readonly string _mediaFiles =
            Path.GetFullPath(@"../..\Media");

        private static readonly string _singleMP4File =
            Path.Combine(_mediaFiles, @"SingleMP4\BigBuckBunny.mp4");

        private static MediaServicesCredentials _cachedCredentials = null;
        private static CloudMediaContext _context = null;

        // Media Services account information.
        private static readonly string _mediaServicesAccountName =
            ConfigurationManager.AppSettings["MediaServicesAccountName"];
        private static readonly string _mediaServicesAccountKey =
            ConfigurationManager.AppSettings["MediaServicesAccountKey"];

        static void Main(string[] args)
        {
            // Create and cache the Media Services credentials in a static class variable.
            _cachedCredentials = new MediaServicesCredentials(
                            _mediaServicesAccountName,
                            _mediaServicesAccountKey);

            // Use the cached credentials to create CloudMediaContext.
            _context = new CloudMediaContext(_cachedCredentials);

            // Use the SDK extension method to create a new asset by 
            // uploading a mezzanine file from a local path.
            IAsset asset = _context.Assets.CreateFromFile(
                _singleMP4File,
                AssetCreationOptions.None,
                (af, p) =>
                {
                    Console.WriteLine("Uploading '{0}' - Progress: {1:0.##}%", af.Name, p.Progress);
                });

            // Encode an MP4 file to a set of multibitrate MP4s.
            IAsset assetMultibitrateMP4s =
                EncodeMP4ToMultibitrateMP4s(asset);

            // Publish the asset.
            _context.Locators.Create(
                LocatorType.OnDemandOrigin,
                assetMultibitrateMP4s,
                AccessPermissions.Read,
                TimeSpan.FromDays(30));

            // Get the URLs.
            Console.WriteLine("Smooth Streaming URL:");
            Console.WriteLine(assetMultibitrateMP4s.GetSmoothStreamingUri().ToString());
            Console.WriteLine("MPEG DASH URL:");
            Console.WriteLine(assetMultibitrateMP4s.GetMpegDashUri().ToString());
            Console.WriteLine("HLS URL:");
            Console.WriteLine(assetMultibitrateMP4s.GetHlsUri().ToString());
        }

        public static IAsset EncodeMP4ToMultibitrateMP4s(IAsset asset)
        {
            // Create a new job.
            IJob job = _context.Jobs.Create("Convert MP4 to Smooth Streaming.");

            // In Media Services, a media processor is a component that handles a specific processing task, 
            // such as encoding, format conversion, encrypting, or decrypting media content.
            //
            // Use the SDK extension method to  get a reference to the Azure Media Encoder.
            IMediaProcessor encoder = _context.MediaProcessors.GetLatestMediaProcessorByName(
                MediaProcessorNames.WindowsAzureMediaEncoder);

            // Add task 1 - Encode single MP4 into multibitrate MP4s.
            ITask adpativeBitrateTask = job.Tasks.AddNew("MP4 to Adaptive Bitrate Task",
                encoder,
                "H264 Adaptive Bitrate MP4 Set 720p",
                TaskOptions.None);

            // Specify the input Asset
            adpativeBitrateTask.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 in the clear (unencrypted).
            adpativeBitrateTask.OutputAssets.AddNew("Multibitrate MP4s",
                                    AssetCreationOptions.None);

            // Submit the job and wait until it is completed.
            job.Submit();
            job = job.StartExecutionProgressTask(
                j =>
                {
                    Console.WriteLine("Job state: {0}", j.State);
                    Console.WriteLine("Job progress: {0:0.##}%", j.GetOverallProgress());
                },
                CancellationToken.None).Result;

            // Get the output asset that contains the smooth stream.
            return job.OutputMediaAssets[0];
        }
    }
}

Build and run your program.


Build Date:

2014-09-09

Fællesskabsindhold

Tilføj
Vis:
© 2014 Microsoft