【问题标题】:Recreating thumbnail from existing asset in Azure Media Services从 Azure 媒体服务中的现有资产重新创建缩略图
【发布时间】: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


    【解决方案1】:

    我没有将原始源视频文件传递给它 这可能就是您遇到问题的原因。如您所见,自适应流作业的输出包含多个文件。需要一些额外的标志来告诉缩略图生成作业只关注一个文件(通常是最高比特率的文件)。下面的预设应该可以解决问题。

    1. 请注意预设如何以 Streams 部分开始,该部分告诉编码器为视频和音频选择最高/最高比特率
    2. 注意,Step 设置为 2,但 range 为 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"
          }
        }
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 2018-01-30
      • 2013-10-24
      • 2019-04-10
      • 1970-01-01
      相关资源
      最近更新 更多