Prerequisites

  • You have an API key. If not, you can get one by contacting us.
  • You have python installed on your machine along with the requests library

Step 1: Create a video resource

Once uploaded, you can reuse the same resource for multiple generation requests.

Python
import sys
import requests
import time

base_url = "https://api.verbalia.net"

headers = {"X-API-Key": "<api_key>"}

files = {
  'file': ('ref_video.mp4', open('ref_video.mp4', 'rb'), 'video/mp4')
} # Replace this with the path to your reference video file

data = {
    'resource_name': 'workshop-facecam', # Descriptive name
    'resource_type': 'video' # One of the 5 types
}

# Make the POST request
video_upload_response = requests.post(
  base_url + "/resources/upload",
  files=files,
  params=data,
  headers=headers
)

# Check the response
if video_upload_response.status_code != 200:
    print(f'Failed to upload file. Status code: {video_upload_response.status_code}')
    print('Response:', video_upload_response.text)
    sys.exit(1)
print('Upload successful', video_upload_response.json())
# Contains resource_id

Step 2: Submit a generation request

Python
# Create a generation request body
body = {
    "avatar": {
        "name": "adam", # One of the default avatars
    },
    "script": {
        "resource_id": video_upload_response.json()['resource_id'],
        "type": "video"
    },
}

generation_response = requests.post(
  base_url + "/generate-video",
  json=body,
  headers=headers
)

# Check the response and status code
if generation_response.status_code != 200:
    print(f'Failed to generate video. Status code: {generation_response.status_code}')
    print('Response:', generation_response.text)
    sys.exit(1)
print('Generation successful.', generation_response.json())
# Contains request_id

Step 3: Poll for the status and download the video

Space out polling requests by at least 30s to avoid getting rate limited.

Python
while True:
    time.sleep(60) # Poll every 60 seconds
    response = requests.get(
        base_url + "/status/" + generation_response.json()["request_id"],
        headers=headers
    )
    print(response.json()) # Status and video urls once completed
    if response.json()["status"] in ["completed", "failed"]:
        break

if response.json()["status"] == "completed":
    video_response = requests.get(response.json()["video_url"])
    with open(f"verbalia_video.mp4", "wb") as f:
        f.write(video_response.content)