C# - 使用 Azure 媒体服务异步编码视频并处理任务完成

标签 c# asynchronous azure-media-services

我有一个 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/

相关文章:

c# - 迁移到 .Net Core 3.1 时,.Net Core 2.2 中使用的 Services.AddMvc() 和 SuperJsonOutputFormatter 的替代方案是什么

c# - 无法加载文件或程序集 'CefSharp.dll' 或其依赖项之一

Azure 媒体服务编码的 mp4 文件大小是原始大小的 10 倍

azure - 如何使用 Azure 媒体服务设置 DRM 保护以防止屏幕捕获

c# - 在 tableadapter 上执行自定义查询

与 Net Framework 2.0 兼容的 C# JSON Rest 客户端

javascript - 如何在 JavaScript 中阻塞异步函数

python - 在 RQ 中重试失败的作业

javascript - 如何使用 async/wait 创建同步循环

ios - Azure 媒体服务 - iOS 上的字幕 - native 而非 AMP