PHP code example of renderingvideo / sdk

1. Go to this page and download the library: Download renderingvideo/sdk 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/ */

    

renderingvideo / sdk example snippets


use RenderingVideo\SDK\Client;
$client = new Client(getenv('RENDERINGVIDEO_API_KEY'));
$capabilities = $client->getCapabilities();



enderingVideo\SDK\Client;

// Initialize the client with your API key
$client = new Client('sk-your-api-key');

$client = new Client('sk-your-api-key', [
    'base_url' => 'https://renderingvideo.com',  // Optional: custom base URL
    'timeout' => 30,                             // Optional: request timeout in seconds
]);

$task = $client->video->create([
    'meta' => [
        'version' => '2.0.0',
        'width' => 1920,
        'height' => 1080,
        'fps' => 30,
        'background' => '#000000',
    ],
    'tracks' => [
        [
            'clips' => [
                [
                    'type' => 'text',
                    'text' => 'Hello World',
                    'start' => 0,
                    'duration' => 5,
                ],
            ],
        ],
    ],
], [
    'project_id' => 'proj_123',  // Optional metadata
]);

echo $task->taskId;        // abc123def456
echo $task->previewUrl;    // https://video.renderingvideo.com/v/abc123def456
echo $task->status;        // created

$taskList = $client->video->list([
    'page' => 1,
    'limit' => 20,
    'status' => 'completed',  // Filter by status
]);

foreach ($taskList->tasks as $task) {
    echo $task->taskId . ': ' . $task->status . PHP_EOL;
}

echo "Total: " . $taskList->total;
echo "Has more: " . ($taskList->hasMore() ? 'yes' : 'no');

$task = $client->video->get('abc123def456');

echo $task->status;      // completed, rendering, created, failed
echo $task->videoUrl;    // Available when completed
echo $task->costCredits; // Credits used

$renderResult = $client->video->render('abc123def456', [
    'webhook_url' => 'https://your-server.com/webhook',  // Optional
    'num_workers' => 5,                                   // Optional
]);

echo $renderResult->status;           // rendering
echo $renderResult->remainingCredits; // 970

// Blocking call that polls until completion
$task = $client->video->waitForCompletion(
    'abc123def456',
    timeout: 300,   // Max wait time in seconds
    interval: 5,    // Polling interval in seconds
);

if ($task->isCompleted()) {
    echo "Video ready: " . $task->videoUrl;
}

$result = $client->video->delete('abc123def456');

echo $result['deleted'];       // true
echo $result['remoteDeleted']; // true

// Single file
$result = $client->files->upload('/path/to/image.png');

// Multiple files
$result = $client->files->upload([
    '/path/to/image.png',
    '/path/to/video.mp4',
]);

$firstFile = $result->getFirst();
echo $firstFile->id;            // asset_001
echo $firstFile->url;           // https://storage.../images/...
echo $firstFile->getReadableSize(); // 123.45 KB

$content = file_get_contents('/path/to/file.png');
$result = $client->files->uploadFromContent($content, 'my-image.png');

$fileList = $client->files->list([
    'page' => 1,
    'limit' => 20,
    'type' => 'image',  // Filter: image, video, audio
]);

foreach ($fileList->files as $file) {
    echo $file->name . ' (' . $file->getReadableSize() . ')' . PHP_EOL;
}

$result = $client->files->delete('asset_001');

$preview = $client->preview->create([
    'meta' => [
        'version' => '2.0.0',
        'width' => 1920,
        'height' => 1080,
        'fps' => 30,
    ],
    'tracks' => [...],
]);

echo $preview->tempId;     // temp_abc123
echo $preview->previewUrl; // https://video.renderingvideo.com/t/temp_abc123
echo $preview->expiresIn;  // 7d

$config = $client->preview->get('temp_abc123');

$task = $client->preview->convert('temp_abc123', [
    'category' => 'my-category',  // Optional
]);

echo $task->taskId;

$renderResult = $client->preview->render('temp_abc123', [
    'webhook_url' => 'https://your-server.com/webhook',
    'num_workers' => 5,
]);

$result = $client->preview->delete('temp_abc123');

$balance = $client->credits->get();

echo $balance->credits;   // 985
echo $balance->currency;  // credits

// Calculate credits needed for a render
$cost = $client->credits->calculateCost(
    duration: 10,   // seconds
    width: 1920,
    height: 1080
);

echo $cost;  // 15 credits (10 seconds × 1.5 multiplier for 1080p)

$canAfford = $client->credits->canAfford(10, 1920, 1080);

if ($canAfford) {
    // Proceed with render
}

use RenderingVideo\SDK\Exceptions\ApiException;
use RenderingVideo\SDK\Exceptions\AuthenticationException;
use RenderingVideo\SDK\Exceptions\InsufficientCreditsException;
use RenderingVideo\SDK\Exceptions\NotFoundException;
use RenderingVideo\SDK\Exceptions\ValidationException;

try {
    $task = $client->video->get('invalid-id');
} catch (NotFoundException $e) {
    echo "Task not found: " . $e->getMessage();
} catch (InsufficientCreditsException $e) {
    echo "Not enough credits: " . $e->getMessage();
} catch (ValidationException $e) {
    echo "Validation error: " . $e->getMessage();
    print_r($e->getDetails());
} catch (AuthenticationException $e) {
    echo "Authentication failed: " . $e->getMessage();
} catch (ApiException $e) {
    echo "API error ({$e->getErrorCode()}): " . $e->getMessage();
}

// Your webhook endpoint
$payload = json_decode(file_get_contents('php://input'), true);

$taskId = $payload['taskId'];
$status = $payload['status'];      // 'completed' or 'failed'
$videoUrl = $payload['videoUrl'];  // Available on success
$error = $payload['error'];        // Available on failure
$timestamp = $payload['timestamp'];