我有一个 ASP.NET MVC Web 应用程序,我想使用 Azure 媒体服务对视频进行编码。
由于这个过程持续时间太长,我需要异步运行,这样我就不会让用户等待这个 Action 被处理。此外,我需要以某种方式处理此任务的执行,一旦它结束。
Azure 媒体服务文档 提供了用于实现此目的的代码框架:
static public IAsset EncodeToAdaptiveBitrateMP4Set(IAsset asset)
{
// Declare a new job.
IJob job = _context.Jobs.Create("Media Encoder Standard Job");
// Get a media processor reference, and pass to it the name of the
// processor to use for the specific task.
IMediaProcessor processor = GetLatestMediaProcessorByName("Media Encoder Standard");
// Create a task with the encoding details, using a string preset.
// In this case "Adaptive Streaming" preset is used.
ITask task = job.Tasks.AddNew("My encoding task",
processor,
"Adaptive Streaming",
TaskOptions.None);
// Specify the input asset to be encoded.
task.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 not encrypted.
task.OutputAssets.AddNew("Output asset",
AssetCreationOptions.None);
job.StateChanged += new EventHandler<JobStateChangedEventArgs>(JobStateChanged);
job.Submit();
job.GetExecutionProgressTask(CancellationToken.None).Wait();
return job.OutputMediaAssets[0];
}
private static void JobStateChanged(object sender, JobStateChangedEventArgs e)
{
// do something when job state changes
}
此代码负责在 Azure 媒体服务中对视频进行编码,但处理是同步完成的,因此在该操作结束之前用户将被阻止。
代替这条让程序等待任务结束的指令:
job.GetExecutionProgressTask(CancellationToken.None).Wait();
,我需要一些东西让作业在 Azure 媒体服务中异步运行,并能够处理任务的结束(编码结束时运行特定代码)。
你能帮我解决这个问题吗?如果您需要更多信息,请大声疾呼!
最佳答案
我们建议您注册通知 - 请参阅 this获取更多信息。
关于C# - 使用 Azure 媒体服务异步编码视频并处理任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48891819/