All Products
Search
Document Center

ApsaraVideo Media Processing:Create a workflow for HLS encryption

Last Updated:Apr 07, 2024

Background information

The video encryption feature allows you to perform in-depth security processing on video content and prevent video data from being illegally acquired and transmitted. This feature is widely used to prevent video leaks and hotlinking in scenarios that require high security, such as online education and finance. 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.

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"; # 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;
}