<?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <appSettings> <add key="accountName" value="Add-Media-Services-Account-Name" /> <add key="accountKey" value="Add-Media-Services-Account-Key" /> </appSettings> </configuration>
private static readonly string _singleInputFilePath = Path.GetFullPath(@"C:\mediaFiles\interview.wmv"); private static readonly string _outputFilesFolder = Path.GetFullPath(@"C:\outputfiles");
using System.Linq; using System; using System.Configuration; using System.IO; using System.Threading; using System.Collections.Generic; using Microsoft.WindowsAzure.MediaServices.Client;
private static readonly string _accountKey = ConfigurationManager.AppSettings["accountKey"]; private static readonly string _accountName = ConfigurationManager.AppSettings["accountName"];
private static CloudMediaContext _context = null; static string _outputAssetID = null;
_context = new CloudMediaContext(_accountName, _accountKey);
static IAsset CreateAndUploadAsset(string inputMediaFilePath) { IAsset theAsset = _context.Assets.Create(inputMediaFilePath, AssetCreationOptions.StorageEncrypted); Console.WriteLine("Asset name: " + theAsset.Name); Console.WriteLine("Asset ID: " + theAsset.Id); Console.WriteLine("Time created: " + theAsset.Created.Date.ToString()); Console.WriteLine("Encrypted status: " + theAsset.Options.ToString()); return theAsset; } IAsset asset = CreateAndUploadAsset(_singleInputFilePath);
// static void CreateEncodingJob(IAsset asset, string outputFolder) { IJob job = _context.Jobs.Create("My Encoding Job"); // - IMediaProcessor processor = GetMediaProcessor("Windows Azure Media Encoder"); // ITask task = job.Tasks.AddNew("My encoding task", processor, "H.264 256k DSL CBR", TaskCreationOptions.None); // , task.InputMediaAssets.Add(asset); // Asset task.OutputMediaAssets.AddNew("Output asset", true, AssetCreationOptions.None); // . job.Submit(); // CheckJobProgress(job.Id); // Job job = GetJob(job.Id); // IAsset outputAsset = job.OutputMediaAssets[0]; // _outputAssetID = outputAsset.Id; // SAS URL string sasUrl = GetAssetSasUrl(outputAsset, TimeSpan.FromMinutes(30)); // URL string outFilePath = Path.GetFullPath(outputFolder + @"\" + "SasUrl.txt"); WriteToFile(outFilePath, sasUrl); }
private static void CheckJobProgress(string jobId) { // bool jobCompleted = false; // const int JobProgressInterval = 20000; while (!jobCompleted) { // Job IJob theJob = GetJob(jobId); // switch (theJob.State) { case JobState.Finished: jobCompleted = true; Console.WriteLine(""); Console.WriteLine("********************"); Console.WriteLine("Job state: " + theJob.State + "."); Console.WriteLine("Please wait while local tasks complete..."); Console.WriteLine(); break; case JobState.Queued: case JobState.Scheduled: case JobState.Processing: Console.WriteLine("Job state: " + theJob.State + "."); Console.WriteLine("Please wait..."); Console.WriteLine(); break; case JobState.Error: break; default: Console.WriteLine(theJob.State.ToString()); break; } // , Thread.Sleep(JobProgressInterval); } }
private static IMediaProcessor GetMediaProcessor(string mediaProcessor) { // // MP4 to Smooth Streams Task // Windows Azure Media Encoder // PlayReady Protection Task // Smooth Streams to HLS Task // Storage Decryption // , var theProcessor = from p in _context.MediaProcessors where p.Name == mediaProcessor select p; // IMediaprocessor. IMediaProcessor processor = theProcessor.First(); if (processor == null) throw new ArgumentException(string.Format(System.Globalization.CultureInfo.CurrentCulture, "Unknown processor", mediaProcessor)); return processor; }
static IJob GetJob(string jobId) { // Job Id var job = from j in _context.Jobs where j.Id == jobId select j; // IJob theJob = job.FirstOrDefault(); // , Job :) if (theJob != null) return theJob; else Console.WriteLine("Job does not exist."); return null; }
static String GetAssetSasUrl(IAsset asset, TimeSpan accessPolicyTimeout) { // IAccessPolicy readPolicy = _context.AccessPolicies.Create("My Test Policy", accessPolicyTimeout, AccessPermissions.Read); // locator, asset ILocator locator = _context.Locators.CreateSasLocator(asset, readPolicy, DateTime.UtcNow.AddMinutes(-5)); Console.WriteLine("Locator path: "); Console.WriteLine(locator.Path); Console.WriteLine(); // mp4 - - xml var theOutputFile = from f in asset.Files where f.Name.EndsWith(".mp4") select f; // IQueryable IFileInfo. IFileInfo theFile = theOutputFile.FirstOrDefault(); string fileName = theFile.Name; // SAS URL var uriBuilder = new UriBuilder(locator.Path); uriBuilder.Path += "/" + fileName; Console.WriteLine("Full URL to file: "); Console.WriteLine(uriBuilder.Uri.AbsoluteUri); Console.WriteLine(); return uriBuilder.Uri.AbsoluteUri; }
static void WriteToFile(string outFilePath, string fileContent) { StreamWriter sr = File.CreateText(outFilePath); sr.Write(fileContent); sr.Close();
CreateEncodingJob(asset, _outputFilesFolder);
Asset name: interview Asset ID: nb:cid:UUID:xyzxyza-318a-4a47-b996-27353b23abc3 Time created: 5/24/2012 12:00:00 AM Encrypted status: StorageEncrypted Job state: Queued. Please wait... Job state: Processing. Please wait... Job state: Processing. Please wait... ******************** Job state: Finished. Please wait while local tasks complete... Locator path: https://MediaServicesServer.blob.core.windows.net/asset-zzzz374-1234-4c60-9da8-3daf 7a6dabcd?st=2012-05-24T21%3A59%3A55Z&se=2012-05-24T22%3A29%3A55Z&sr=c&si=b1a0cf8 f-45bf-4f77-a84a-a38c3f8a002d&sig=tWmPLPpNuQpEXvCd2Ik8rCfY5AqjII3gnWgi9ustBI4%3D Full URL to file: https:// MediaServicesServer.blob.core.windows.net/asset- zzzz374-1234-4c60-9da8-3daf 7a6dabcd/interview.mp4?st=2012-05-24T21%3A59%3A55Z&se=2012-05-24T22%3A29%3A55Z&s r=c&si=b1a0cf8f-45bf-4f77-a84a-a38c3f8a002d&sig=tWmPLPpNuQpEXvCd2Ik8rCfY5AqjII3g nWgi9ustBI4%3D
Source: https://habr.com/ru/post/150593/
All Articles