Create a media workflow
To produce videos that involve multiple bitrates, audio tracks, subtitles, or formats, or to run multiple jobs in a specified sequence or based on conditions, you can create a media workflow. In the workflow, you can configure nodes for transcoding, analysis, snapshot, packaging, review, media fingerprint extraction, and intelligent thumbnail generation. This topic provides sample code for using ApsaraVideo Media Processing (MPS) SDK for PHP to create a media 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 media workflow
Call the AddMediaWorkflow operation to create a media workflow. 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>";
private $outputBucket = "<OutputBucket>";
private $actStart = "Act-Start";
private $actTransCode = "Act-TransCode";
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" => "Transcoding workflow",
"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->actTransCode => $this->buildTranscodeActivity(),
$this->actReport => $this->buildReportActivity()
];
return $activities;
}
/**
* The start node.
*/
function buildStartActivity() {
$startActivity = array(
"Name" => $this->actStart,
"Type" => "Start",
"Parameters" => $this->buildStartParameters()
);
return $startActivity;
}
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 configurations.
*/
function buildTranscodeActivity() {
$transcodeParameters = array(
"Name" => $this->actTransCode,
"Type" => "Transcode",
"Parameters" => $this->buildTranscodeParameters()
);
return $transcodeParameters;
}
function buildTranscodeParameters() {
$transcodeParameters = array(
"OutputBucket" => $this->outputBucket,
"OutputLocation" => $this->ossLocation,
"Outputs" => $this->buildOutputsConfig()
);
return $transcodeParameters;
}
/**
* The output configurations.
*/
function buildOutputsConfig() {
$output = array(
"ObjectRegex" => $this->actTranscCode."/{RunId}/{FileName}",
"TemplateId" => $this->templateId
);
$outputs = array($output);
return $outputs;
}
/**
* The Report node.
*/
private $reportActivity = array(
"Type"=> "Report",
"Parameters"=> array(
"PublishType"=>"Auto"
)
);
/**
* Add nodes.
*/
function buildReportActivity() {
$reportActivity = array(
"Name" => $this->actReport,
"Parameters" => (object)[],
"Type" => "Report"
);
return $reportActivity;
}
/**
* Add dependencies.
*/
function buildDependencies() {
$subActivityOfStart = array(
$this->actTransCode
);
$subActivityOfTranscode = array(
$this->actReport
);
$dependencies = array(
$this->actStart => $subActivityOfStart,
$this->actTransCode => $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();