全部產品
Search
文件中心

ApsaraVideo Media Processing:拼接和簡單剪輯

更新時間:Jun 30, 2024

本文提供PHP SDK使用拼接和簡單剪輯相關功能的API調用樣本。

完整代碼

<?php
require_once './aliyun-php-sdk-core/Config.php'; 
use Mts\Request\V20140618 as Mts;

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // 服務接入地區
    $profile = DefaultProfile::getProfile($regionId, $accessKeyId, $accessKeySecret);
    return new DefaultAcsClient($profile);
}

try {
    $client = initMtsClient(getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'));
    $response = submitClipJob($client);
    print_r($response);
} catch (Exception $e) {
    print $e->getMessage()."\n";
}

function submitClipJob($client) {
    $pipelineId = 'd7cedd984be7dd63395c****';   #管道ID  可在控制台查看
    $templateId = "S00000001-100020"; #轉碼模板ID,可在控制台查看
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output.mp4';
    $headObject = 'head.mp4';
    $tailObject = 'tail.mp4';
    $request = new Mts\SubmitJobsRequest();
    # input參數
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # output參數
    $output = array('OutputObject' => urlencode($ossOutputObject));
    $output['Video'] = array('Width' => 1280,
                            'Height' => 720);
    $output['TemplateId'] = $templateId;
    # 拼接參數
    $mergeVideo = array('MergeURL' => 'http://'.$bucket.'.'.$ossLocation.'.aliyuncs.com/'.urlencode($ossInputObject));
    $mergeTail = array('MergeURL' => 'http://'.$bucket.'.'.$ossLocation.'.aliyuncs.com/'.urlencode($tailObject));
    $output['MergeList'] = array($mergeVideo, $mergeTail);

    $outputs = array($output);

    $request->setInput(json_encode($input));
    $request->setOUtputs(json_encode($outputs));
    $request->setOutputBucket($bucket);
    $request->setOutputLocation($ossLocation);
    $request->setPipelineId($pipelineId);  
    return $client->getAcsResponse($request);
}