All Products
Search
Document Center

ApsaraVideo Media Processing:Create a workflow for HLS encryption

Last Updated:Nov 16, 2023

Background information

This topic provides the sample code for using ApsaraVideo Media Processing (MPS) SDK for PHP to create a workflow for HTTP-Live-Streaming (HLS) encryption. In the sample code, an API operation is called through the SDK. For more information about the API operation, see AddMediaWorkflow. For more information about how to install MPS SDK for PHP, see Install MPS SDK for PHP.

Sample code

<?php
require_once './aliyun-php-sdk-core/Config.php'; 
use Mts\Request\V20140618 as Mts;

$pipelineId = 'd7cedd984be7dd63395c****';   # The ID of the MPS queue. You can log on to the MPS console to view the ID.
$templateId = "S00000001-100020"; # The ID of the transcoding template. The output file is in the M3U8 format. Select a template based on your business requirements.
$ossLocation = 'oss-cn-shanghai';
$inputBucket = '<input bucket name>';
$inputPath  = "<input path>"; 
$outputBucket = "<output bucket name>";
$encryptionType = "hls-aes-128";
$hlsKeyUri = "http://example.aliyundoc.com/decryption"; # Replace with the actual endpoint of the decryption.  
$actStart = "Act-Start";
$actEncryption = "Act-HLS-Encryption";
$actReport = "Act-Report";

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // The ID of the region in which your MPS service is deployed.
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

function createHLSEncryptionWorkflow($client) {
    $request = new Mts\AddMediaWorkflowRequest();
    $request->setName("Workflow for HLS encryption");
    $request->setTopology(buildWorkflowTopology());
    return $client->getAcsResponse($request);
}

try {
    $client = initMtsClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $response = createHLSEncryptionWorkflow($client);
    print_r($response);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

function buildWorkflowTopology() {
    $activities = buildActivities();
    $dependencies = buildDependencies();
    $workflow = array(
                      "Activities" => $activities,
                      "Dependencies" => $dependencies
                     );
    echo json_encode($workflow)."\n";
    return json_encode($workflow);
}

function buildActivities() {
    global $actStart;
    global $actEncryption;
    global $actReport;
    $activities = [
        $actStart => buildStartActivity(),
        $actEncryption => buildTranscodeActivity(),
        $actReport => buildReportActivity()
    ];
    return $activities;
}

function buildStartActivity() {
    global $actStart;
    $startActivity = array(
        "Name" => $actStart,
        "Type" => "Start",
        "Parameters" => buildStartParameters()
    );
    return $startActivity;
}

function buildStartParameters() {
    global $pipelineId;
    $startParameters = array(
        "PipelineId" => $pipelineId,
        "InputFile" => buildInputFile()
    );
    return $startParameters;
}

function buildInputFile() {
    global $inputBucket;
    global $ossLocation;
    global $inputPath;
    $inputFile = array(
        "Bucket" => $inputBucket,
        "Location" => $ossLocation,
        "ObjectPrefix" => $inputPath
    );
    return $inputFile;
}

function buildTranscodeActivity() {
    global $actEncryption;
    $transcodeParameters = array(
        "Name" => $actEncryption,
        "Type" => "Transcode",
        "Parameters" => buildTranscodeParameters()
    );
    return $transcodeParameters;
}

function buildTranscodeParameters() {
    global $outputBucket;
    global $ossLocation;
    $transcodeParameters = array(
        "OutputBucket" => $outputBucket,
        "OutputLocation" => $ossLocation,
        "Outputs" => buildOutputsConfig()
    );
    return $transcodeParameters;
}

function buildOutputsConfig() {
    global $actEncryption;
    global $templateId;
    $output = array(
        "ObjectRegex" => $actEncryption."/{RunId}/{FileName}",
        "TemplateId" => $templateId,
        "Encryption" => buildEncryption()
    );
    $outputs = array($output);
    return $outputs;
}

function buildEncryption() {
    global $encryptionType;
    global $hlsKeyUri;
    $encryption = array(
        "Type" => $encryptionType,
        "KeyUri" => $hlsKeyUri
    );
    return $encryption;
}

function buildReportActivity() {
    global $actReport;
    $reportActivity = array(
        "Name" => $actReport,
        "Parameters" => (object)[],
        "Type" => "Report"
    );
    return $reportActivity;
}

function buildDependencies() {
    global $actEncryption;
    global $actReport;
    global $actStart;
    $subActivityOfStart = array(
        $actEncryption
    );
    $subActivityOfTranscode = array(
        $actReport
    );
    $dependencies = array(
        $actStart => $subActivityOfStart,
        $actEncryption => $subActivityOfTranscode,
        $actReport => []
    );
    return $dependencies;
}