Discussions
API failing
15 days ago by John Danahy
I am using this code to call the API but it fails every time. I have tried many avatar_ids and voice_ids, either from the web interface or from listing the available ids and using those. I had some initial success but later it just failed. I cannot see the API calls in the API usage either:
generate_url = "https://api.heygen.com/v2/video/generate"
def generate_and_download_videos(podcast_script_data):
video_segments = []
for index, (speaker, text) in enumerate(podcast_script_data):
avatar_id, voice_id = get_avatar_params(speaker)
print(avatar_id, voice_id)
payload = {
"video_inputs": [
{
"character": {
"type": "avatar",
"avatar_id": "nik_expressive_20240910",
"avatar_style": "normal"
},
"voice": {
"type": "text",
"voice_id": "0009aabefe3a4553bc581d837b6268cb",
"input_text": "This is a test please respect that",
"speed": 1.0
}
}
],
"test": False,
"aspect_ratio": "16:9"
}
response = requests.post(generate_url, json=payload, headers=HEADERS)
print(f"Response Status Code: {response.status_code}")
print(f"Response Text: {response.text}")
response_data = response.json()
print('response', response_data)
if response.status_code != 200 or "data" not in response_data:
print(f"Error generating video for segment {index + 1}: {response_data}")
continue
video_id = response_data["data"]["video_id"]
print(f"Video generation started for segment {index + 1}. Video ID: {video_id}")
retrieve_url = f"https://api.heygen.com/v1/video_status.get?video_id={video_id}"
status = "waiting"
elapsed_time = 0
max_wait_time = 600
wait_interval = 5
while status in ["waiting", "pending", "processing"]:
response = requests.get(retrieve_url, headers=HEADERS)
print('response to data request: ', response)
video_status_data = response.json()
if response.status_code != 200 or "data" not in video_status_data:
print(f"Error checking status for segment {index + 1}: {response.text}")
break
status = video_status_data["data"].get("status", "unknown")
print(f"Segment {index + 1} status: {status}")
if status == "completed":
video_url = video_status_data["data"].get("video_url")
if video_url:
print(f"Segment {index + 1} completed. Download URL: {video_url}")
video_response = requests.get(video_url)
if video_response.status_code == 200:
segment_filename = f"segment_{index + 1}.mp4"
with open(segment_filename, "wb") as video_file:
video_file.write(video_response.content)
print(f"Segment {index + 1} downloaded as {segment_filename}.")
video_segments.append(segment_filename)
else:
print(f"Error downloading segment {index + 1}: {video_response.status_code}")
break
elif status == "failed":
print(f"Video generation failed for segment {index + 1}.")
break
time.sleep(wait_interval)
elapsed_time += wait_interval
if elapsed_time >= max_wait_time:
print(f"Video generation timed out for segment {index + 1}.")
break
print('video_segments', video_segments)
return video_segments