Skip to content

Instantly share code, notes, and snippets.

@derkzomer
Created December 15, 2023 06:16
Show Gist options
  • Save derkzomer/b518fcf47f9c0877e79355211eacdfaf to your computer and use it in GitHub Desktop.
Save derkzomer/b518fcf47f9c0877e79355211eacdfaf to your computer and use it in GitHub Desktop.
<?php
require 'vendor/autoload.php'; // Install the Shotstack SDK using: `composer require shotstack/shotstack-sdk-php`
use Shotstack\Client\Api\EditApi;
use Shotstack\Client\ApiException;
use Shotstack\Client\Configuration;
use Shotstack\Client\Model\Edit;
use Shotstack\Client\Model\Output;
use Shotstack\Client\Model\Timeline;
use Shotstack\Client\Model\Track;
use Shotstack\Client\Model\Clip;
use Shotstack\Client\Model\ImageAsset;
use Shotstack\Client\Model\VideoAsset;
function generatePermutations($items, $perms = [], &$all = [])
{
if (empty($items)) {
$all[] = $perms;
} else {
for ($i = count($items) - 1; $i >= 0; --$i) {
$newitems = $items;
$newperms = $perms;
list($foo) = array_splice($newitems, $i, 1);
array_unshift($newperms, $foo);
generatePermutations($newitems, $newperms, $all);
}
return $all;
}
}
$config = Configuration::getDefaultConfiguration()
->setHost('https://api.shotstack.io/v1')
->setApiKey('x-api-key', 'YOUR_API_KEY');
// Map mergeFields to their lengths
$mergeFields = [
'https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/footage/night-sky.mp4' => 13,
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4' => 27,
'https://github.com/shotstack/test-media/raw/main/captioning/scott-ko.mp4' => 28,
'https://cdn.shotstack.io/au/v1/gb0ckjoadi/18724ca5-d607-4171-920d-144270c9650d.mp4' => 16,
'https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/examples/images/pexels/pexels-photo-712850.jpeg' => 5
];
$client = new EditApi(null, $config);
// Generate all permutations of 5 clips
$permutations = generatePermutations(array_keys($mergeFields));
foreach ($permutations as $permutation) {
$track = new Track();
$clips = [];
$startTime = 0;
foreach ($permutation as $mergeField) {
$asset = (strpos($mergeField, '.jpeg') !== false) ? new ImageAsset() : new VideoAsset();
$asset->setSrc($mergeField);
$length = $mergeFields[$mergeField];
$clip = new Clip();
$clip->setAsset($asset)->setStart($startTime)->setLength($length);
$clips[] = $clip;
$startTime += $length;
}
$track->setClips($clips);
$timeline = new Timeline();
$timeline->setTracks([$track]);
$output = new Output();
$output->setFormat('mp4')->setResolution('sd');
$edit = new Edit();
$edit->setTimeline($timeline)->setOutput($output);
try {
$response = $client->postRender($edit)->getResponse();
echo $response->getMessage() . PHP_EOL;
} catch (ApiException $e) {
die('Request failed: ' . $e->getMessage() . $e->getResponseBody());
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment