All Products
Search
Document Center

ApsaraVideo Media Processing:Create a media workflow

Last Updated:Nov 19, 2024

If you want to produce videos that involve multiple bitrates, audio tracks, subtitles, and formats, or want to run multiple jobs based on the specified sequence or conditions, you can create a workflow. In the workflow, you can configure nodes for running jobs such as transcoding, analysis, snapshot, packaging or encapsulation, review, media fingerprint extraction, and intelligent thumbnail configuration. This topic provides an example on how to use ApsaraVideo Media Processing (MPS) SDK for PHP V2.0 to create a media workflow.

Sample code

<?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 {

    /**
     * Use your AccessKey ID and AccessKey secret to initialize the client.
     * @return Mts Client
     */
    public static function createClient(){

        $config = new Config([
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID environment variable is configured. 
                "accessKeyId" => getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                // Required. Make sure that the ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variable is configured. 
                "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([
                // The name of the media workflow.
                "name" => "mediaworkflow-example",
                // The topology of the media workflow.
                "topology" => "{\"Activities\": {\"mediaworkflow-example\": {\"Parameters\": {\"Outputs\": \"[{\"OutputObject\":\"examplebucket/output/{RunId}/TRANSCODE_165941222****/{FileName}\",\"TemplateId\":\"S00000001-200010\",\"TemplateName\":\"MP4-Low definition\"}]\",\"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\": []}}",
                // The trigger mode.
                "triggerMode" => "OssAutoTrigger"
        ]);
        $runtime = new RuntimeOptions([]);
        try {
            // Write your own code to display the response of the API operation if necessary.
            $client->addMediaWorkflowWithOptions($addMediaWorkflowRequest, $runtime);
        }
        catch (Exception $error) {
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            // Handle exceptions with caution in actual business scenarios and never ignore exceptions in your project. In this example, error messages are displayed for reference only. 
            // The error message.
            var_dump($error->message);
            // The URL of the corresponding error diagnostics page.
            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));

References