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 an audio 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': ('audio.mp3', open('audio.mp3', 'rb'), 'audio/mpeg')
} # Replace this with the path to your audio file

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

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

# Check the response
if audio_upload_response.status_code != 200:
    print(f'Failed to upload file. Status code: {audio_upload_response.status_code}')
    print('Response:', audio_upload_response.text)
    sys.exit(1)
print('Upload successful', audio_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": audio_upload_response.json()['resource_id'],
        "type": "audio"
    },
}

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)