Skip to main content

generateVideo()

Generate a video from text prompt with optional image URL.
<?php
require_once(__DIR__ . '/vendor/autoload.php');

use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\VideoGenerationApi;
use OpenAPI\Client\Model\VideoDiffusionRequest;

// Configure API client
$config = Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', getenv('LEGNEXT_API_KEY'));
$config->setHost('https://api.legnext.ai');

$api = new VideoGenerationApi(null, $config);

try {
    // Generate from text only
    $request = new VideoDiffusionRequest();
    $request->setPrompt('a flowing river through mountains');
    $request->setVideoType(1);  // 0=480p, 1=720p
    $request->setCallback('https://your-domain.com/webhook');

    $response = $api->generateVideo($request);

    echo "Job ID: " . $response->getJobId() . "\n";
    echo "Status: " . $response->getStatus() . "\n";

} catch (\OpenAPI\Client\ApiException $e) {
    echo "Error: " . $e->getMessage() . "\n";
}
?>
Generate from Image URL in Prompt:
<?php
// Generate from image URL in prompt
$request = new VideoDiffusionRequest();
$request->setPrompt('https://example.com/image.png a flowing river through mountains');
$request->setVideoType(1);
$request->setCallback('https://your-domain.com/webhook');

$response = $api->generateVideo($request);

echo "Job ID: " . $response->getJobId() . "\n";
?>
Generate from Image Animation (using job_id and image_no):
<?php
// Animate an existing image from previous generation
$request = new VideoDiffusionRequest();
$request->setJobId('previous-generation-job-id');  // Reference to previous image generation
$request->setImageNo(0);  // Image index to animate
$request->setPrompt('continue the flowing motion smoothly');
$request->setVideoType(1);
$request->setCallback('https://your-domain.com/webhook');

$response = $api->generateVideo($request);

echo "Job ID: " . $response->getJobId() . "\n";
?>
Parameters: Text-only generation:
  • prompt (string): Video prompt (1-8192 characters)
  • video_type (int, optional): Quality (0=480p, 1=720p)
  • callback (string, optional): Webhook URL
Image URL in prompt:
  • prompt (string): “[image_url] prompt text” format (1-8192 characters)
  • video_type (int, optional): Quality (0=480p, 1=720p)
  • callback (string, optional): Webhook URL
Image animation:
  • job_id (string): Previous image generation job ID
  • image_no (int): Image index to animate (0-3)
  • prompt (string, optional): Animation prompt (1-8192 characters)
  • video_type (int, optional): Quality (0=480p, 1=720p)
  • callback (string, optional): Webhook URL
Returns: Response with job_id and status

extendVideo()

Extend an existing video.
<?php
use OpenAPI\Client\Model\ExtendVideoRequest;

$request = new ExtendVideoRequest();
$request->setJobId('original-video-job-id');
$request->setVideoNo(0);
$request->setPrompt('continue with dramatic lighting');
$request->setCallback('https://your-domain.com/webhook');

$response = $api->extendVideo($request);

echo "Extension Job ID: " . $response->getJobId() . "\n";
?>
Parameters:
  • job_id (string): Original video task ID
  • video_no (int): Video index (0-3)
  • prompt (string, optional): Extension prompt (1-8192 characters)
  • callback (string, optional): Webhook URL
Returns: Response with job_id and status

upscaleVideo()

Upscale video to higher resolution.
<?php
use OpenAPI\Client\Model\VideoUpscaleRequest;

$request = new VideoUpscaleRequest();
$request->setJobId('original-video-job-id');
$request->setVideoNo(0);
$request->setCallback('https://your-domain.com/webhook');

$response = $api->upscaleVideo($request);

echo "Upscale Job ID: " . $response->getJobId() . "\n";
?>
Parameters:
  • job_id (string): Original video task ID
  • video_no (int): Video index (0-3)
  • callback (string, optional): Webhook URL
Returns: Response with job_id and status

Complete Example

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use OpenAPI\Client\Configuration;
use OpenAPI\Client\Api\VideoGenerationApi;
use OpenAPI\Client\Model\VideoDiffusionRequest;
use OpenAPI\Client\Model\ExtendVideoRequest;
use OpenAPI\Client\Model\VideoUpscaleRequest;

// Configure API client
$config = Configuration::getDefaultConfiguration();
$config->setApiKey('x-api-key', getenv('LEGNEXT_API_KEY'));
$config->setHost('https://api.legnext.ai');

$api = new VideoGenerationApi(null, $config);

