PHP code example of shotstack / shotstack-sdk-php

1. Go to this page and download the library: Download shotstack/shotstack-sdk-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shotstack / shotstack-sdk-php example snippets



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();

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();
}


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();

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);

$mergeFieldUrl = new MergeField();
$mergeFieldUrl
    ->setFind('URL')
    ->setReplace('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/footage/skater.hd.mp4');

$mergeFieldTrim = new MergeField();
$mergeFieldTrim
    ->setFind('TRIM')
    ->setReplace(3);

$mergeFieldLength = new MergeField();
$mergeFieldLength
    ->setFind('LENGTH')
    ->setReplace(6);

$template = new TemplateRender();
$template
    ->setId($response->id)
    ->setMerge([
        $mergeFieldUrl,
        $mergeFieldTrim,
        $mergeFieldLength,
    ]);

$video = $client->postTemplateRender($template)->getResponse();

if ($video->getStatus() === 'done') {
    echo $video->getUrl();
}

use Shotstack\Client\Model\Edit;

$edit = new Edit();
$edit
  ->setTimeline($timeline)
  ->setOutput($output)
  ->setMerge($merge)
  ->setCallback('https://my-server.com/callback.php')
  ->setDisk('local');

use Shotstack\Client\Model\Timeline;

$timeline = new Timeline();
$timeline
  ->setSoundtrack($soundtrack)
  ->setBackground('#000000')
  ->setFonts($fonts)
  ->setTracks($tracks)
  ->setCache(true);

use Shotstack\Client\Model\Soundtrack;

$soundtrack = new Soundtrack();
$soundtrack
  ->setSrc('https://s3-ap-southeast-2.amazonaws.com/shotstack-assets/music/disco.mp3')
  ->setEffect('fadeIn')
  ->setVolume(1);

use Shotstack\Client\Model\Font;

$font = new Font();
$font
  ->setSrc('https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/fonts/OpenSans-Regular.ttf');

use Shotstack\Client\Model\Track;

$track = new Track();
$track
  ->setClips($clips);

use Shotstack\Client\Model\Clip;

$clip = new Clip();
$clip
  ->setAsset($asset)
  ->setStart(2)
  ->setLength(5)
  ->setFit('crop')
  ->setScale(0)
  ->setPosition('center')
  ->setOffset($offset)
  ->setTransition($transition)
  ->setEffect('zoomIn')
  ->setFilter('greyscale')
  ->setOpacity(1)
  ->setTransform($transform);

use Shotstack\Client\Model\ImageAsset;

$imageAsset = new ImageAsset();
$imageAsset
  ->setSrc('https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/images/earth.jpg')
  ->setCrop($crop);

use Shotstack\Client\Model\TitleAsset;

$titleAsset = new TitleAsset();
$titleAsset
  ->setText('My Title')
  ->setStyle('minimal')
  ->setColor('#ffffff')
  ->setSize('medium')
  ->setBackground('#000000')
  ->setPosition('center')
  ->setOffset($offset);

use Shotstack\Client\Model\HtmlAsset;

$htmlAsset = new HtmlAsset();
$htmlAsset
  ->setHtml('<p>Hello <b>World</b></p>')
  ->setCss('p { color: #ffffff; } b { color: #ffff00; }')
  ->setWidth(400)
  ->setHeight(200)
  ->setBackground('transparent')
  ->setPosition('center);

use Shotstack\Client\Model\AudioAsset;

$audioAsset = new AudioAsset();
$audioAsset
  ->setSrc('https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/music/unminus/lit.mp3')
  ->setTrim(2)
  ->setVolume(0.5)
  ->setEffect('fadeInFadeOut');

use Shotstack\Client\Model\LumaAsset;

$lumaAsset = new LumaAsset();
$lumaAsset
  ->setSrc('https://shotstack-assets.s3-ap-southeast-2.amazonaws.com/examples/luma-mattes/paint-left.mp4')
  ->setTrim(5);

use Shotstack\Client\Model\Transition;

$transition = new Transition();
$transition
  ->setIn('fade')
  ->setOut('fade');

use Shotstack\Client\Model\Offset;

$offset = new Offset();
$offset
  ->setX(0.1)
  ->setY(-0.2);

use Shotstack\Client\Model\Crop;

$crop = new Crop();
$crop
  ->setTop(0.15)
  ->setBottom(0.15)
  ->setLeft(0)
  ->setRight(0);

use Shotstack\Client\Model\Transformation;

$transformation = new Transformation();
$transformation
  ->setRotate($rotate)
  ->setSkew($skew)
  ->setFlip($flip);

use Shotstack\Client\Model\RotateTransformation;

$rotateTransformation = new RotateTransformation();
$rotateTransformation
  ->setAngle(45);

use Shotstack\Client\Model\SkewTransformation;

$skewTransformation = new SkewTransformation();
$skewTransformation
  ->setX(0.5)
  ->setY(0.5);

use Shotstack\Client\Model\FlipTransformation;

$flipTransformation = new FlipTransformation();
$flipTransformation
  ->setHorizontal(true)
  ->setVertical(true);

use Shotstack\Client\Model\MergeField;

$mergeField = new MergeField();
$mergeField
  ->setFind('NAME')
  ->setReplace('Jane');

use Shotstack\Client\Model\Template;

$template = new Template();
$template
  ->setName('My Template')
  ->setTemplate($edit);

use Shotstack\Client\Model\TemplateRender;

$render = new TemplateRender();
$render
  ->setId('21e781c0-8232-4418-fec1-cc99f0280c21')
  ->setMerge($merge);

use Shotstack\Client\Model\Output;

$output = new Output();
$output
  ->setFormat('mp4')
  ->setResolution('sd')
  ->setAspectRatio('16:9')
  ->setSize($size)
  ->setFps(25)
  ->setScaleTo('preview')
  ->setQuality('mediue')
  ->setRepeat(true)
  ->setMute(false)
  ->setRange($range)
  ->setPoster($poster)
  ->setThumbnail($thumbnail)
  ->setDestinations($destinations);

use Shotstack\Client\Model\Size;

$size = new Size();
$size
  ->setWidth(1200)
  ->setHeight(800);

use Shotstack\Client\Model\Range;

$range = new Range();
$range
  ->setStart(3)
  ->setLength(6);

use Shotstack\Client\Model\Poster;

$poster = new Poster();
$poster
  ->setCapture(1);

use Shotstack\Client\Model\Thumbnail;

$thumbnail = new Thumbnail();
$thumbnail
  ->setCapture(1)
  ->setScale(0.3);

use Shotstack\Client\Model\ShotstackDestination;

$shotstackDestination = new ShotstackDestination();
$shotstackDestination
  ->setProvider('shotstack')
  ->setExclude(false);

use Shotstack\Client\Model\MuxDestination;

$muxDestination = new MuxDestination();
$muxDestination
  ->setProvider('mux')
  ->setOptions($muxDestinationOptions);

use Shotstack\Client\Model\MuxDestinationOptions;

$muxDestinationOptions = new MuxDestinationOptions();
$muxDestinationOptions
  ->setPlaybackPolicy(['public']);

use Shotstack\Client\Model\S3Destination;

$s3Destination = new S3Destination();
$s3Destination
  ->setProvider('s3')
  ->setOptions($s3DestinationOptions);

use Shotstack\Client\Model\S3DestinationOptions;

$s3DestinationOptions = new S3DestinationOptions();
$s3DestinationOptions
  ->setRegion('us-east-1');
  ->setBucket('my-bucket');
  ->setPrefix('my-renders');
  ->setFilename('my-file');
  ->setAcl('public-read');

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);

$url = 'https://github.com/shotstack/test-media/raw/main/captioning/scott-ko.mp4';

$response = $client->probe($url)->getResponse();

foreach ($response['metadata']->streams as $stream) {
    if ($stream->codec_type === 'video') {
        echo 'Example settings for: ' . $response['metadata']->format->filename, PHP_EOL, PHP_EOL;
        echo 'Width: ' . $stream->width . 'px', PHP_EOL;
        echo 'Height: ' . $stream->height . 'px', PHP_EOL;
        echo 'Framerate: ' . $stream->r_frame_rate . ' fps', PHP_EOL;
        echo 'Duration: ' . $stream->duration . ' secs', PHP_EOL;
    }
}

use Shotstack\Client\Api\ServeApi;
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 ServeApi(null, $config);

$renderId = '140924c6-077d-4334-a89f-94befcfc0155'; // Use a valid render ID

$response = $client->getAssetByRenderId($renderId)->getData();

foreach ($response as $asset) {
    if ($asset->getAttributes()->getStatus() === 'ready') {
        echo "Status: " . strtoupper($asset->getAttributes()->getStatus()), PHP_EOL, PHP_EOL;
        echo ">> Asset CDN URL: " . $asset->getAttributes()->getUrl(), PHP_EOL, PHP_EOL;
        echo ">> Asset ID: " . $asset->getAttributes()->getId(), PHP_EOL, PHP_EOL;
        echo ">> Render ID: " . $asset->getAttributes()->getRenderId(), PHP_EOL, PHP_EOL;
    }
}

use Shotstack\Client\Api\ServeApi;
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 ServeApi(null, $config);

$renderId = 'ed43eae3-4825-4c03-979d-f7dc47b9997c'; // Use a valid asset ID

$response = $client->getAsset($renderId)->getData();

foreach ($response as $asset) {
    if ($asset->getAttributes()->getStatus() === 'ready') {
        echo "Status: " . strtoupper($asset->getAttributes()->getStatus()), PHP_EOL, PHP_EOL;
        echo ">> Asset CDN URL: " . $asset->getAttributes()->getUrl(), PHP_EOL, PHP_EOL;
        echo ">> Asset ID: " . $asset->getAttributes()->getId(), PHP_EOL, PHP_EOL;
        echo ">> Render ID: " . $asset->getAttributes()->getRenderId(), PHP_EOL, PHP_EOL;
    }
}
bash
composer