> ## Documentation Index
> Fetch the complete documentation index at: https://docs.legnext.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Video Generation

> Video generation API reference for Ruby SDK

## generate\_video()

Generate a video from text prompt with optional image URL.

```ruby theme={null}
require 'legnext_sdk'

# Configure API client
Legnext.configure do |config|
  config.api_key['x-api-key'] = ENV['LEGNEXT_API_KEY']
  config.host = 'api.legnext.ai'
  config.base_path = '/api'
end

api = Legnext::VideoGenerationApi.new

begin
  # Mode 1: Generate from text only
  request = Legnext::VideoDiffusionRequest.new(
    prompt: 'a flowing river through mountains',
    video_type: 1,  # 0=480p, 1=720p
    callback: 'https://your-domain.com/webhook'
  )

  response = api.generate_video(request)

  puts "Job ID: #{response.job_id}"
  puts "Status: #{response.status}"

rescue Legnext::ApiError => e
  puts "Error: #{e.message}"
end
```

**Mode 2: Generate from image URL in prompt:**

```ruby theme={null}
# Generate video from image included in prompt
request = Legnext::VideoDiffusionRequest.new(
  prompt: 'https://example.com/image.png a flowing river through mountains',
  video_type: 1,
  callback: 'https://your-domain.com/webhook'
)

response = api.generate_video(request)

puts "Job ID: #{response.job_id}"
```

**Mode 3: Animate existing image:**

```ruby theme={null}
# Animate an image from a previous generation job
request = Legnext::VideoDiffusionRequest.new(
  job_id: 'previous-image-job-id',
  image_no: 0,
  prompt: 'flowing movement through the landscape',
  video_type: 1,
  callback: 'https://your-domain.com/webhook'
)

response = api.generate_video(request)

puts "Job ID: #{response.job_id}"
```

**Parameters:**

* `prompt` (String): Video prompt or "\[image\_url] prompt text" (1-8192 characters)
* `job_id` (String, optional): Original image job ID (for image animation mode)
* `image_no` (Integer, optional): Image index (0-3) (for image animation mode)
* `video_type` (Integer, optional): Quality (0=480p, 1=720p)
* `callback` (String, optional): Webhook URL

**Returns:** Response with `job_id` and `status`

***

## extend\_video()

Extend an existing video.

```ruby theme={null}
request = Legnext::ExtendVideoRequest.new(
  job_id: 'original-video-job-id',
  video_no: 0,
  prompt: 'continue with dramatic lighting',
  callback: 'https://your-domain.com/webhook'
)

response = api.extend_video(request)

puts "Extension Job ID: #{response.job_id}"
```

**Parameters:**

* `job_id` (String): Original video task ID
* `video_no` (Integer): Video index (0-3)
* `prompt` (String, optional): Extension prompt (1-8192 characters)
* `callback` (String, optional): Webhook URL

**Returns:** Response with `job_id` and `status`

***

## upscale\_video()

Upscale video to higher resolution.

```ruby theme={null}
request = Legnext::VideoUpscaleRequest.new(
  job_id: 'original-video-job-id',
  video_no: 0,
  callback: 'https://your-domain.com/webhook'
)

response = api.upscale_video(request)

puts "Upscale Job ID: #{response.job_id}"
```

**Parameters:**

* `job_id` (String): Original video task ID
* `video_no` (Integer): Video index (0-3)
* `callback` (String, optional): Webhook URL

**Returns:** Response with `job_id` and `status`

***

## Complete Example

