Easy to learn and get started, the Shotstack PHP SDK will help you and your developers build a video editing application in days instead of months.
The open-source PHP video editing library talks directly to Shotstack's fully managed video editing API which means you don't need to worry about setting up servers, installing FFmpeg or creating your own PHP video editing scripts.
The PHP video editing SDK provides access to all of Shotstack's video editing features including cut and trim, stitching clips, transitions, filters and effects and lets you merge videos, images, text and audio.
The Shotstack PHP video editing library is available as a Composer package and can be installed directly from Packagist or downloaded from GitHub.
composer require shotstack/shotstack-sdk-php
Require the library in to your script. Make sure the path is correct and points to the vendor folder at the root of your project.
require 'vendor/autoload.php';
Import the features from the SDK needed to create a basic edit using use statements.
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;
Authenticate and configure the client library. Register and replace the key with your own API key and set the host to stage or v1. Ideally you will load the key using environment variables.
$config = Configuration::getDefaultConfiguration()
->setHost('https://api.shotstack.io/stage')
->setApiKey('x-api-key', 'H7jKyj90kd09lbLOF7J900jNbSWS67X87xs9j0cD');
$client = new EditApi(null, $config);
The possibilities are limitless but in this example a video clips start and end point will be trimmed. Set the video asset trim to 3 to chop the first 3 seconds off the clip and then play the next 8 seconds by setting the length to 8. Setting the start to 0 means the video clip start straight away.
$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);
Each clip needs to be added to an array of a track. You can have multiple tracks layered over the top of each other.
$track = new Track();
$track->setClips([$videoClip]);
The timeline is made up of tracks that specifies the sequence of clips and when they start and finish.
$timeline = new Timeline();
$timeline->setTracks([$track]);
To create a video the output format and resolution need to be specified, in this example an SD resolution is used and output is mp4.
$output = new Output();
$output
->setFormat('mp4')
->setResolution('sd');
The final edit is made up of the timeline and the output format.
$edit = new Edit();
$edit
->setTimeline($timeline)
->setOutput($output);
Post the edit to the API for rendering.
$render = $client->postRender($edit)->getResponse();
Finally, check the status of the render, and when done output the video URL.
$video = $client->getRender($render->getId())->getResponse();
if ($video->getStatus() === 'done') {
echo $video->getUrl();
}
The PHP code above will prepare a JSON payload, describing the video editing parameters to trim a video clip, POST it to the Shotstack API and generate this video.
This application is the perfect example of a well executed and documented API. In less than 10 mins, set up, web hook done, and first render!
There are a couple of other options out there that attempt to provide the same or similar solution, but none of them come close in terms of quality, ease of use, and speed.
Shotstack was EXACTLY what I was looking for, and incredibly easy to get started with. You guys are killing it.