Everywhere online I can find out some explanation related to Video files uploaded to Azure Media Services.
Based on the tutorials I wrote my own code.
After running the StoreAudio method I have:
- New Blob on Storage
- New Asset on Media Services
- New Job successfully completed on Media Services
- The created asset is Not Published
- When I try to get from the convertedAsset properties like ID or URI I get an exception
Why are ID and URI null? Why is the content "not published"?
Code:
public string StoreAudio(int ID, byte[] file)
{
try
{
var blobContainerName = AudioBookContainer; //+ AudioChapterID % 1000?
var fileName = ID + ".mp3";
var mediaBlobContainer = blobClient.GetContainerReference(blobContainerName);
mediaBlobContainer.CreateIfNotExists();
using (MemoryStream ms = new MemoryStream(file))
{
var reference = mediaBlobContainer.GetBlockBlobReference(fileName);
reference.UploadFromStream(ms);
}
IAsset asset = _context.Assets.Create(fileName, AssetCreationOptions.None);
IAccessPolicy writePolicy = _context.AccessPolicies.Create("writePolicy", TimeSpan.FromMinutes(120), AccessPermissions.Write);
ILocator destinationLocator = _context.Locators.CreateLocator(LocatorType.Sas, asset, writePolicy);
Uri uploadUri = new Uri(destinationLocator.Path);
string assetContainerName = uploadUri.Segments[1];
CloudBlobContainer assetContainer = blobClient.GetContainerReference(assetContainerName);
var sourceCloudBlob = mediaBlobContainer.GetBlockBlobReference(fileName);
sourceCloudBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length > 0)
{
IAssetFile assetFile = asset.AssetFiles.Create(fileName);
var destinationBlob = assetContainer.GetBlockBlobReference(fileName);
destinationBlob.DeleteIfExists();
destinationBlob.StartCopyFromBlob(sourceCloudBlob);
destinationBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length != destinationBlob.Properties.Length)
throw new Exception("Error copying");
}
destinationLocator.Delete();
writePolicy.Delete();
asset = _context.Assets.Where(a => a.Id == asset.Id).FirstOrDefault(); //At this point, you can create a job using your asset.
var encodedAsset = EncodeToWMA(asset);
return encodedAsset.Id;
//var ismAssetFiles = encodedAsset.AssetFiles.ToList().Where(f => f.Name.EndsWith(".ism", StringComparison.OrdinalIgnoreCase)).ToArray();
//if (ismAssetFiles.Count() != 1)
// throw new ArgumentException("The asset should have only one, .ism file");
//ismAssetFiles.First().IsPrimary = true;
//ismAssetFiles.First().Update();
asset.Delete();
return encodedAsset.Uri.AbsoluteUri;
}
catch(Exception exx)
{
return exx.Message + exx.InnerException;
}
}
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;
}
public static IAsset EncodeToWMA(IAsset asset)
{
IJob job = _context.Jobs.Create("Convert MP3 to WMA");
IMediaProcessor processor = GetLatestMediaProcessorByName("Windows Azure Media Encoder");
ITask task = job.Tasks.AddNew("My encoding task", processor, "WMA High Quality Audio", TaskOptions.None);
task.InputAssets.Add(asset);
task.OutputAssets.AddNew(asset.Name.Replace(".mp3", ".wma"), AssetCreationOptions.None);
job.Submit();
Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
progressJobTask.Wait();
return task.OutputAssets.First();
}