視頻加密旨在通過對視頻內容進行深度安全處理,確保視頻數據不被非法獲取和傳播,可有效防止視頻泄露和盜鏈問題,廣泛用于在線教育及財經等對內容安全性要求高的領域。本文提供PHP SDK創建HLS標準加密工作流相關功能的示例代碼。
前提條件
創建HLS標準加密工作流
調用AddMediaWorkflow創建媒體工作流,HLS加密參數可根據您的實際業務需要自行配置,接口參數和返回字段詳細信息請參見新增媒體工作流,調用示例如下:
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Mts\V20140618\Mts;
use AlibabaCloud\Darabonba\Env\Env;
use AlibabaCloud\Tea\Tea;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Console\Console;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\AddMediaWorkflowRequest;
class Sample {
private $region = '<region>';
private $pipelineId = "<PipelineId>";
private $templateId = "S00000001-100020"; #轉碼模板ID,m3u8模板,按需配置
private $ossLocation = "<OssLocation>";
private $inputBucket = "<InputBucket>";
private $inputPath = "<InputPath>"; #如 "HLS-Encryption"
private $outputBucket = "<OutputBucket>";
private $encryptionType = "hls-aes-128";
private $hlsKeyUri = "<解密密鑰的URI>"; #示例:http://example.aliyundoc.com
private $actStart = "Act-Start";
private $actEncryption = "Act-HLS-Encryption";
private $actReport = "Act-Report";
/**
* @param string $accessKeyId
* @param string $accessKeySecret
* @param string $regionId
* @return Mts
* 正式環境下 建議 protocol = "HTTPS";
*/
public static function createClient($accessKeyId, $accessKeySecret, $regionId){
$config = new Config([]);
$config->accessKeyId = $accessKeyId;
$config->accessKeySecret = $accessKeySecret;
$config->regionId = $regionId;
$config->protocol = "HTTP";
return new Mts($config);
}
/**
* @return void
* includePlayList 返回結果中是否包含播放信息
* includeSnapshotList 返回結果中是否包含截圖信息
* includeMediaInfo 返回結果中是否包含媒體信息
* includeSummaryList 返回結果中是否包含摘要
*/
public static function main(){
$sample = new Sample;
$client = self::createClient(Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_ID"), Env::getEnv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), 'cn-shanghai');
$request = new AddMediaWorkflowRequest([
"name" => "HLS加密工作流",
"topology" => $sample->buildWorkflowTopology()
]);
$response = $client->addMediaWorkflow($request);
Console::log(Utils::toJSONString(Tea::merge($response->body)));
}
function buildWorkflowTopology() {
$activities = $this->buildActivities();
$dependencies = $this->buildDependencies();
$workflow = array(
"Activities" => $activities,
"Dependencies" => $dependencies
);
echo json_encode($workflow)."\n";
return json_encode($workflow);
}
function buildActivities() {
$activities = [
$this->actStart => $this->buildStartActivity(),
$this->actEncryption => $this->buildTranscodeActivity(),
$this->actReport => $this->buildReportActivity()
];
return $activities;
}
function buildStartActivity() {
$startActivity = array(
"Name" => $this->actStart,
"Type" => "Start",
"Parameters" => $this->buildStartParameters()
);
return $startActivity;
}
/**
* start節點
*/
function buildStartParameters() {
$startParameters = array(
"PipelineId" => $this->pipelineId,
"InputFile" => $this->buildInputFile()
);
return $startParameters;
}
/**
* 觸發路徑
*/
function buildInputFile() {
$inputFile = array(
"Bucket" => $this->inputBucket,
"Location" => $this->ossLocation,
"ObjectPrefix" => $this->inputPath
);
return $inputFile;
}
/**
* 轉碼節點
*/
function buildTranscodeActivity() {
$transcodeParameters = array(
"Name" => $this->actEncryption,
"Type" => "Transcode",
"Parameters" => $this->buildTranscodeParameters()
);
return $transcodeParameters;
}
/**
* 轉碼配置
*/
function buildTranscodeParameters() {
$transcodeParameters = array(
"OutputBucket" => $this->outputBucket,
"OutputLocation" => $this->ossLocation,
"Outputs" => $this->buildOutputsConfig()
);
return $transcodeParameters;
}
/**
* 輸出配置
*/
function buildOutputsConfig() {
$output = array(
"ObjectRegex" => $this->actEncryption."/{RunId}/{FileName}",
"TemplateId" => $this->templateId,
"Encryption" => $this->buildEncryption()
);
$outputs = array($output);
return $outputs;
}
/**
* 加密配置
*/
function buildEncryption() {
$encryption = array(
"Type" => $this->encryptionType,
"KeyUri" => $this->hlsKeyUri
);
return $encryption;
}
/**
* 構建Activity節點
*/
function buildReportActivity() {
$reportActivity = array(
"Name" => $this->actReport,
"Parameters" => (object)[],
"Type" => "Report"
);
return $reportActivity;
}
/**
* 構建依賴關系
*/
function buildDependencies() {
$subActivityOfStart = array(
$this->actEncryption
);
$subActivityOfTranscode = array(
$this->actReport
);
$dependencies = array(
$this->actStart => $subActivityOfStart,
$this->actEncryption => $subActivityOfTranscode,
$this->actReport => []
);
return $dependencies;
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main();
文檔內容是否對您有幫助?