Sometimes, you might come across situations where you need to join multiple videos together. Imagine a scenario where you have various video clips that you want to stitch into one seamless video. For instance, let's say you want to attach an outro to a recorded video. To make this happen using PHP, you'll require a video editing API. Here's a quick and easy way to achieve this in your own PHP application using just a few lines of code:
// Don't forget to install the required library using Composer:
// composer require creatomate/creatomate
$client = new Creatomate\Client('Your API Key');
$output = $client->render([
'source' => new Creatomate\Source([
'output_format' => 'mp4',
'width' => 1920,
'height' => 1080,
'elements' => [
[
'type' => 'video',
'track' => 1,
'source' => 'https://cdn.creatomate.com/demo/drone.mp4'
],
[
'type' => 'video',
'track' => 1,
'source' => 'https://cdn.creatomate.com/demo/river.mp4',
'animations' => [
[
'time' => 'start',
'duration' => 1,
'transition' => true,
'type' => 'fade'
]
]
]
]
])
]);
var_dump($output);
How it works: Start by installing the Creatomate library from Packagist using Composer. To play the video clips in sequence, each element must be placed on the same track (in this case, track 1). No need to specify the start and duration for each clip, as the track adjusts automatically based on the total footage. To combine two videos, simply provide the URLs where they're hosted, and let the API handle the rest.
For a smooth transition between clips, use the "animations" property. This property accepts an array of animations applied to the element. In this case, we add a 1-second fade animation to the second video element. By setting the "transition" property to "true", it overlaps with the first video clip. The output video above shows the result after rendering. The fade animation is just one of many effects that can be applied to merging together multiple videos.
Merging videos is one of the features offered by the Creatomate video editing API. The JSON-based API allows automating any kind of video editing task in PHP. Together with the online video editor, it gives you all the tools you need to quickly set up code-driven video editing. Check out the editor below to see how to create your own video scenarios in PHP.