All Products
Search
Document Center

ApsaraVideo Media Processing:Merge and edit videos

Last Updated:Aug 29, 2023

This topic provides the sample code for using ApsaraVideo Media Processing (MPS) SDK for PHP to merge and edit videos.

Sample code

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

function initMtsClient($accessKeyId, $accessKeySecret) {
    $regionId = 'cn-shanghai';  // The ID of the region in which your MPS service is deployed.
    $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****';   # The ID of the MPS queue. You can log on to the MPS console to view the ID.
    $templateId = "S00000001-100020"; # The ID of the transcoding template. You can log on to the MPS console to view the ID.
    $ossLocation = 'oss-cn-shanghai';
    $bucket = '<bucket name>';
    $ossInputObject = 'input.mp4';
    $ossOutputObject = 'output.mp4';
    $headObject = 'head.mp4';
    $tailObject = 'tail.mp4';
    $request = new Mts\SubmitJobsRequest();
    # The job input.
    $input = array('Location' => $ossLocation,
               'Bucket' => $bucket,
               'Object' => urlencode($ossInputObject));

    # The job output.
    $output = array('OutputObject' => urlencode($ossOutputObject));
    $output['Video'] = array('Width' => 1280,
                            'Height' => 720);
    $output['TemplateId'] = $templateId;
    # The parameters for merging videos.
    $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);
}