edit-icon download-icon

Create HLS standard encryption workflow

Last Updated: Sep 10, 2018

Introduction

This document is an example of calling API to create HLS standard encryption workflow. For more information about creating HLS standard encryption workflow and playing encryption videos, see HLS encryption and play. For more information about MPS SDK, see Installation.

Example

  1. import json
  2. from aliyunsdkmts.request.v20140618 import AddMediaWorkflowRequest
  3. from aliyunsdkcore import client
  4. REGION_ID = '<region>'
  5. ACCESS_KEY_ID = '<accessKeyId>'
  6. ACCESS_KEY_SECRET = '<accessKeySecret>'
  7. PIPELINE_ID = "<PipelineId>"
  8. TEMPLATE_ID = "S00000001-100020" #Transcoding template ID, m3u8 template, set as needed
  9. OSS_LOCATION = "<OssLocation>"
  10. INPUT_BUCKET = "<InputBucket>"
  11. INPUT_PATH = "<InputPath>" #Example: "HLS-Encryption"
  12. OUTPUT_BUCKET = "<OutputBucket>"
  13. ENCRYPTION_TYPE = "hls-aes-128"
  14. HLS_KEY_URI = "< URI of decryption key >" #Example: http://decrypt.testdomain.com
  15. ACT_START = "Act-Start"
  16. ACT_ENCRYPTION = "Act-HLS-Encryption"
  17. ACT_REPORT = "Act-Report"
  18. def addMediaWorkflow():
  19. global client
  20. client = client.AcsClient(ACCESS_KEY_ID, ACCESS_KEY_SECRET, REGION_ID)
  21. request = AddMediaWorkflowRequest.AddMediaWorkflowRequest()
  22. request.set_Topology(buildWorkflowTopology())
  23. request.set_Name("HLS encryption workflow py")
  24. response = client.do_action_with_exception(request)
  25. print json.loads(response)
  26. def buildWorkflowTopology():
  27. workflow = {}
  28. workflow["Activities"] = buildActivities()
  29. workflow["Dependencies"] = buildDependencies()
  30. print json.dumps(workflow)
  31. return json.dumps(workflow)
  32. def buildActivities():
  33. activities = {}
  34. activities[ACT_START] = buildStartActivity()
  35. activities[ACT_ENCRYPTION] = buildTranscodeActivity()
  36. activities[ACT_REPORT] = buildReportActivity()
  37. return activities
  38. def buildStartActivity():
  39. startActivity = {}
  40. startActivity["Name"] = ACT_START
  41. startActivity["Type"] = "Start"
  42. startActivity["Parameters"] = buildStartParameters()
  43. return startActivity
  44. def buildStartParameters():
  45. startParameters = {}
  46. startParameters["PipelineId"] = PIPELINE_ID
  47. startParameters["InputFile"] = buildInputFile();
  48. return startParameters
  49. def buildInputFile():
  50. inputFile = {}
  51. inputFile["Bucket"] = INPUT_BUCKET
  52. inputFile["Location"] = OSS_LOCATION
  53. inputFile["ObjectPrefix"] = INPUT_PATH
  54. return inputFile
  55. def buildTranscodeActivity():
  56. transcodeActivity = {}
  57. transcodeActivity["Name"] = ACT_ENCRYPTION
  58. transcodeActivity["Type"] = "Transcode"
  59. transcodeActivity["Parameters"] = buildTranscodeParameters()
  60. return transcodeActivity
  61. def buildTranscodeParameters():
  62. transcodeParameters = {}
  63. transcodeParameters["OutputBucket"] = OUTPUT_BUCKET
  64. transcodeParameters["OutputLocation"] = OSS_LOCATION
  65. transcodeParameters["Outputs"] = buildOutputsConfig()
  66. return transcodeParameters
  67. def buildOutputsConfig():
  68. outputs = []
  69. output = {}
  70. output["ObjectRegex"] = ACT_ENCRYPTION + "/{RunId}/{FileName}"
  71. output["TemplateId"] = TEMPLATE_ID
  72. output["Encryption"] = buildEncryption()
  73. outputs.append(output)
  74. return outputs
  75. def buildEncryption():
  76. encryption = {}
  77. encryption["Type"] = ENCRYPTION_TYPE
  78. encryption["KeyUri"] = HLS_KEY_URI
  79. return encryption
  80. def buildReportActivity():
  81. reportActivity = {}
  82. reportActivity["Name"] = ACT_REPORT
  83. reportActivity["Parameters"] = buildReportParameters()
  84. reportActivity["Type"] = "Report"
  85. return reportActivity
  86. def buildReportParameters():
  87. parameters = {}
  88. parameters["PublishType"] = "Auto"
  89. return parameters
  90. def buildDependencies():
  91. dependencies ={}
  92. subActivityOfStart = [ACT_ENCRYPTION]
  93. dependencies[ACT_START] = subActivityOfStart
  94. subActivityOfTranscode = [ACT_REPORT]
  95. dependencies[ACT_ENCRYPTION] = subActivityOfTranscode
  96. dependencies[ACT_REPORT] = []
  97. return dependencies
  98. if __name__ == "__main__":
  99. addMediaWorkflow()
Thank you! We've received your feedback.