How to dub video and audio with ElevenLabs

Learn how to automate the dubbing of audio and video files into various languages using the ElevenLabs API

Introduction

Dubbing videos and audio files from one language to another can be a great way to reach a wider audience. The ElevenLabs API provides a convenient way to automatically dub media files using state-of-the-art technology. In this guide, we will walk you through how to upload a video or audio file, dub it, and download the translated video. We’ll also discuss how to directly dub a link such as a YouTube, TikTok, or Twitter video.

If you’re looking to jump straight into the action, the complete code is available on the following repos:

On the 8th of May 2024 we launched the Dubbing API for all ElevenLabs tiers

How to upload and dub a video or audio file

Requirements

Before proceeding, please ensure that you have the following:

  • An ElevenLabs account with an API key (here’s how to find your API key).
  • Python or Node.js installed on your machine

Then, install the ElevenLabs SDK as shown below

Python
$pip install elevenlabs

Install the necessary packages to manage your environmental variables:

Python
$pip install python-dotenv

Next, create a .env file in your project directory and fill it with your credentials like so:

.env
$ELEVENLABS_API_KEY=your_elevenlabs_api_key_here

Start the dubbing

First we want to send the file to the ElevenLabs dubbing service

Python
1def create_dub_from_file(
2 input_file_path: str,
3 file_format: str,
4 source_language: str,
5 target_language: str,
6) -> Optional[str]:
7 """
8 Dubs an audio or video file from one language to another and saves the output.
9
10 Args:
11 input_file_path (str): The file path of the audio or video to dub.
12 file_format (str): The file format of the input file.
13 source_language (str): The language of the input file.
14 target_language (str): The target language to dub into.
15
16 Returns:
17 Optional[str]: The file path of the dubbed file or None if operation failed.
18 """
19 if not os.path.isfile(input_file_path):
20 raise FileNotFoundError(f"The input file does not exist: {input_file_path}")
21
22 with open(input_file_path, "rb") as audio_file:
23 response = client.dubbing.dub_a_video_or_an_audio_file(
24 file=(os.path.basename(input_file_path), audio_file, file_format), # Optional file
25 target_lang=target_language, # The target language to dub the content into. Can be none if dubbing studio editor is enabled and running manual mode
26 mode="automatic", # automatic or manual.
27 source_lang=source_language, # Source language
28 num_speakers=1, # Number of speakers to use for the dubbing.
29 watermark=False, # Whether to apply watermark to the output video.
30 )
31
32 # rest of the code

Check for completion

The wait_for_dubbing_completion() function within the dubbing_utils.py file polls the API to check whether the dubbing process is complete. If completed, it proceeds to the next step; otherwise, it reports the status or failure.

Python
1def wait_for_dubbing_completion(dubbing_id: str) -> bool:
2 """
3 Waits for the dubbing process to complete by periodically checking the status.
4
5 Args:
6 dubbing_id (str): The dubbing project id.
7
8 Returns:
9 bool: True if the dubbing is successful, False otherwise.
10 """
11 MAX_ATTEMPTS = 120
12 CHECK_INTERVAL = 10 # In seconds
13
14 for _ in range(MAX_ATTEMPTS):
15 metadata = client.dubbing.get_dubbing_project_metadata(dubbing_id)
16 if metadata.status == "dubbed":
17 return True
18 elif metadata.status == "dubbing":
19 print(
20 "Dubbing in progress... Will check status again in",
21 CHECK_INTERVAL,
22 "seconds.",
23 )
24 time.sleep(CHECK_INTERVAL)
25 else:
26 print("Dubbing failed:", metadata.error_message)
27 return False
28
29 print("Dubbing timed out")
30 return False

Save the video locally

Upon completion of dubbing, the download_dubbed_file() function in dubbing_utils.py will save the dubbed file to a local directory, typically under the data/{dubbing_id}/{language_code}.mp4.

Python
1def download_dubbed_file(dubbing_id: str, language_code: str) -> str:
2 """
3 Downloads the dubbed file for a given dubbing ID and language code.
4
5 Args:
6 dubbing_id: The ID of the dubbing project.
7 language_code: The language code for the dubbing.
8
9 Returns:
10 The file path to the downloaded dubbed file.
11 """
12 dir_path = f"data/{dubbing_id}"
13 os.makedirs(dir_path, exist_ok=True)
14
15 file_path = f"{dir_path}/{language_code}.mp4"
16 with open(file_path, "wb") as file:
17 for chunk in client.dubbing.get_dubbed_file(dubbing_id, language_code):
18 file.write(chunk)
19
20 return file_path

Putting it together

We add the wait_for_dubbing_completion(waitForDubbingCompletion) function and the download_dubbed_file(downloadDubbedFile) function together to create the final function.

