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

This is the video you want to translate. If the file is larger than 5MB, follow the Upload Large Resources guide and note the resource_id. If not, you can upload it directly.

Step 2: Submit a translation request

Python
# Create a translation request body
import sys
import requests
import time

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

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

# Here goes the code for resource upload

body = {
    "video_resource_id": response.json()["resource_id"], # Resource ID from the previous step
    "source_language": "en", # The language of the video
    "target_language": "fr", # The language to translate to
}

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

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

Step 3: Poll for the status and download the video

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

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

    print("Downloaded video to verbalia_translation.mp4")