Mastering Media Dubbing

Anis MarrouchiAI Bot
By Anis Marrouchi & AI Bot ·

Loading the Text to Speech Audio Player...

Dubbing videos and audio files from one language to another can significantly expand your audience reach. The ElevenLabs API offers a seamless way to automate this process, allowing you to dub media files with cutting-edge technology. In this guide, we'll explore how to upload a video or audio file, dub it, and download the translated version. Additionally, we'll cover how to dub content directly from platforms like YouTube, TikTok, or Twitter.

Getting Started

Before you begin, ensure you have the following prerequisites:

  • An ElevenLabs account with an API key.
  • Python or Node.js installed on your machine.

Install the ElevenLabs SDK using the following command:

pip install elevenlabs

Also, manage your environmental variables by installing:

pip install python-dotenv

Create a .env file in your project directory and add your credentials:

ELEVENLABS_API_KEY=your_elevenlabs_api_key_here

Dubbing a File

To dub a file, send it to the ElevenLabs dubbing service. Here's a Python function to get you started:

def create_dub_from_file(input_file_path: str, file_format: str, source_language: str, target_language: str) -> Optional[str]:
    """
    Dubs an audio or video file from one language to another and saves the output.
    """
    if not os.path.isfile(input_file_path):
        raise FileNotFoundError(f"The input file does not exist: {input_file_path}")
    with open(input_file_path, "rb") as audio_file:
        response = client.dubbing.dub_a_video_or_an_audio_file(
            file=(os.path.basename(input_file_path), audio_file, file_format),
            target_lang=target_language,
            mode="automatic",
            source_lang=source_language,
            num_speakers=1,
            watermark=False,
        )
    dubbing_id = response.dubbing_id
    if wait_for_dubbing_completion(dubbing_id):
        output_file_path = download_dubbed_file(dubbing_id, target_language)
        return output_file_path
    else:
        return None

Monitoring Progress

Use the wait_for_dubbing_completion() function to check the status of your dubbing process. This function will poll the API and notify you once the dubbing is complete.

Saving the Dubbed File

Once dubbing is complete, save the dubbed file locally using the download_dubbed_file() function. The file will be stored in a directory structured as data/{dubbing_id}/{language_code}.mp4.

Dubbing from URLs

For web-based content, you can dub directly from a URL. This supports platforms like YouTube, TikTok, Twitter, and Vimeo. Here's how you can do it:

def create_dub_from_url(source_url: str, source_language: str, target_language: str) -> Optional[str]:
    """
    Downloads a video from a URL, and creates a dubbed version in the target language.
    """
    response = client.dubbing.dub_a_video_or_an_audio_file(
        source_url=source_url,
        target_lang=target_language,
        mode="automatic",
        source_lang=source_language,
        num_speakers=1,
        watermark=True,
    )
    dubbing_id = response.dubbing_id
    if wait_for_dubbing_completion(dubbing_id):
        output_file_path = download_dubbed_file(dubbing_id, target_language)
        return output_file_path
    else:
        return None

Conclusion

With this guide, you now have a foundational setup for dubbing audio and video content using the ElevenLabs API. Whether working with local files or online content, you can create multilingual versions of your media to cater to diverse audiences. Always follow best practices when handling API keys and sensitive data, and refer to the ElevenLabs API documentation for more advanced features.

For more information on dubbing capabilities and available languages, visit the ElevenLabs API documentation.


Reference: ElevenLabs API Documentation by ElevenLabs.


Want to read more tutorials? Check out our latest tutorial on 2 Laravel 11 Basics: Routing.

Discuss Your Project with Us

We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.

Let's find the best solutions for your needs.