edit-icon download-icon

Create HLS standard encryption workflow

Last Updated: Apr 10, 2018

Purpose

This document is an example of calling API to create HLS standard encryption workflow. For more information about creating workflow, see AddMediaWorkflow.

Code dependency example

  1. For more information about MPS SDK, see Installation.

  2. Other dependency

    1. <dependency>
    2. <groupId>com.alibaba</groupId>
    3. <artifactId>fastjson</artifactId>
    4. <version>1.2.25</version
    5. </dependency>

Example

  1. package com.aliyun.smallcode;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.aliyuncs.DefaultAcsClient;
  5. import com.aliyuncs.exceptions.ClientException;
  6. import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowRequest;
  7. import com.aliyuncs.mts.model.v20140618.AddMediaWorkflowResponse;
  8. import com.aliyuncs.profile.DefaultProfile;
  9. public class MediaHls {
  10. final String REGION_ID = "<region>";
  11. final String ACCESS_KEY_ID = "<accessKeyId>";
  12. final String ACCESS_KEY_SECRET = "<accessKeySecret>";
  13. final String PIPELINE_ID = "<PipelineId>";
  14. final String TEMPLATE_ID = "S00000001-100020"; //Transcoding template ID,m3u8 template, set as neeeded
  15. final String OSS_LOCATION = "<OssLocation>";
  16. final String INPUT_BUCKET = "<InputBucket>"; //input bucket
  17. final String INPUT_PATH = "<InputPath>"; //for example "HLS-Encryption"
  18. final String OUTPUT_BUCKET = "<OutputBucket>"; //output bucket
  19. final String ENCRYPTION_TYPE = "hls-aes-128";
  20. final String HLS_KEY_URI = "<Decryption key URI>"; //For example, http://decrypt.testdomain.com
  21. final String ACT_START = "Act-Start";
  22. final String ACT_ENCRYPTION = "Act-HLS-Encryption";
  23. final String ACT_REPORT = "Act-Report";
  24. private DefaultAcsClient client;
  25. public MediaHls() {
  26. DefaultProfile profile = DefaultProfile.getProfile(REGION_ID, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
  27. this.client = new DefaultAcsClient(profile);
  28. }
  29. public AddMediaWorkflowResponse addMediaWorkflow() throws ClientException {
  30. AddMediaWorkflowRequest request = new AddMediaWorkflowRequest();
  31. request.setTopology(createWorkflow().toJSONString());
  32. request.setName("HLS encryption workflow");
  33. return this.client.getAcsResponse(request);
  34. }
  35. private JSONObject createWorkflow() {
  36. JSONObject workflow = new JSONObject();
  37. JSONObject activities = new JSONObject();
  38. activities.put(ACT_START, createStartActivity());
  39. activities.put(ACT_ENCRYPTION, createTrancodeActivity());
  40. activities.put(ACT_REPORT, createReportActivity());
  41. workflow.put("Activities", activities);
  42. workflow.put("Dependencies", createDependencies());
  43. return workflow;
  44. }
  45. private JSONObject createStartActivity() {
  46. JSONObject startActivity = new JSONObject();
  47. startActivity.put("Name", ACT_START);
  48. startActivity.put("Type", "Start");
  49. startActivity.put("Parameters", buildStartParameters());
  50. return startActivity;
  51. }
  52. private JSONObject buildStartParameters() {
  53. JSONObject parameters = new JSONObject();
  54. parameters.put("PipelineId", PIPELINE_ID);
  55. parameters.put("InputFile", buildInputFile());
  56. return parameters;
  57. }
  58. private JSONObject buildInputFile() {
  59. JSONObject inputFile = new JSONObject();
  60. inputFile.put("Bucket", INPUT_BUCKET);
  61. inputFile.put("Location", OSS_LOCATION);
  62. inputFile.put("ObjectPrefix", INPUT_PATH);
  63. return inputFile;
  64. }
  65. private JSONObject createTrancodeActivity() {
  66. JSONObject transcodeActivity = new JSONObject();
  67. transcodeActivity.put("Name", ACT_ENCRYPTION);
  68. transcodeActivity.put("Type", "Transcode");
  69. transcodeActivity.put("Parameters", buildTranscodeParameters());
  70. return transcodeActivity;
  71. }
  72. private JSONObject buildTranscodeParameters() {
  73. JSONObject transcodeParamters = new JSONObject();
  74. transcodeParamters.put("OutputBucket", OUTPUT_BUCKET);
  75. transcodeParamters.put("OutputLocation", OSS_LOCATION);
  76. transcodeParamters.put("Outputs", buildOutputsConfig());
  77. return transcodeParamters;
  78. }
  79. private JSONArray buildOutputsConfig() {
  80. JSONArray outputs = new JSONArray();
  81. JSONObject output = new JSONObject();
  82. output.put("ObjectRegex", ACT_ENCRYPTION + "/{RunId}/{FileName}");
  83. output.put("TemplateId", TEMPLATE_ID);
  84. output.put("Encryption", buildEncryption());
  85. outputs.add(output);
  86. return outputs;
  87. }
  88. private JSONObject buildEncryption() {
  89. JSONObject encryption = new JSONObject();
  90. encryption.put("Type", ENCRYPTION_TYPE);
  91. encryption.put("KeyUri", HLS_KEY_URI);
  92. return encryption;
  93. }
  94. private JSONObject createReportActivity() {
  95. JSONObject reportActivity = new JSONObject();
  96. reportActivity.put("Name", ACT_REPORT);
  97. reportActivity.put("Parameters", new JSONObject());
  98. reportActivity.put("Type", "Report");
  99. return reportActivity;
  100. }
  101. private JSONObject createDependencies() {
  102. JSONObject dependencies = new JSONObject();
  103. JSONArray subActivityOfStart = new JSONArray();
  104. subActivityOfStart.add(ACT_ENCRYPTION);
  105. dependencies.put(ACT_START, subActivityOfStart);
  106. JSONArray subActivityOfTranscode = new JSONArray();
  107. subActivityOfTranscode.add(ACT_REPORT);
  108. dependencies.put(ACT_ENCRYPTION, subActivityOfTranscode);
  109. dependencies.put(ACT_REPORT, new JSONArray());
  110. return dependencies;
  111. }
  112. public static void main(String[] args) throws ClientException {
  113. MediaHls mediaHls = new MediaHls();
  114. AddMediaWorkflowResponse response = mediaHls.addMediaWorkflow();
  115. System.out.println(JSONObject.toJSONString(response));
  116. }
  117. }
Thank you! We've received your feedback.