```ruby theme={null}
require 'legnext_sdk'

# Configure API client
Legnext.configure do |config|
  config.api_key['x-api-key'] = ENV['LEGNEXT_API_KEY']
  config.host = 'api.legnext.ai'
  config.base_path = '/api'
end

api = Legnext::VideoGenerationApi.new

begin
  # Step 1: Generate video
  request = Legnext::VideoDiffusionRequest.new(
    prompt: 'a beautiful sunset over the ocean',
    video_type: 1,
    callback: 'https://your-domain.com/webhook'
  )

  response = api.generate_video(request)
  job_id = response.job_id

  puts "Video generation started: #{job_id}"

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

  # Step 3: Extend the video
  extend_request = Legnext::ExtendVideoRequest.new(
    job_id: job_id,
    video_no: 0,
    prompt: 'continue with dramatic clouds',
    callback: 'https://your-domain.com/webhook'
  )

  extend_response = api.extend_video(extend_request)

  puts "Video extension started: #{extend_response.job_id}"

  # Step 4: Upscale the extended video
  upscale_request = Legnext::VideoUpscaleRequest.new(
    job_id: extend_response.job_id,
    video_no: 0,
    callback: 'https://your-domain.com/webhook'
  )

  upscale_response = api.upscale_video(upscale_request)

  puts "Video upscale started: #{upscale_response.job_id}"

rescue Legnext::ApiError => e
  puts "HTTP Status: #{e.code}"
  puts "Error Message: #{e.message}"
  puts "Response Body: #{e.response_body}"
end
```

***

## Error Handling

```ruby theme={null}
begin
  response = api.generate_video(request)

rescue Legnext::ApiError => e
  case e.code
  when 400
    puts "Validation error: Check request parameters"
  when 401
    puts "Authentication error: Invalid API key"
  when 404
    puts "Resource not found"
  when 429
    puts "Rate limit exceeded. Please retry later."
  when 500, 502, 503
    puts "Server error. Please retry."
  else
    puts "API error (#{e.code}): #{e.message}"
  end
  puts "Response body: #{e.response_body}"
end
```

***

## Retry Logic

```ruby theme={null}
def generate_video_with_retry(api, request, max_retries = 3)
  retries = 0

  begin
    api.generate_video(request)
  rescue Legnext::ApiError => e
    if e.code == 429 && retries < max_retries
      # Rate limited - wait and retry
      retries += 1
      sleep 5
      retry
    elsif e.code >= 500 && retries < max_retries
      # Server error - exponential backoff
      retries += 1
      sleep(2**retries)
      retry
    else
      # Other errors - don't retry
      raise
    end
  end
end

# Usage
api = Legnext::VideoGenerationApi.new
request = Legnext::VideoDiffusionRequest.new(
  prompt: 'a flowing river',
  video_type: 1,
  callback: 'https://your-domain.com/webhook'
)

response = generate_video_with_retry(api, request)
puts "Job ID: #{response.job_id}"
```

***

## Asynchronous Processing with Threads

```ruby theme={null}
require 'thread'

def process_multiple_videos(api, prompts)
  threads = []
  results = Queue.new

  prompts.each do |prompt|
    threads << Thread.new do
      begin
        request = Legnext::VideoDiffusionRequest.new(
          prompt: prompt,
          video_type: 1,
          callback: 'https://your-domain.com/webhook'
        )

        response = api.generate_video(request)
        results << { prompt: prompt, job_id: response.job_id }

      rescue Legnext::ApiError => e
        results << { prompt: prompt, error: e.message }
      end
    end
  end

  # Wait for all threads
  threads.each(&:join)

  # Collect results
  all_results = []
  all_results << results.pop until results.empty?
  all_results
end

# Usage
api = Legnext::VideoGenerationApi.new
prompts = ['sunset', 'mountains', 'ocean']

results = process_multiple_videos(api, prompts)
results.each do |result|
  if result[:error]
    puts "Error for '#{result[:prompt]}': #{result[:error]}"
  else
    puts "Video '#{result[:prompt]}': #{result[:job_id]}"
  end
end
```

***

## Next Steps

* [Image Generation](/sdk/ruby/image-generation)
* [Task Management](/sdk/ruby/task-management)
* [Quick Start](/sdk/ruby/quickstart)
