【发布时间】:2019-02-24 16:20:06
【问题描述】:
我正在使用 Azure 媒体服务和 Azure Functions 为网站构建 VOD 元素。基本上,当上传源视频时,blob 触发器会启动 DurableOrchestration 以创建资产,然后对视频进行编码。它还使用默认的 {Best} 帧生成 3 个不同大小的缩略图。到目前为止一切顺利。
我现在要做的是让用户从编码的视频中选择一个帧,然后选择它作为海报缩略图。
我有一个 HttpTrigger,它获取资产 ID 和帧时间戳,并启动另一个持久函数,该函数应该在指定帧重新创建缩略图。
但它不起作用。
我最初在新资源中获得了 3 张空白图像,当我试图强制它将图像放回原始资源中时,我什么也没得到。
这是我用来尝试实现此目的的代码。它与创建原始资产的代码几乎相同。唯一真正的区别是 json 预设只有生成缩略图的指令,资产中已经有 6 个编码视频、3 个缩略图和关联的元文件,并且我没有将原始源视频文件传递给它(因为我删除作为原始编码完成后清理的一部分)。
PostData data = inputs.GetInput<PostData>();
IJob job = null;
ITask taskEncoding = null;
IAsset outputEncoding = null;
int OutputMES = -1;
int taskindex = 0;
bool useEncoderOutputForAnalytics = false;
MediaServicesCredentials amsCredentials = new MediaServicesCredentials();
try
{
AzureAdTokenCredentials tokenCredentials = new AzureAdTokenCredentials(amsCredentials.AmsAadTenantDomain,
new AzureAdClientSymmetricKey(amsCredentials.AmsClientId, amsCredentials.AmsClientSecret),
AzureEnvironments.AzureCloudEnvironment);
AzureAdTokenProvider tokenProvider = new AzureAdTokenProvider(tokenCredentials);
_context = new CloudMediaContext(amsCredentials.AmsRestApiEndpoint, tokenProvider);
IAsset asset = _context.Assets.Where(a => a.Id == data.assetId).FirstOrDefault();
// Declare a new encoding job with the Standard encoder
int priority = 10;
job = _context.Jobs.Create("CMS encoding job", priority);
foreach (var af in asset.AssetFiles)
{
if (af.Name.Contains(".mp4)"))
af.IsPrimary = true;
else
af.IsPrimary = false;
}
// Get a media processor reference, and pass to it the name of the
// processor to use for the specific task.
IMediaProcessor processorMES = MediaServicesHelper.GetLatestMediaProcessorByName(_context, "Media Encoder Standard");
string preset = null;
preset = "MesThumbnails.json"; // the default preset
string start = data.frame;
if (preset.ToUpper().EndsWith(".JSON"))
{
// Build the folder path to the preset
string presetPath = Path.Combine(System.IO.Directory.GetParent(data.execContext.FunctionDirectory).FullName, "presets", preset);
log.Info("presetPath= " + presetPath);
preset = File.ReadAllText(presetPath).Replace("{Best}", start);
}
taskEncoding = job.Tasks.AddNew("rebuild thumbnails task",
processorMES,
preset,
TaskOptions.None);
// Specify the input asset to be encoded.
taskEncoding.InputAssets.Add(asset);
OutputMES = taskindex++;
string _storageAccountName = amsCredentials.StorageAccountName;
outputEncoding = taskEncoding.OutputAssets.AddNew(asset.Name + " MES encoded", _storageAccountName, AssetCreationOptions.None);
asset = useEncoderOutputForAnalytics ? outputEncoding : asset;
job.Submit();
await job.GetExecutionProgressTask(CancellationToken.None);
我的问题是我正在尝试做的事情是否真的可行,如果可以,我正在采取的方法有什么问题。
我在这个主题上搜索了很多,但总是只能找到在编码视频时生成缩略图的参考,在事件发生后从不从编码视频生成缩略图。
【问题讨论】:
标签: c# azure-functions azure-media-services