从资产创建缩略图 - Azure 媒体服务

Posted

技术标签:

【中文标题】从资产创建缩略图 - Azure 媒体服务【英文标题】:Create thumbnail from asset - Azure Media Services 【发布时间】:2017-08-04 17:29:30 【问题描述】:

最近我们在创建新闻媒体服务时遇到了一些问题。我们已经看到“Azure 媒体编码器”已被贬值,现在我们使用“媒体编码器标准”。因此,我们可以再次上传视频,但不能创建缩略图。你能帮助我们吗?我们如何从资产中获取缩略图?

这是显示在 Azure 管理页面中的错误:

An error has occurred. Stage: ParsePreset. Code: System.InvalidOperationException.`

Microsoft.Cloud.Media.Encoding.PresetException: The XML is invalid or malformed. --->
System.InvalidOperationException: There is an error in XML document (1, 2). --->
System.InvalidOperationException: <Thumbnail xmlns=''> was not expected.">An error has occurred. Stage: ParsePreset. Code: System.InvalidOperationException.

Microsoft.Cloud.Media.Encoding.PresetException: The XML is invalid or malformed. --->
System.InvalidOperationException: There is an error in XML document (1, 2). --->
System.InvalidOperationException: &lt;Thumbnail xmlns=''&gt; was not expected.

我们的代码:

    public void CreateThumbnails(string idAssetVideo, int iSegundo, string sPath)
    
        if (string.IsNullOrEmpty(idAssetVideo)) throw new Excepcion("El idAssetVideo no puede ser nulo. Póngase en contacto con soporte.");

        string sTime = Utilidades.GetDuracionLabelFromSegundos(iSegundo);

        IAsset asset = _context.Assets.Where(a => a.Id == idAssetVideo).SingleOrDefault();
        if (asset == null) throw new Excepcion("No existe ningún Asset con el Id indicado. Póngase en contacto con soporte.");

        // Declare a new job.
        IJob job = _context.Jobs.Create("My Thumbnail job");

        // Get a media processor reference, and pass to it the name of the 
        // processor to use for the specific task.
        IMediaProcessor processor = UtilsAzure.GetLatestMediaProcessorByName("Media Encoder Standard", _context);

        //Configuración para generar el Thumbnail
        string configThumbnails = @"<Thumbnail Size='50%,*' Type='Jpeg' Filename='OriginalFilename_Size_ThumbnailTime_ThumbnailIndex_Date_Time.DefaultExtension'>
                                            <Time Value='0:0:0' Step='" + sTime + "'/></Thumbnail>";
        //string configThumbnails = "<?xml version='1.0' encoding='UTF-8'?><Thumbnail Size='200,*' Type='Jpeg' JpegQuality='90' Filename='" + sTime + ".jpeg'><Time Value='" + sTime + "'/></Thumbnail>";

        ITask task = job.Tasks.AddNew("My thumbnail task",
            processor,
            configThumbnails,
            TaskOptions.ProtectedConfiguration);

        // Specify the input asset to be encoded.
        task.InputAssets.Add(asset);
        // Add an output asset to contain the results of the job.
        task.OutputAssets.AddNew("Output asset",
            AssetCreationOptions.None);

        // Use the following event handler to check job progress.  
        job.StateChanged += new
                EventHandler<JobStateChangedEventArgs>(UtilsAzure.StateChanged);

        // Launch the job.
        job.Submit();

        // Check job execution and wait for job to finish. 
        Task progressJobTask = job.GetExecutionProgressTask(CancellationToken.None);
        progressJobTask.Wait();

        // If job state is Error, the event handling 
        // method for job progress should log errors.  Here we check 
        // for error state and exit if needed.
        if (job.State == JobState.Error)
        
            throw new Exception("Exiting method due to job error.");
        

        List<IAssetFile> lAF = job.OutputMediaAssets[0].AssetFiles.ToList();

        //Valido
        if (lAF == null || !lAF.Any()) throw new Excepcion("El trabajo azure no devolvió resultados");
        if (lAF.Where(x => x.Name.EndsWith(".jpg")).FirstOrDefault() == null) throw new Excepcion("El trabajo azure no contiene ningún archivo con extensión JPG");
        if (!Directory.GetParent(sPath).Exists) throw new Excepcion("No existe la carpeta donde guardar el thumbnail de manera provisional");

        lAF.Where(x => x.Name.EndsWith(".jpg")).FirstOrDefault().Download(sPath);
    

【问题讨论】:

【参考方案1】:

在我看来,您正在为缩略图使用旧的已弃用预设架构。 请查看本文中媒体编码器标准功能的新预设: https://docs.microsoft.com/en-us/azure/media-services/media-services-advanced-encoding-with-mes#a-idthumbnailsagenerate-thumbnails

将您的 configThumbnails 替换为较新的 JSON 版本的 Thumbnail 预设。

希望对您有所帮助!

-京东

【讨论】:

谢谢!其作品!真的,我们只需要创建一个图像。因此,我们简化了 XML:【参考方案2】:

谢谢!其作品!真的,我们只需要创建一个图像。因此,我们简化了 XML:

<?xml version='1.0' encoding='utf-16'?>
<Preset xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' Version='1.0' xmlns='http://www.windowsazure.com/media/encoding/Preset/2014/03'>
  <Encoding>
    <JpgImage Start='00:01:16' Step='00:00:01' Range='00:00:01'>
      <JpgLayers>
        <JpgLayer>
          <Width>640</Width>
          <Height>360</Height>
          <Quality>90</Quality>
        </JpgLayer>
      </JpgLayers>
    </JpgImage>
  </Encoding>
  <Outputs>
    <Output FileName='Basename_IndexExtension'>
      <JpgFormat />
    </Output>
  </Outputs>
</Preset>

【讨论】:

以上是关于从资产创建缩略图 - Azure 媒体服务的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 媒体缩略图/固定链接

如何从 JavaScript 应用程序中的视频创建缩略图 [关闭]

使用 PHP 从 URL 创建缩略图

从视频生成的 AWS mediaconvert 缩略图被旋转?

改进了从视频中提取缩略图

如何从 PHAsset 获取视频的缩略图?