全部产品
云市场

媒体处理

更新时间:2019-08-29 10:07:15

初始化客户端

使用前请先初始化客户端,参考 initVodClient 接口。

提交转码作业

接口参数和返回字段请参考 SubmitTranscodeJobs

  1. import com.aliyuncs.vod.model.v20170321.SubmitTranscodeJobsRequest;
  2. import com.aliyuncs.vod.model.v20170321.SubmitTranscodeJobsResponse;
  3. /**
  4. * 提交媒体处理作业
  5. */
  6. public static SubmitTranscodeJobsResponse submitTranscodeJobs(DefaultAcsClient client) throws Exception {
  7. SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
  8. //需要转码的视频ID
  9. request.setVideoId("34a6ca54f5c140eece85a289096d");
  10. //转码模板ID
  11. request.setTemplateGroupId("e8aa925a9798c630d30cd737d4");
  12. //构建需要替换的水印参数(只有需要替换水印相关信息才需要构建)
  13. JSONObject overrideParams = buildOverrideParams();
  14. //覆盖参数,暂只支持水印部分参数替换(只有需要替换水印相关信息才需要传递)
  15. request.setOverrideParams(overrideParams.toJSONString());
  16. //构建标准加密配置参数(只有标准加密才需要构建)
  17. JSONObject encryptConfig = buildEncryptConfig(client);
  18. //HLS标准加密配置(只有标准加密才需要传递)
  19. request.setEncryptConfig(encryptConfig.toJSONString());
  20. return client.getAcsResponse(request);
  21. }
  22. /**
  23. * 以下为调用示例
  24. */
  25. public static void main(String[] args) throws ClientException {
  26. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  27. SubmitTranscodeJobsResponse response = new SubmitTranscodeJobsResponse();
  28. try {
  29. response = submitTranscodeJobs(client);
  30. //任务ID
  31. System.out.println("JobId = " + response.getTranscodeJobs().get(0).getJobId());
  32. } catch (Exception e) {
  33. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  34. }
  35. System.out.println("RequestId = " + response.getRequestId());
  36. }
  37. /**
  38. * 构建HLS标准加密的配置信息
  39. * @return
  40. * @throws ClientException
  41. */
  42. public static JSONObject buildEncryptConfig(DefaultAcsClient client) throws ClientException {
  43. //点播给用户在KMS(秘钥管理服务)中的Service Key,可在用户秘钥管理服务对应的区域看到描述为vod的service key
  44. String serviceKey = "<您的Service Key>";
  45. //随机生成一个加密的秘钥,返回的response包含明文秘钥以及密文秘钥,
  46. //视频标准加密只需要传递密文秘钥即可
  47. GenerateDataKeyResponse response = generateDataKey(client, serviceKey);
  48. JSONObject encryptConfig = new JSONObject();
  49. //解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样
  50. //至于Ciphertext这个解密接口参数的名称,用户可自行制定,这里只作为参考参数名称
  51. encryptConfig.put("DecryptKeyUri", "http://decrypt.demo.com/decrypt?" +
  52. "Ciphertext=" + response.getCiphertextBlob());
  53. //秘钥服务的类型,目前只支持KMS
  54. encryptConfig.put("KeyServiceType", "KMS");
  55. //密文秘钥
  56. encryptConfig.put("CipherText", response.getCiphertextBlob());
  57. return encryptConfig;
  58. }
  59. /**
  60. * 1、构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;
  61. * 2、需要替换的水印信息对应水印ID必须是关联在指定的模板ID(即TranscodeTemplateId)中;
  62. * 3、不支持通过媒体处理接口去增加一个没有关联上的水印
  63. * 注意:图片水印的文件存储源站需要和发起转码的视频存储源站一致
  64. * @return
  65. */
  66. public static JSONObject buildOverrideParams() {
  67. JSONObject overrideParams = new JSONObject();
  68. JSONArray watermarks = new JSONArray();
  69. //图片水印文件地址替换
  70. JSONObject watermark1 = new JSONObject();
  71. //模板上面关联需要替换的水印文件图片水印ID
  72. watermark1.put("WatermarkId", "2ea587477c5a1bc8b5742d7");
  73. //需要替换成对应图片水印文件的OSS地址,水印文件存储源站需要和视频存储源站一致
  74. watermark1.put("FileUrl", "https://outin-40564284ef05113e1403e7.oss-cn-shanghai.aliyuncs.com/watermarks/02A1B22DF25D46C3C725A4-6-2.png");
  75. watermarks.add(watermark1);
  76. //文字水印内容替换
  77. JSONObject watermark2 = new JSONObject();
  78. //模板上面关联需要替换内容的文字水印ID
  79. watermark2.put("WatermarkId", "d297ba31ac5242d2071bf7");
  80. //需要替换成对应的内容
  81. watermark2.put("Content", "用户ID:66666");
  82. watermarks.add(watermark2);
  83. overrideParams.put("Watermarks", watermarks);
  84. return overrideParams;
  85. }
  86. /**
  87. * 生成加密需要的秘钥,response中包含密文秘钥和明文秘钥,用户只需要将密文秘钥传递给点播即可
  88. * 注意:KeySpec 必须传递AES_128,且不能设置NumberOfBytes
  89. * @param client KMS-SDK客户端
  90. * @param serviceKey 点播提供生成秘钥的service key,在用户的秘钥管理服务中可看到描述为vod的加密key
  91. * @return
  92. * @throws ClientException
  93. */
  94. public static GenerateDataKeyResponse generateDataKey(DefaultAcsClient client, String serviceKey) throws ClientException {
  95. GenerateDataKeyRequest request = new GenerateDataKeyRequest();
  96. request.setKeyId(serviceKey);
  97. request.setKeySpec("AES_128");
  98. return client.getAcsResponse(request);
  99. }

