Shotstack PHP SDK
PHP SDK for the Shotstack PHP video editor and cloud video editing API.
Shotstack is a cloud based video editing platform that enables the editing of videos using code.
The platform uses an API and a JSON format for specifying how videos should be edited and what assets and titles should be used.
A server based render farm takes care of rendering the videos allowing multiple videos to be created simultaneously.
For examples of how to use the SDK to create videos using code checkout the PHP demo repo: https://github.com/shotstack/php-demos
Contents
Using the PHP SDK
Requirements
Requires PHP7.4+ and uses Guzzle7.3+.
Installation
The recommended way to use the SDK is as a composer package. Install using the command:
composer require shotstack/shotstack-sdk-php
Video Editing
The Shotstack SDK enables programmatic video editing via the Edit API render endpoint. Prepare JSON edits using the
provided schema classes and POST to the API for rendering.
Video Editing Example
The example below trims the start of a video clip and plays it for 8 seconds. The edit is prepared using the SDK models
and then sent to the API for rendering.
<?php
require 'vendor/autoload.php';
use Shotstack\Client\Api\EditApi;
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\VideoAsset;
$config = Configuration::getDefaultConfiguration()
->setHost('https://api.shotstack.io/stage')
->setApiKey('x-api-key', 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'); // use the correct API key
$client = new EditApi(null, $config);
$videoAsset = new VideoAsset();
$videoAsset
->setSrc('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4')
->setTrim(3);
$videoClip = new Clip();
$videoClip
->setAsset($videoAsset)
->setLength(8)
->setStart(0);
$track = new Track();
$track->setClips([$videoClip]);
$timeline = new Timeline();
$timeline->setTracks([$track]);
$output = new Output();
$output
->setFormat('mp4')
->setResolution('sd');
$edit = new Edit();
$edit
->setTimeline($timeline)
->setOutput($output);
$render = $client->postRender($edit)->getResponse();
Status Check Example
The example request below can be called a few seconds after the render above is posted. It will return the status of
the render, which can take several seconds to process. It uses the render->getId() method returned by the postRender
request.
use Shotstack\Client\Api\EditApi;
use Shotstack\Client\Configuration;
$config = Configuration::getDefaultConfiguration()
->setHost('https://api.shotstack.io/stage')
->setApiKey('x-api-key', 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'); // use the correct API key
$client = new EditApi(null, $config);
$video = $client->getRender($render->getId())->getResponse();
if ($video->getStatus() === 'done') {
echo $video->getUrl();
}
Save a Template Example
The example below uses the Edit we create in the Video Editing Example and saves it as a
template. The template can be rendered at a later date and can include placeholders. Placeholders can be replaced
when rendered using merge fields.
This example uses a placeholder for the video src (URL), trim (TRIM), and length (LENGTH) to allow you to trim any video
using a template.
<?php
require 'vendor/autoload.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\VideoAsset;
use Shotstack\Client\Model\Template;
$config = Configuration::getDefaultConfiguration()
->setHost('https://api.shotstack.io/stage')
->setApiKey('x-api-key', 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD'); // use the correct API key
$client = new EditApi(null, $config);
$videoAsset = new videoAsset();
$videoAsset
->setSrc('{{ URL }}')
->setTrim('{{ TRIM }}');
$videoClip = new Clip();
$videoClip
->setAsset($videoAsset)
->setStart(0)
->setLength('{{ LENGTH }}');
$track = new Track();
$track
->setClips([$videoClip]);
$timeline = new Timeline();
$timeline
->setTracks([$track]);
$output = new Output();
$output
->setFormat('mp4')
->setResolution('sd');
$edit = new Edit();
$edit
->setTimeline($timeline)
->setOutput($output);
$template = new Template();
$template
->setName('Trim Template')
->setTemplate($edit);
$response = $client->postTemplate($template)->getResponse();
Render a Template Example
The example below renders the template we created in the previous example and includes merge fields that will replace
the placeholders. Once submitted use the returned render ID and call the Status Check Example
to get the render progress. It uses the response->getId() method returned by the postTemplate request.