Video merging combines videos of different formats, bitrates, and resolutions into a single output video. Common use cases include adding a start or end part to a video and joining recorded live stream clips. Video clipping extracts a segment from a video and saves it as a new file, which is useful for capturing highlights. The following example calls the SubmitJobs operation in ApsaraVideo Media Processing (MPS) SDK for PHP to merge and clip videos.
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.
Merge and clip videos
Call the SubmitJobs operation to merge and clip videos. For more information about request and response parameters, see SubmitJobs. 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\SubmitJobsRequest;
class Sample {
private $pipelineId = "<PipelineId>";
private $templateId = "S00000001-100020"; # The ID of the transcoding template. Select a template based on your business requirements.
private $ossLocation = "<OssLocation>";
private $bucket = "<bucket name>";
private $oss_input_object = "input.mp4";
private $oss_output_object = "output.mp4";
private $head_object = "head.mp4";
private $tail_object = "tail.mp4";
/**
* @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
*/
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 SubmitJobsRequest([
"input" => json_encode(array(
'Location' => $sample->ossLocation,
'Bucket' => $sample->bucket,
'Object' => urlencode($sample->head_object))
),
"outputBucket" => $sample->bucket,
"outputLocation" => $sample->ossLocation,
"pipelineId" => $sample->pipelineId,
"outputs" => $sample->outputs(),
]);
$response = $client->submitJobs($request);
Console::log(Utils::toJSONString(Tea::merge($response->body)));
}
function outputs() {
$output = array('OutputObject' => urlencode($this->oss_output_object));
$output['Video'] = array('Width' => 1280,
'Height' => 720);
$output['TemplateId'] = $this->templateId;
// The parameters configured for merging videos.
$merge_video = array('MergeURL' => 'http://'.$this->bucket.'.'.$this->ossLocation.'.aliyuncs.com/'.urlencode($this->oss_input_object));
$merge_tail = array('MergeURL' => 'http://'.$this->bucket.'.'.$this->ossLocation.'.aliyuncs.com/'.urlencode($this->tail_object));
$output['MergeList'] = array($merge_video, $merge_tail);
$outputs = array($output);
return json_encode($outputs);
}
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
require_once $path;
}
Sample::main();