edit-icon download-icon

Create HLS standard encryption workflow

Last Updated: Apr 19, 2018

Purpose

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

Code dependency example

For more information about MPS SDK, see Installation.

Example

  1. <?php
  2. include_once 'aliyun-php-sdk-core/Config.php';
  3. use Mts\Request\V20140618 as Mts;
  4. date_default_timezone_set('PRC');
  5. class HLSEncryptionWorkflowDemo {
  6. private $client;
  7. private $region = '<region>';
  8. private $accessKeyId = '<accessKeyId>';
  9. private $accessKeySecret = '<accessKeySecret>';
  10. private $pipelineId = "<PipelineId>";
  11. private $templateId = "S00000001-100020"; #transcoding template ID,m3u8 template, set as needed
  12. private $ossLocation = "<OssLocation>";
  13. private $inputBucket = "<InputBucket>";
  14. private $inputPath = "<InputPath>"; #For example "HLS-Encryption"
  15. private $outputBucket = "<OutputBucket>";
  16. private $encryptionType = "hls-aes-128";
  17. private $hlsKeyUri = "<decryption key URI>"; #For example, http://decrypt.testdomain.com
  18. private $actStart = "Act-Start";
  19. private $actEncryption = "Act-HLS-Encryption";
  20. private $actReport = "Act-Report";
  21. function __construct() {
  22. $profile = DefaultProfile::getProfile($this->region, $this->accessKeyId, $this->accessKeySecret);
  23. $this->client = new DefaultAcsClient($profile);
  24. }
  25. function addMediaWorkflow() {
  26. $request = new Mts\AddMediaWorkflowRequest();
  27. $request->setName("HLS encryption workflow php");
  28. $request->setTopology($this->buildWorkflowTopology());
  29. $response = $this->client->getAcsResponse($request);
  30. echo json_encode($response);
  31. }
  32. function buildWorkflowTopology() {
  33. $activities = $this->buildActivities();
  34. $dependencies = $this->buildDependencies();
  35. $workflow = array(
  36. "Activities" => $activities,
  37. "Dependencies" => $dependencies
  38. );
  39. echo json_encode($workflow)."\n";
  40. return json_encode($workflow);
  41. }
  42. function buildActivities() {
  43. $activities = [
  44. $this->actStart => $this->buildStartActivity(),
  45. $this->actEncryption => $this->buildTranscodeActivity(),
  46. $this->actReport => $this->buildReportActivity()
  47. ];
  48. return $activities;
  49. }
  50. function buildStartActivity() {
  51. $startActivity = array(
  52. "Name" => $this->actStart,
  53. "Type" => "Start",
  54. "Parameters" => $this->buildStartParameters()
  55. );
  56. return $startActivity;
  57. }
  58. function buildStartParameters() {
  59. $startParameters = array(
  60. "PipelineId" => $this->pipelineId,
  61. "InputFile" => $this->buildInputFile()
  62. );
  63. return $startParameters;
  64. }
  65. function buildInputFile() {
  66. $inputFile = array(
  67. "Bucket" => $this->inputBucket,
  68. "Location" => $this->ossLocation,
  69. "ObjectPrefix" => $this->inputPath
  70. );
  71. return $inputFile;
  72. }
  73. function buildTranscodeActivity() {
  74. $transcodeParameters = array(
  75. "Name" => $this->actEncryption,
  76. "Type" => "Transcode",
  77. "Parameters" => $this->buildTranscodeParameters()
  78. );
  79. return $transcodeParameters;
  80. }
  81. function buildTranscodeParameters() {
  82. $transcodeParameters = array(
  83. "OutputBucket" => $this->outputBucket,
  84. "OutputLocation" => $this->ossLocation,
  85. "Outputs" => $this->buildOutputsConfig()
  86. );
  87. return $transcodeParameters;
  88. }
  89. function buildOutputsConfig() {
  90. $output = array(
  91. "ObjectRegex" => $this->actEncryption."/{RunId}/{FileName}",
  92. "TemplateId" => $this->templateId,
  93. "Encryption" => $this->buildEncryption()
  94. );
  95. $outputs = array($output);
  96. return $outputs;
  97. }
  98. function buildEncryption() {
  99. $encryption = array(
  100. "Type" => $this->encryptionType,
  101. "KeyUri" => $this->hlsKeyUri
  102. );
  103. return $encryption;
  104. }
  105. function buildReportActivity() {
  106. $reportActivity = array(
  107. "Name" => $this->actReport,
  108. "Parameters" => (object)[],
  109. "Type" => "Report"
  110. );
  111. return $reportActivity;
  112. }
  113. function buildDependencies() {
  114. $subActivityOfStart = array(
  115. $this->actEncryption
  116. );
  117. $subActivityOfTranscode = array(
  118. $this->actReport
  119. );
  120. $dependencies = array(
  121. $this->actStart => $subActivityOfStart,
  122. $this->actEncryption => $subActivityOfTranscode,
  123. $this->actReport => []
  124. );
  125. return $dependencies;
  126. }
  127. }
  128. $demo = new HLSEncryptionWorkflowDemo();
  129. $demo->addMediaWorkflow();
  130. ?>
Thank you! We've received your feedback.