提交截图作业

接口参数和返回字段请参考 SubmitSnapshotJob

创建截图模板详细请参考 截图模板

  1. import com.aliyuncs.vod.model.v20170321.SubmitSnapshotJobRequest;
  2. import com.aliyuncs.vod.model.v20170321.SubmitSnapshotJobResponse;
  3. /**
  4. * 提交媒体截图处理作业调用函数
  5. */
  6. public static SubmitSnapshotJobResponse submitSnapshotJob(DefaultAcsClient client) throws Exception {
  7. SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
  8. //需要截图的视频ID(推荐传递截图模板ID)
  9. request.setVideoId("4d237a8270084849bf42078761814069");
  10. //截图模板ID
  11. request.setSnapshotTemplateId("5d745e6b8baadf589e0702426cfc62c81");
  12. //如果设置了SnapshotTemplateId,会忽略下面参数
  13. request.setCount(50L);
  14. request.setSpecifiedOffsetTime(0L);
  15. request.setInterval(1L);
  16. request.setWidth("200");
  17. request.setHeight("200");
  18. JSONObject spriteSnapshotConfig = buildSnapshotTemplateConfig();
  19. request.setSpriteSnapshotConfig(spriteSnapshotConfig.toJSONString());
  20. return client.getAcsResponse(request);
  21. }
  22. /**
  23. * 构建雪碧图截图配置
  24. * @return
  25. */
  26. public static JSONObject buildSnapshotTemplateConfig() {
  27. JSONObject spriteSnapshotConfig = new JSONObject();
  28. spriteSnapshotConfig.put("CellWidth", "120");
  29. spriteSnapshotConfig.put("CellHeight", "68");
  30. spriteSnapshotConfig.put("Columns", "3");
  31. spriteSnapshotConfig.put("Lines", "10");
  32. spriteSnapshotConfig.put("Padding", "20");
  33. spriteSnapshotConfig.put("Margin", "50");
  34. //保留雪碧图原始图
  35. spriteSnapshotConfig.put("KeepCellPic", "keep");
  36. spriteSnapshotConfig.put("Color", "tomato");
  37. return spriteSnapshotConfig;
  38. }
  39. /**
  40. * 以下为调用示例
  41. */
  42. public static void main(String[] args) throws ClientException {
  43. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  44. SubmitSnapshotJobResponse response = new SubmitSnapshotJobResponse();
  45. try {
  46. response = submitSnapshotJob(client);
  47. //任务ID
  48. System.out.println("JobId = " + response.getSnapshotJob().getJobId());
  49. } catch (Exception e) {
  50. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  51. }
  52. System.out.println("RequestId = " + response.getRequestId());
  53. }

查询截图数据

接口参数和返回字段请参考 ListSnapshots

  1. import com.aliyuncs.vod.model.v20170321.ListSnapshotsRequest;
  2. import com.aliyuncs.vod.model.v20170321.ListSnapshotsResponse;
  3. /**
  4. * 查询截图数据
  5. */
  6. public static ListSnapshotsResponse listSnapshots(DefaultAcsClient client) throws Exception {
  7. ListSnapshotsRequest request = new ListSnapshotsRequest();
  8. //视频ID
  9. request.setVideoId("c86c0ceba97965352418");
  10. //截图类型
  11. request.setSnapshotType("CoverSnapshot");
  12. request.setPageNo("1");
  13. request.setPageSize("20");
  14. return client.getAcsResponse(request);
  15. }
  16. /**
  17. * 以下为调用示例
  18. */
  19. public static void main(String[] args) throws ClientException {
  20. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  21. ListSnapshotsResponse response = new ListSnapshotsResponse();
  22. try {
  23. response = listSnapshots(client);
  24. //截图地址
  25. System.out.println("Url = " + response.getMediaSnapshot().getSnapshots().get(0).getUrl());
  26. } catch (Exception e) {
  27. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  28. }
  29. System.out.println("RequestId = " + response.getRequestId());
  30. }

导播台视频预处理

接口参数和返回字段请参考 SubmitPreprocessJobs

  1. import com.aliyuncs.vod.model.v20170321.SubmitPreprocessJobsRequest;
  2. import com.aliyuncs.vod.model.v20170321.SubmitPreprocessJobsResponse;
  3. /**
  4. * 导播台视频预处理
  5. */
  6. public static SubmitPreprocessJobsResponse submitPreprocessJobs(DefaultAcsClient client) throws Exception {
  7. SubmitPreprocessJobsRequest request = new SubmitPreprocessJobsRequest();
  8. //视频ID
  9. request.setVideoId("c86c0ceb8db54ae097965352418");
  10. request.setPreprocessType("PreprocessType");
  11. return client.getAcsResponse(request);
  12. }
  13. /**
  14. * 以下为调用示例
  15. */
  16. public static void main(String[] args) throws ClientException {
  17. DefaultAcsClient client = initVodClient("<您的AccessKeyId>", "<您的AccessKeySecret>");
  18. SubmitPreprocessJobsResponse response = new SubmitPreprocessJobsResponse();
  19. try {
  20. response = submitPreprocessJobs(client);
  21. //任务ID
  22. System.out.println("JobId = " + response.getPreprocessJobs().get(0).getJobId());
  23. } catch (Exception e) {
  24. System.out.println("ErrorMessage = " + e.getLocalizedMessage());
  25. }
  26. System.out.println("RequestId = " + response.getRequestId());
  27. }