Python
1def create_dub_from_file(
2 input_file_path: str,
3 file_format: str,
4 source_language: str,
5 target_language: str,
6) -> Optional[str]:
7 """
8 Dubs an audio or video file from one language to another and saves the output.
9
10 Args:
11 input_file_path (str): The file path of the audio or video to dub.
12 file_format (str): The file format of the input file.
13 source_language (str): The language of the input file.
14 target_language (str): The target language to dub into.
15
16 Returns:
17 Optional[str]: The file path of the dubbed file or None if operation failed.
18 """
19 if not os.path.isfile(input_file_path):
20 raise FileNotFoundError(f"The input file does not exist: {input_file_path}")
21
22 with open(input_file_path, "rb") as audio_file:
23 response = client.dubbing.dub_a_video_or_an_audio_file(
24 file=(os.path.basename(input_file_path), audio_file, file_format),
25 target_lang=target_language,
26 mode="automatic",
27 source_lang=source_language,
28 num_speakers=1,
29 watermark=False, # reduces the characters used if enabled, only works for videos not audio
30 )
31
32 dubbing_id = response.dubbing_id
33 if wait_for_dubbing_completion(dubbing_id):
34 output_file_path = download_dubbed_file(dubbing_id, target_language)
35 return output_file_path
36 else:
37 return None

We then use the final the function as shown below.

create_a_dub_from_file.py (Python)
1if __name__ == "__main__":
2 result = create_dub_from_file(
3 "../example_speech.mp3", # Input file path
4 "audio/mpeg", # File format
5 "en", # Source language
6 "es", # Target language
7 )
8 if result:
9 print("Dubbing was successful! File saved at:", result)
10 else:
11 print("Dubbing failed or timed out.")

How to dub a video from YouTube, TikTok, Twitter or Vimeo

For dubbing web-based content, instead of uploading a file you can pass in a URL. This supports popular platforms like YouTube, TikTok, Twitter, and Vimeo.

Python
1def create_dub_from_url(
2 source_url: str,
3 source_language: str,
4 target_language: str,
5) -> Optional[str]:
6 """
7 Downloads a video from a URL, and creates a dubbed version in the target language.
8
9 Args:
10 source_url (str): The URL of the source video to dub. Can be a YouTube link, TikTok, X (Twitter) or a Vimeo link.
11 source_language (str): The language of the source video.
12 target_language (str): The target language to dub into.
13
14 Returns:
15 Optional[str]: The file path of the dubbed file or None if operation failed.
16 """
17
18 response = client.dubbing.dub_a_video_or_an_audio_file(
19 source_url=source_url, # URL of the source video/audio file.
20 target_lang=target_language, # The Target language to dub the content into. Can be none if dubbing studio editor is enabled and running manual mode
21 mode="automatic", # automatic or manual.
22 source_lang=source_language, # Source language.
23 num_speakers=1, # Number of speakers to use for the dubbing.
24 watermark=True, # Whether to apply watermark to the output video.
25 )
26
27 dubbing_id = response.dubbing_id
28 if wait_for_dubbing_completion(dubbing_id):
29 output_file_path = download_dubbed_file(dubbing_id, target_language)
30 return output_file_path
31 else:
32 return None

You can then call the function as shown below.

Python
1if __name__ == "__main__":
2 source_url = "https://www.youtube.com/watch?v=0EqSXDwTq6U" # Charlie bit my finger
3 source_language = "en"
4 target_language = "fr"
5 result = create_dub_from_url(source_url, source_language, target_language)
6 if result:
7 print("Dubbing was successful! File saved at:", result)
8 else:
9 print("Dubbing failed or timed out.")

Conclusion

With this guide and the accompanying code structure, you now have a basic setup for dubbing audio and video content using the ElevenLabs API. Whether you’re working with local files or content from URLs, you can create multilingual versions of your media to cater to diverse audiences.

Remember to always follow the best practices when dealing with API keys and sensitive data, and consult the ElevenLabs API documentation for more advanced features and options. Happy dubbing!

For additional information on dubbing capabilities, translation services, and available languages, please refer to the ElevenLabs API documentation.

Should you encounter any issues or have questions, our GitHub Issues page is open for your queries and feedback.

List of supported languages for dubbing

NoLanguage NameLanguage Code
1Englishen
2Hindihi
3Portuguesept
4Chinesezh
5Spanishes
6Frenchfr
7Germande
8Japaneseja
9Arabicar
10Russianru
11Koreanko
12Indonesianid
13Italianit
14Dutchnl
15Turkishtr
16Polishpl
17Swedishsv
18Filipinofil
19Malayms
20Romanianro
21Ukrainianuk
22Greekel
23Czechcs
24Danishda
25Finnishfi
26Bulgarianbg
27Croatianhr
28Slovaksk
29Tamilta