try {
    // Step 1: Generate video
    $request = new VideoDiffusionRequest();
    $request->setPrompt('a beautiful sunset over the ocean');
    $request->setVideoType(1);

    $response = $api->generateVideo($request);
    $jobId = $response->getJobId();

    echo "Video generation started: {$jobId}\n";

    // Step 2: Wait for completion (see Task Management docs)
    // ...

    // Step 3: Extend the video
    $extendRequest = new ExtendVideoRequest();
    $extendRequest->setJobId($jobId);
    $extendRequest->setVideoNo(0);
    $extendRequest->setPrompt('continue with dramatic clouds');

    $extendResponse = $api->extendVideo($extendRequest);

    echo "Video extension started: " . $extendResponse->getJobId() . "\n";

    // Step 4: Upscale the extended video
    $upscaleRequest = new VideoUpscaleRequest();
    $upscaleRequest->setJobId($extendResponse->getJobId());
    $upscaleRequest->setVideoNo(0);

    $upscaleResponse = $api->upscaleVideo($upscaleRequest);

    echo "Video upscale started: " . $upscaleResponse->getJobId() . "\n";

} catch (\OpenAPI\Client\ApiException $e) {
    echo "HTTP Status: " . $e->getCode() . "\n";
    echo "Error Message: " . $e->getMessage() . "\n";
    echo "Response Body: " . $e->getResponseBody() . "\n";
}
?>

Error Handling

<?php
try {
    $response = $api->generateVideo($request);

} catch (\OpenAPI\Client\ApiException $e) {
    $statusCode = $e->getCode();
    $errorBody = $e->getResponseBody();

    switch ($statusCode) {
        case 400:
            echo "Validation error: Check request parameters\n";
            break;
        case 401:
            echo "Authentication error: Invalid API key\n";
            break;
        case 404:
            echo "Resource not found\n";
            break;
        case 429:
            echo "Rate limit exceeded. Please retry later.\n";
            break;
        case 500:
        case 502:
        case 503:
            echo "Server error. Please retry.\n";
            break;
        default:
            echo "API error ({$statusCode}): {$errorBody}\n";
    }
}
?>

Retry Logic

<?php
function generateVideoWithRetry($api, $request, $maxRetries = 3) {
    $retries = 0;

    while ($retries < $maxRetries) {
        try {
            return $api->generateVideo($request);

        } catch (\OpenAPI\Client\ApiException $e) {
            if ($e->getCode() === 429) {
                // Rate limited - wait and retry
                $retries++;
                sleep(5);
                continue;
            } elseif ($e->getCode() >= 500) {
                // Server error - exponential backoff
                $retries++;
                $delay = pow(2, $retries);
                sleep($delay);
                if ($retries >= $maxRetries) {
                    throw $e;
                }
                continue;
            } else {
                // Other errors - don't retry
                throw $e;
            }
        }
    }

    throw new Exception("Max retries exceeded");
}

// Usage
$request = new VideoDiffusionRequest();
$request->setPrompt('a flowing river');
$request->setVideoType(1);

try {
    $response = generateVideoWithRetry($api, $request, 3);
    echo "Job ID: " . $response->getJobId() . "\n";
} catch (Exception $e) {
    echo "Failed after retries: " . $e->getMessage() . "\n";
}
?>

Asynchronous Processing with Promises

For modern PHP with async support (using ReactPHP or Amp):
<?php
// Example using ReactPHP (requires: composer require react/http)
use React\EventLoop\Factory;
use React\Promise\Promise;

$loop = Factory::create();

function generateVideoAsync($api, $request) {
    return new Promise(function ($resolve, $reject) use ($api, $request) {
        try {
            $response = $api->generateVideo($request);
            $resolve($response);
        } catch (\OpenAPI\Client\ApiException $e) {
            $reject($e);
        }
    });
}

// Usage
$promise = generateVideoAsync($api, $request);

$promise->then(
    function ($response) {
        echo "Job ID: " . $response->getJobId() . "\n";
    },
    function (\OpenAPI\Client\ApiException $e) {
        echo "Error: " . $e->getMessage() . "\n";
    }
);

$loop->run();
?>

Processing Multiple Videos

<?php
function processMultipleVideos($api, $prompts) {
    $results = [];

    foreach ($prompts as $prompt) {
        try {
            $request = new VideoDiffusionRequest();
            $request->setPrompt($prompt);
            $request->setVideoType(1);

            $response = $api->generateVideo($request);

            $results[] = [
                'prompt' => $prompt,
                'job_id' => $response->getJobId(),
                'status' => 'started'
            ];

        } catch (\OpenAPI\Client\ApiException $e) {
            $results[] = [
                'prompt' => $prompt,
                'error' => $e->getMessage(),
                'status' => 'failed'
            ];
        }
    }

    return $results;
}

// Usage
$prompts = ['sunset', 'mountains', 'ocean'];
$results = processMultipleVideos($api, $prompts);

foreach ($results as $result) {
    if (isset($result['error'])) {
        echo "Error for '{$result['prompt']}': {$result['error']}\n";
    } else {
        echo "Video '{$result['prompt']}': {$result['job_id']}\n";
    }
}
?>

Next Steps