Quantcast
Channel: Southworks Blog » Mariano Converti
Viewing all articles
Browse latest Browse all 19

New Extensions available in WindowsAzure.MediaServices.Extensions NuGet package

$
0
0

Yesterday I published a new version of the WindowsAzure.MediaServices.Extensions NuGet package (1.0.4). This version includes new extension methods proposed by Mingfei Yan (Program Manager in Microsoft Windows Azure Media Team) in this GitHub issue, and a different organization of the code into separate classes.

The following are the changes and new features available in this version (you can find the full documentation in the README document):

Split extension methods and helpers in separate classes

The MediaServicesExtensions\MediaServicesExtensions.cs file was split into the following files to have a better organization of the code:

  • MediaServicesExtensions\AssetExtensions.cs: Contains useful extension methods and helpers related to the IAsset interface.
  • MediaServicesExtensions\JobExtensions.cs: Contains useful extension methods and helpers related to the IJob interface.
  • MediaServicesExtensions\LocatorExtensions.cs: Contains useful extension methods and helpers related to the ILocator interface.
  • MediaServicesExtensions\UrlExtensionsFixture.cs: Contains extension methods and helpers related to the Uri and String classes.

Get Job overall progress

Get the overall progress of a job by calculating the average progress of all its tasks using a single extension method for the IJob interface.

   1: CloudMediaContext context = new CloudMediaContext("%accountName%", "%accountKey%");

   2:  

   3: // The input asset for the task. Get a reference to it from the context.

   4: IAsset inputAsset = null;

   5:  

   6: // Prepare a job ready to be submitted with a single task with one 

   7: // input/output asset using a single extension method.

   8: IJob job = context.PrepareJobWithSingleTask(

   9:     "Windows Azure Media Encoder",

  10:     "H264 Adaptive Bitrate MP4 Set 720p",

  11:     inputAsset,

  12:     "OutputAssetName", 

  13:     AssetCreationOptions.None);

  14:  

  15: // Submit the job.

  16: job.Submit();

  17:  

  18: // ...

  19:  

  20: // Refresh the job instance.

  21: job = context.Jobs.Where(j => j.Id == job.Id).First();

  22:  

  23: // Get the overall progress of the job by calculating the average progress

  24: // of all its tasks using a single extension method. 

  25: double jobOverallProgress = job.GetOverallProgress();

Start Job execution progress task to notify when its state or overall progress change

Start a Task to monitor a job progress using a single extension method for the IJob interface. The difference with the IJob.GetExecutionProgressTask method is that this extension invokes a callback when the job state or its overall progress change. There is an additional overload with different parameters.

   1: CloudMediaContext context = new CloudMediaContext("%accountName%", "%accountKey%");

   2:  

   3: // The input asset for the task. Get a reference to it from the context.

   4: IAsset inputAsset = null;

   5:  

   6: // Prepare a job ready to be submitted with a single task with one 

   7: // input/output asset using a single extension method.

   8: IJob job = context.PrepareJobWithSingleTask(

   9:     "Windows Azure Media Encoder",

  10:     "H264 Adaptive Bitrate MP4 Set 720p",

  11:     inputAsset,

  12:     "OutputAssetName",

  13:     AssetCreationOptions.None);

  14:  

  15: // Submit the job.

  16: job.Submit();

  17:  

  18: // Start a task to monitor the job progress by invoking a callback when

  19: // its state or overall progress change in a single extension method.

  20: job = await context.StartExecutionProgressTask(

  21:     job,

  22:     j =>

  23:     {

  24:         Console.WriteLine("Current job state: {0}", j.State);

  25:         Console.WriteLine("Current job progress: {0}", j.GetOverallProgress());

  26:     },

  27:     CancellationToken.None);

Get SAS URL for Asset File

Get the SAS URL of an asset file for progressive download using a single extension method for the IAssetFile interface. This methods requires the parent asset to contain a SAS locator for the asset; otherwise it returns null.

   1: // The asset with multi-bitrate MP4 content. Get a reference to it from the context.

   2: IAsset asset = null;

   3:  

   4: // Make sure to create a SAS locator for the asset.

   5:  

   6: // Get the SAS URL for progressive download of an MP4 asset file.

   7: IAssetFile assetFile = asset

   8:     .AssetFiles

   9:     .ToList()

  10:     .Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase))

  11:     .First();

  12: Uri sasUri = assetFile.GetSasUri();

Save Uri to file

Save an Uri to a local file using a extension method for the Uri class. It creates the file if does not exist; otherwise, appends a new line to the end.

   1: // The asset with multi-bitrate MP4 content. Get a reference to it from the context.

   2: IAsset asset = null;

   3:  

   4: // Make sure to create an Origin locator for the asset.

   5: // Make sure to create a SAS locator for the asset.

   6:  

   7: IEnumerable<IAssetFile> mp4AssetFiles = asset

   8:         .AssetFiles

   9:         .ToList()

  10:         .Where(af => af.Name.EndsWith(".mp4", StringComparison.OrdinalIgnoreCase));

  11:  

  12: // Get the adaptive streaming URL's for the asset.

  13: Uri smoothStreamingUri = asset.GetSmoothStreamingUri();

  14: Uri hlsUri = asset.GetHlsUri();

  15: Uri mpegDashUri = asset.GetMpegDashUri();

  16: List<Uri> mp4ProgressiveDownloadUris = mp4AssetFiles.Select(af => af.GetSasUri()).ToList();

  17:  

  18: string filePath = @"C:\asset-urls.txt";

  19:  

  20: // Save the URL's to a file.

  21: smoothStreamingUri.Save(filePath);

  22: hlsUri.Save(filePath);

  23: mpegDashUri.Save(filePath);

  24: mp4ProgressiveDownloadUris.ForEach(uri => uri.Save(filePath));

 

If you have feedback, improvements or new extensions/helpers to propose, please feel free to open a new issue in the azure-sdk-for-media-services-extensions GitHub repository.

Enjoy!


Viewing all articles
Browse latest Browse all 19

Trending Articles