Video encryption protects video content from unauthorized access and distribution, helping prevent leaks and hotlinking in security-sensitive scenarios such as online education and finance. This topic provides sample code for using ApsaraVideo Media Processing (MPS) SDK for PHP to create an HTTP Live Streaming (HLS) encryption workflow.
Prerequisites
MPS SDK for PHP is installed and configured. For more information, see MPS SDK for PHP. To obtain more information about the SDK and view examples on how to use the SDK to call API operations, visit OpenAPI Explorer.
Create a workflow for HLS encryption
Call the AddMediaWorkflow operation to configure HLS encryption parameters. For more information about the request and response parameters, see AddMediaWorkflow. Sample request:
<?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"; # The ID of the transcoding template. The output file is in the M3U8 format. Select a template based on your business requirements.
private $ossLocation = "<OssLocation>";
private $inputBucket = "<InputBucket>";
private $inputPath = "<InputPath>"; # The input path. Example: HLS-Encryption.
private $outputBucket = "<OutputBucket>";
private $encryptionType = "hls-aes-128";
private $hlsKeyUri = "<URI of the decryption key>"; # Example: 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
* We recommend that you set the protocol parameter to HTTPS in a production environment.
*/
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
* The includePlayList parameter specifies whether to return the playback information.
* The includeSnapshotList parameter specifies whether to return the information about the snapshots.
* The includeMediaInfo parameter specifies whether to return the information about the media files.
* The includeSummaryList parameter specifies whether to return the summary.
*/
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" => "Workflow for HLS encryption",
"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;
}
/**
* The start node.
*/
function buildStartParameters() {
$startParameters = array(
"PipelineId" => $this->pipelineId,
"InputFile" => $this->buildInputFile()
);
return $startParameters;
}
/**
* The trigger path.
*/
function buildInputFile() {
$inputFile = array(
"Bucket" => $this->inputBucket,
"Location" => $this->ossLocation,
"ObjectPrefix" => $this->inputPath
);
return $inputFile;
}
/**
* The transcoding node.
*/
function buildTranscodeActivity() {
$transcodeParameters = array(
"Name" => $this->actEncryption,
"Type" => "Transcode",
"Parameters" => $this->buildTranscodeParameters()
);
return $transcodeParameters;
}
/**
* The transcoding configurations.
*/
function buildTranscodeParameters() {
$transcodeParameters = array(
"OutputBucket" => $this->outputBucket,
"OutputLocation" => $this->ossLocation,
"Outputs" => $this->buildOutputsConfig()
);
return $transcodeParameters;
}
/**
* The output configurations.
*/
function buildOutputsConfig() {
$output = array(
"ObjectRegex" => $this->actEncryption."/{RunId}/{FileName}",
"TemplateId" => $this->templateId,
"Encryption" => $this->buildEncryption()
);
$outputs = array($output);
return $outputs;
}
/**
* The encryption configurations.
*/
function buildEncryption() {
$encryption = array(
"Type" => $this->encryptionType,
"KeyUri" => $this->hlsKeyUri
);
return $encryption;
}
/**
* Add nodes.
*/
function buildReportActivity() {
$reportActivity = array(
"Name" => $this->actReport,
"Parameters" => (object)[],
"Type" => "Report"
);
return $reportActivity;
}
/**
* Add dependencies.
*/
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();
该文章对您有帮助吗?