简体   繁体   English

从Azure Media Services中的现有资产重新创建缩略图

[英]Recreating thumbnail from existing asset in Azure Media Services

I am using Azure Media Services and Azure Functions to build a VOD element for a website. 我正在使用Azure媒体服务和Azure函数来为网站构建VOD元素。 Basically, when the source video is uploaded a blob trigger starts off a DurableOrchestration to create an asset and then encode the video. 基本上,当上传源视频时,blob触发器从DurableOrchestration开始创建资产,然后对视频进行编码。 It also generates 3 different size thumbnails using the default {Best} frame. 它还使用默认的{最佳}帧生成3个不同尺寸的缩略图。 So far so good. 到现在为止还挺好。

What I want to do now is allow the user to select a frame from the encoded video and choose that to be the poster thumbnail. 我现在想做的是允许用户从编码视频中选择一个帧,然后选择该帧作为海报缩略图。

I have an HttpTrigger which takes the asset id and the frame timestamp and kicks off another durable function which should recreate the thumbnails at the specified frame. 我有一个HttpTrigger,它带有资产ID和帧时间戳,并启动了另一个持久功能,该功能应在指定帧处重新创建缩略图。

But it isn't working. 但这不起作用。

I originally got 3 blank images in a new asset and when I tried to force it to put the images back into the original asset, I got nothing. 我最初在新资产中获得了3张空白图像,当我试图强制将其放回原始资产中时,我什么也没得到。

This is the code I'm using to try to achieve this. 这是我用来尝试实现此目的的代码。 It's pretty much the same as the code to create the original asset. 它与创建原始资产的代码几乎相同。 The only real difference is that the json preset only has instruction for generating thumbnails, the asset already has 6 encoded videos, 3 thumbnails and associated meta files in it, and I'm not passing the original source video file to it (because I delete that as part of the clean-up once the original encoding is complete). 唯一真正的区别是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);

My question is whether what I am trying to do is actually possible, and if so what is wrong with the approach I'm taking. 我的问题是,我实际上想做的事是否可行,如果可以,我采用的方法有什么问题。

I've searched quite a bit on this topic but can always only find reference to generating thumbnails whilst encoding a video, never generating thumbnails from encoded videos after the event. 我已经在该主题上进行了很多搜索,但始终只能在编码视频时找到生成缩略图的参考,而在事件发生后再也不会从编码的视频生成缩略图。

I'm not passing the original source video file to it That's likely why you are running into the problem. 我没有将原始源视频文件传递给它,这可能是您遇到问题的原因。 The output of your Adaptive Streaming Job, as you have seen, contains multiple files. 如您所见,自适应流作业的输出包含多个文件。 There are some additional flags needed to tell the thumbnail generation Job to focus on just one file (typically the highest bitrate file). 还需要一些其他标志来告知缩略图生成作业仅关注一个文件(通常是最高比特率的文件)。 The preset below should do the trick. 下面的预设应该可以解决问题。

  1. Note how the preset starts with a Streams section which tells the encoder to pick the highest/top bitrate for video and audio 请注意预设如何从“流”部分开始,该部分告诉编码器选择视频和音频的最高/最高比特率
  2. Note that the Step is set to 2, but range is 1, ensuring only one image is generated in the output 请注意,Step设置为2,但范围是1,确保在输出中仅生成一张图像


{
  "Version": 1.0,
  "Sources": [
    {
      "Streams": [
        {
          "Type": "AudioStream",
          "Value": "TopBitrate"
        },
        {
          "Type": "VideoStream",
          "Value": "TopBitrate"
        }
      ]
    }
  ],
  "Codecs": [
    {
      "Start": "00:00:03:00",
      "Step": "2",
      "Range": "1",
      "Type": "JpgImage",
      "JpgLayers": [
        {
          "Quality": 90,
          "Type": "JpgLayer",
          "Width": "100%",
          "Height": "100%"
        }
      ]
    }
  ],
  "Outputs": [
    {
      "FileName": "{Basename}_{Index}{Extension}",
      "Format": {
        "Type": "JpgFormat"
      }
    }
  ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM