Discussions

Ask a Question
Back to All

How can I download the generated video?

I have created an example code to generate a talk and download the resulting video. I have attached the resources I used in the order of implementation. Could you please review and let me know where I might be going wrong?

Thank you in advance for your assistance!

Code:

import requests
import base64
import time

# Replace with your actual D-ID API key
API_KEY = 'my_api_key'

# Split the API key into username and password
username, password = API_KEY.split(':')

url = "https://api.d-id.com/talks"

payload = {
    "script": {
        "type": "text",
        "subtitles": "false",
        "provider": {
            "type": "elevenlabs",
            "voice_id": "21m00Tcm4TlvDq8ikWAM"
        },
        "input": "This is an AI prompt that works using python"
    },
    "config": {
        "fluent": "false",
        "pad_audio": "0.0"
    },
    "source_url": "https://img.freepik.com/free-photo/handsome-bearded-guy-posing-against-white-wall_273609-20597.jpg?t=st=1714185052~exp=1714185652~hmac=383a21b3bb15d52279d7caa61d64112ec02ff373e4036e14c3c65288ab05935f"
}

# Create the authorization header using Basic Authentication
auth_str = f"{username}:{password}"
b64_auth_str = base64.b64encode(auth_str.encode()).decode()

headers = {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": f"Basic {b64_auth_str}"
}

# Make the request to create the video
response = requests.post(url, json=payload, headers=headers)
response_data = response.json()
print(response_data)

# Extract the video ID from the response
video_id = response_data.get('id')

if video_id:
    # Check the status of the video creation process
    status_url = f"https://api.d-id.com/talks/{video_id}"

    while True:
        status_response = requests.get(status_url, headers=headers)
        status_data = status_response.json()

        if status_data['status'] == 'completed':
            video_url = status_data.get('result_url')
            print(f'Video is ready. You can download it from: {video_url}')
            break
        elif status_data['status'] == 'failed':
            print('Video creation failed.')
            break
        else:
            print('Video is still processing. Checking again in 10 seconds...')
            time.sleep(10)
else:
    print('Failed to create the video.')

Output:

{'id': 'tlk_zL-tdfa-PAXXh6-XacY1Y', 'created_at': '2024-07-15T10:43:59.724Z', 'created_by': 'google-oauth2|115088040357497543329', 'status': 'created', 'object': 'talk'}
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...
Video is still processing. Checking again in 10 seconds...

Taking forever to process ....