将Azure Media Services缩略图任务预设与Java SDK一起使用

时间:2015-02-26 00:55:12

标签: java azure azure-media-services

Windows Azure文档提供了一个任务预设,用于在C#中创建缩略图和示例代码:https://msdn.microsoft.com/en-us/library/azure/hh973624.aspx

我正在尝试使用java SDK http://azure.microsoft.com/en-us/develop/java/执行此操作,并从服务器获取错误400.

我的代码的相关摘录是:

JobInfo jobInfo = mediaService
            .create(Job
                    .create()
                    .setName(
                            "Encoding " + assetToEncode.getName() + " to "
                                    + encodingPreset
                                    + " and Packaging to HLS")
                    .addInputMediaAsset(assetToEncode.getId())
                    // Thumbnails see
                    // https://msdn.microsoft.com/en-us/library/azure/hh973624.aspx
                     .addTaskCreator(
                     Task.create(
                     mediaProcessor.getId(),
                     "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                     +
                     "<Thumbnail Size=\"100%,*\" Type=\"Jpeg\" Filename=\"{OriginalFilename}_{Size}_{ThumbnailTime}_{ThumbnailIndex}_{Date}_{Time}.{DefaultExtension}\">"
                     + "<Time Value=\"10%\" Step=\"10%\" Stop=\"95%\"/>" +
                     "</Thumbnail>").setConfiguration( "Thumbnails")
                     .setOptions(
                     TaskOption.ProtectedConfiguration).setName(
                     "Thumbnails"))

有没有人在java中使用此任务有一个工作示例?

1 个答案:

答案 0 :(得分:0)

简答:

这个答案适用于azure-media maven软件包版本0.6.0到0.8.x

您应该使用create方法上的常用XML,并通过setConfiguration设置缩略图XML,如下例所示:

// Create the Thumbnail task
String xmlThumbnail = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
       "<Thumbnail Size=\"" + size + "\" Type=\"Jpeg\" " + 
       "Filename=\"{OriginalFilename}_{Size}_{ThumbnailTime}_{ThumbnailIndex}_{Date}_{Time}.{DefaultExtension}\">" + 
       "<Time Value=\"" + time + "\"/></Thumbnail>";

Task.CreateBatchOperation task = Task
            .create(mediaProcessor.getId(), 
                    "<taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody>")
            .setConfiguration(xmlThumbnail)
            .setName("MakeThumbnailTask");

完整示例:

<强>用法

MakeThumbnail(mediaService, "50%,*", "10%"); 

<强>代码

private void MakeThumbnail(MediaContract mediaService, String size, String time) throws ServiceException, InterruptedException, MalformedURLException {

        // Use the Azure Media Encoder, by specifying it by name.
        ListResult<MediaProcessorInfo> mediaProcessors = mediaService
                .list(MediaProcessor.list().set("$filter", "Name eq 'Azure Media Encoder'"));

        // Use the latest version of the media processor.
        MediaProcessorInfo mediaProcessor = null;
        for (MediaProcessorInfo info : mediaProcessors) {
            if (null == mediaProcessor || info.getVersion().compareTo(mediaProcessor.getVersion()) > 0) {
                mediaProcessor = info;
            }
        }

        // Create the Thumbnail task
        String xmlThumbnail = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
           "<Thumbnail Size=\"" + size + "\" Type=\"Jpeg\" " + 
           "Filename=\"{OriginalFilename}_{Size}_{ThumbnailTime}_{ThumbnailIndex}_{Date}_{Time}.{DefaultExtension}\">" + 
           "<Time Value=\"" + time + "\"/></Thumbnail>";

        Task.CreateBatchOperation task = Task
                .create(mediaProcessor.getId(), 
                        "<taskBody><inputAsset>JobInputAsset(0)</inputAsset><outputAsset>JobOutputAsset(0)</outputAsset></taskBody>")
                .setConfiguration(xmlThumbnail)
                .setName("MakeThumbnailTask");

        // Create a job creator that specifies the asset, priority and task for the job.
        Job.Creator jobCreator = Job.create().setName("MakeThumbnailTask").addInputMediaAsset("nb:cid:UUID:ec25435d-1500-80c3-dd89-f1e528b02207").setPriority(2)
                .addTaskCreator(task);

        JobInfo jobInfo = mediaService.create(jobCreator);
        String jobId = jobInfo.getId();

        // Check to see if the job has completed.
        CheckJobStatus(jobId);
        // Done with the job.

        // Retrieve the output asset.
        ListResult<AssetInfo> outputAssets = mediaService.list(Asset.list(jobInfo.getOutputAssetsLink()));
        AssetInfo thumbnailAsset = outputAssets.get(0);

        AccessPolicyInfo downloadAccessPolicy = null;
        double t100years = 100.0 * 365.0 * 24.0 * 60.0;
        downloadAccessPolicy = mediaService
                .create(AccessPolicy.create("Public Thumbnail Policy", t100years, EnumSet.of(AccessPolicyPermission.READ)));

        LocatorInfo downloadLocator = null;
        downloadLocator = mediaService
                .create(Locator.create(downloadAccessPolicy.getId(), thumbnailAsset.getId(), LocatorType.SAS));

        // Lookup the output asset file
        ListResult<AssetFileInfo> assetFiles = mediaService.list(AssetFile.list(thumbnailAsset.getAssetFilesLink()));
        AssetFileInfo jpegFile = null;
        for (AssetFileInfo outputAssetFile : assetFiles) {
            // check if primary output.
            if (outputAssetFile.getIsPrimary())  {
                jpegFile = outputAssetFile;
            }
        }

        // Adding the file name to the URL
        String imageUrl = downloadLocator.getPath().replaceAll("\\?", "/" + jpegFile.getName() + "?");

        System.out.println("Created Thumbnail JPEG URL: " + imageUrl);

 }