All Products
Search
Document Center

Media processing

Last Updated: Aug 01, 2019

Initialize a client

Initialize a client before using the SDK. For more information, see initVodClient.

Submit a transcoding job

For more information about the request and response parameters, see SubmitTranscodeJobs.

  1. import com.aliyuncs.vod.model.v20170321.SubmitTranscodeJobsRequest;
  2. import com.aliyuncs.vod.model.v20170321.SubmitTranscodeJobsResponse;
  3. /**
  4. * Method for submitting a media processing job
  5. */
  6. public static SubmitTranscodeJobsResponse submitTranscodeJobs(DefaultAcsClient client) throws Exception {
  7. SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
  8. // Specifies the ID of the video to be transcoded.
  9. request.setVideoId("34a6ca54f5c140eece85a289096d");
  10. // Sets the ID of the transcoding template group.
  11. request.setTemplateGroupId("e8aa925a9798c630d30cd737d4");
  12. // Constructs a watermark replacement parameter (applicable only when the watermark information needs to be replaced).
  13. JSONObject overrideParams = buildOverrideParams();
  14. // Sets the watermark replacement parameter (applicable only when the watermark information needs to be replaced).
  15. request.setOverrideParams(overrideParams.toJSONString());
  16. // Constructs a standard encryption configuration parameter (applicable only to standard encryption).
  17. JSONObject encryptConfig = buildEncryptConfig(client);
  18. // Specifies the standard HLS encryption configuration (applicable only to standard encryption).
  19. request.setEncryptConfig(encryptConfig.toJSONString());
  20. return client.getAcsResponse(request);
  21. }
  22. /**
  23. * Call example
  24. */
  25. public static void main(String[] args) throws ClientException {
  26. DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  27. SubmitTranscodeJobsResponse response = new SubmitTranscodeJobsResponse();
  28. try {
  29. response = submitTranscodeJobs(client);
  30. // The job 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. * Method for constructing the standard HLS encryption configuration
  39. * @return
  40. * @throws ClientException
  41. */
  42. public static JSONObject buildEncryptConfig(DefaultAcsClient client) throws ClientException {
  43. // Specifies the service key that ApsaraVideo for VOD grants to you in KMS. You can view the service key whose description is vod on the Keys page in the KMS console.
  44. String serviceKey = "<Your service key>";
  45. // Generates a random encryption key. The response contains a plaintext key and a ciphertext key.
  46. // Specifies only the ciphertext key for standard video encryption.
  47. GenerateDataKeyResponse response = generateDataKey(client, serviceKey);
  48. JSONObject encryptConfig = new JSONObject();
  49. // Specifies the URL of the decryption API. The ciphertext key generated each time and the API URL are concatenated to generate the value of this parameter to provide a unique ciphertext decryption key for each video.
  50. // You can customize the name of the decryption API parameter. Ciphertext is used as an example.
  51. encryptConfig.put("DecryptKeyUri", "http://decrypt.demo.com/decrypt?" +
  52. "Ciphertext=" + response.getCiphertextBlob());
  53. // Sets the key service type. Currently, only KMS is supported.
  54. encryptConfig.put("KeyServiceType", "KMS");
  55. // Specifies the ciphertext key.
  56. encryptConfig.put("CipherText", response.getCiphertextBlob());
  57. return encryptConfig;
  58. }
  59. /**
  60. * Currently, the constructed watermark replacement parameter can be used to replace only the file URL of an image watermark or the content of a text watermark.
  61. * The watermark ID of the source watermark information (to be replaced) must be associated with the transcoding template ID specified by the TranscodeTemplateId parameter.
  62. * You can call the SubmitTranscodeJobs operation to add only watermarks whose IDs are associated with template IDs.
  63. * Note: The origin that stores the image watermark file must be the same as the origin that stores the video to be transcoded.
  64. * @return
  65. */
  66. public static JSONObject buildOverrideParams() {
  67. JSONObject overrideParams = new JSONObject();
  68. JSONArray watermarks = new JSONArray();
  69. // Replaces the file URL of an image watermark.
  70. JSONObject watermark1 = new JSONObject();
  71. // Associates the ID of the source image watermark with a template ID.
  72. watermark1.put("WatermarkId", "2ea587477c5a1bc8b5742d7");
  73. // Specifies the target OSS URL of the image watermark file. The origin that stores the image watermark file must be the same as the origin that stores the video to be transcoded.
  74. watermark1.put("FileUrl", "https://outin-40564284ef05113e1403e7.oss-cn-shanghai.aliyuncs.com/watermarks/02A1B22DF25D46C3C725A4-6-2.png");
  75. watermarks.add(watermark1);
  76. // Replaces the content of a text watermark.
  77. JSONObject watermark2 = new JSONObject();
  78. // Associates the ID of the source text watermark with a template ID.
  79. watermark2.put("WatermarkId", "d297ba31ac5242d2071bf7");
  80. // Specifies the target content of the text watermark.
  81. watermark2.put("Content", "User ID: 66666");
  82. watermarks.add(watermark2);
  83. overrideParams.put("Watermarks", watermarks);
  84. return overrideParams;
  85. }
  86. /**
  87. * Generates an encryption key. The response contains a plaintext key and a ciphertext key. You need to specify only the ciphertext key for ApsaraVideo for VOD.
  88. * Note: Set the KeySpec parameter to AES_128 and do not set the NumberOfBytes parameter.
  89. * @param client The KMS SDK client.
  90. * @param serviceKey The service key that ApsaraVideo for VOD grants to you for generating a key. You can view the service key whose description is vod on the Keys page in the KMS console.
  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. }

Submit a video snapshot job

For more information about the request and response parameters, see SubmitSnapshotJob.

For more information about how to create a snapshot template, see AddVodTemplate.

  1. import com.aliyuncs.vod.model.v20170321.SubmitSnapshotJobRequest;
  2. import com.aliyuncs.vod.model.v20170321.SubmitSnapshotJobResponse;
  3. /**
  4. * Method for submitting a video snapshot job
  5. */
  6. public static SubmitSnapshotJobResponse submitSnapshotJob(DefaultAcsClient client) throws Exception {
  7. SubmitSnapshotJobRequest request = new SubmitSnapshotJobRequest();
  8. // Specifies the ID of the video to be snapshot. (We recommend that you use a snapshot template.)
  9. request.setVideoId("4d237a8270084849bf42078761814069");
  10. // Specifies the snapshot template ID.
  11. request.setSnapshotTemplateId("5d745e6b8baadf589e0702426cfc62c81");
  12. // If you set the SnapshotTemplateId parameter, ignore the following parameters:
  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. * Method for constructing the sprite snapshot configuration
  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. // Keeps the original sprite snapshots.
  35. spriteSnapshotConfig.put("KeepCellPic", "keep");
  36. spriteSnapshotConfig.put("Color", "tomato");
  37. return spriteSnapshotConfig;
  38. }
  39. /**
  40. * Call example
  41. */
  42. public static void main(String[] args) throws ClientException {
  43. DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  44. SubmitSnapshotJobResponse response = new SubmitSnapshotJobResponse();
  45. try {
  46. response = submitSnapshotJob(client);
  47. // The job 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. }

Query snapshot data

For more information about the request and response parameters, see ListSnapshots.

  1. import com.aliyuncs.vod.model.v20170321.ListSnapshotsRequest;
  2. import com.aliyuncs.vod.model.v20170321.ListSnapshotsResponse;
  3. /**
  4. * Method for querying snapshot data
  5. */
  6. public static ListSnapshotsResponse listSnapshots(DefaultAcsClient client) throws Exception {
  7. ListSnapshotsRequest request = new ListSnapshotsRequest();
  8. // Sets the video ID.
  9. request.setVideoId("c86c0ceba97965352418");
  10. // Sets the snapshot type.
  11. request.setSnapshotType("CoverSnapshot");
  12. request.setPageNo("1");
  13. request.setPageSize("20");
  14. return client.getAcsResponse(request);
  15. }
  16. /**
  17. * Call example
  18. */
  19. public static void main(String[] args) throws ClientException {
  20. DefaultAcsClient client = initVodClient("<Your AccessKey ID>", "<Your AccessKey Secret>");
  21. ListSnapshotsResponse response = new ListSnapshotsResponse();
  22. try {
  23. response = listSnapshots(client);
  24. // The snapshot URL.
  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. }