import requests
import json
import time
BASE = "https://apis.viggle.ai"
headers = {"Authorization": "Bearer YOUR_API_KEY"}
# Map each person in the scene to a character
character_mapping = {
"person_a1b2c3d4": "char_alice",
"person_e5f6g7h8": "char_bob",
}
# 1. Start multi-character render
resp = requests.post(
f"{BASE}/api/render",
headers=headers,
data={
"scene_id": "scene_770e8400e29b41d4",
"character_mapping": json.dumps(character_mapping),
"background_mode": "original",
},
).json()
if "detail" in resp:
raise Exception(f"Error {resp['detail']['error_code']}: {resp['detail']['message']}")
job_id = resp["job_id"]
print(f"Job created: {job_id}")
# 2. Poll until complete (no auth required)
while True:
status = requests.get(f"{BASE}/api/render/{job_id}").json()
if status["status"] == "complete":
break
elif status["status"] == "failed":
raise Exception(f"Error {status.get('error_code')}: {status.get('error')}")
time.sleep(5)
# 3. Download (auth required)
video = requests.get(
f"{BASE}/api/render/{job_id}/download",
headers=headers,
allow_redirects=True,
stream=True,
)
with open("output.mp4", "wb") as f:
for chunk in video.iter_content(chunk_size=8192):
f.write(chunk)
print("Saved output.mp4")