Create a workflow for HLS encryption

更新时间:
复制 MD 格式

Background information

HLS encryption protects video content against unauthorized access and redistribution. It is commonly used in high-security scenarios such as online education and finance, where preventing video leaks and hotlinking is critical. This topic provides sample PHP code for using the ApsaraVideo Media Processing (MPS) SDK to create an HTTP Live Streaming (HLS) encryption workflow.

Sample code

The following example uses the PHP SDK to call AddMediaWorkflow and build a three-activity workflow: a Start activity that reads from OSS, a Transcode activity that applies AES-128 HLS encryption, and a Report activity that signals completion.

<?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"; # The endpoint used to obtain the decryption key.  
$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;
}