创建HLS标准加密工作流

简介

视频加密旨在通过对视频内容进行深度安全处理,确保视频数据不被非法获取和传播,可有效防止视频泄露和盗链问题,广泛用于在线教育及财经等对内容安全性要求高的领域。本文提供PHP SDK创建HLS标准加密工作流相关功能的示例代码。

示例代码

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

$pipelineId = 'd7cedd984be7dd63395c****';   #管道ID  可在控制台查看
$templateId = "S00000001-100020"; #转码模板ID,m3u8模板,按需配置
$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"; #替换为真实解密服务地址  
$actStart = "Act-Start";
$actEncryption = "Act-HLS-Encryption";
$actReport = "Act-Report";

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // 服务接入区域
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

function createHLSEncryptionWorkflow($client) {
    $request = new Mts\AddMediaWorkflowRequest();
    $request->setName("HLS加密工作流");
    $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;
}