gpt4 book ai didi

java - 将 Azure 媒体服务缩略图任务预设与 Java SDK 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:12 25 4
gpt4 key购买 nike

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 执行此任务的工作示例吗?

最佳答案

简短回答:

此答案适用于 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);

}

关于java - 将 Azure 媒体服务缩略图任务预设与 Java SDK 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com