如果需要制作多码率、多音轨、多字幕或多格式等类型视频,或希望预先编排好一系列任务流程,然后按照编排好的环节和条件依次执行任务,您可以创建工作流并在工作流中设置转码、分析、截图、打包(封装)、审核、提取视频DNA、设置智能封面等任务节点。本文介绍媒体处理PHP SDK V2.0新增媒体工作流的示例代码。
示例代码
<?php
namespace AlibabaCloud\SDK\Sample;
use AlibabaCloud\SDK\Mts\V20140618\Mts;
use \Exception;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Utils\Utils;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Mts\V20140618\Models\AddMediaWorkflowRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
class Sample {
/**
* 使用AK&SK初始化账号Client
* @return Mts Client
*/
public static function createClient(){
$config = new Config([
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
"accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
"accessKeySecret" => getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")
]);
$config->endpoint = "mts.cn-hangzhou.aliyuncs.com";
return new Mts($config);
}
/**
* @param string[] $args
* @return void
*/
public static function main($args){
$client = self::createClient();
$addMediaWorkflowRequest = new AddMediaWorkflowRequest([
//媒体工作流名称
"name" => "mediaworkflow-example",
//媒体工作流拓扑结构
"topology" => "{\"Activities\": {\"mediaworkflow-example\": {\"Parameters\": {\"Outputs\": \"[{\"OutputObject\":\"examplebucket/output/{RunId}/TRANSCODE_165941222****/{FileName}\",\"TemplateId\":\"S00000001-200010\",\"TemplateName\":\"MP4-流畅\"}]\",\"OutputBucket\": \"examplebucket\",\"OutputLocation\": \"oss-cn-shanghai\"},\"Type\": \"Transcode\"},\"Act-Start\": {\"Parameters\": {\"PipelineId\": \"a7d481f07d8c45da88c71853ce7d****\",\"InputFile\": \"{\"Bucket\":\"example-input\",\"Location\":\"oss-cn-shanghai\",\"ObjectPrefix\":\"mps-test/input/\"}\"},\"Type\": \"Start\"},\"Act-Report\": {\"Parameters\": {\"PublishType\": \"Manual\"},\"Type\": \"Report\"}},\"Dependencies\": {\"mediaworkflow-example\": [\"Act-Report\"],\"Act-Start\": [\"mediaworkflow-example\"],\"Act-Report\": []}}",
//触发模式
"triggerMode" => "OssAutoTrigger"
]);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值
$client->addMediaWorkflowWithOptions($addMediaWorkflowRequest, $runtime);
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
$error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
}
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
// 错误 message
var_dump($error->message);
// 诊断地址
var_dump($error->data["Recommend"]);
Utils::assertAsString($error->message);
}
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main(array_slice($argv, 1));