Audio & MusicAdvanced

How to Batch-Narrate Many Video Scripts with the ElevenLabs API

Loop over a folder of script files and generate one voiceover MP3 per file with a single Python script.

10 minAdvanced

If you publish a lot of short videos, generating each voiceover by hand is slow. With the API you can drop every script into a folder and produce all the MP3s in one run. This guide builds a small Python batch script that reads text files and writes matching audio files.

What you need

  • An ElevenLabs API key in an environment variable
  • Python 3 with the requests library installed
  • A folder of plain-text scripts, one file per video
  • A chosen voice ID

Step 1: Organize your scripts

Put each video's script in its own .txt file inside a scripts folder. The output file will reuse the script's name, so name them clearly, for example video-01.txt produces video-01.mp3.

zsh - project layout
$ls scripts/
video-01.txt video-02.txt video-03.txt
$pip install requests
$

Step 2: Write the batch script

The script lists every .txt file, sends each one to the Text to Speech endpoint, and saves the audio with a matching name. It skips files that already have an MP3 so reruns do not waste quota.

batch_tts.py
import os, glob, requests

VOICE_ID = "21m00Tcm4TlvDq8ikWAM"
API_KEY = os.environ["ELEVENLABS_API_KEY"]
URL = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"

os.makedirs("audio", exist_ok=True)

for path in sorted(glob.glob("scripts/*.txt")):
    name = os.path.splitext(os.path.basename(path))[0]
    out = f"audio/{name}.mp3"
    if os.path.exists(out):
        print(f"skip {name} (already done)")
        continue

    with open(path, encoding="utf-8") as f:
        text = f.read().strip()

    resp = requests.post(
        URL,
        headers={"xi-api-key": API_KEY, "Content-Type": "application/json"},
        json={"text": text, "model_id": "eleven_multilingual_v2"},
    )
    resp.raise_for_status()

    with open(out, "wb") as f:
        f.write(resp.content)
    print(f"done {name}")

Step 3: Run it

Run the script from your project root. It prints progress per file and leaves the finished MP3s in the audio folder. If it stops on an error, the already-done files are kept so you can rerun safely.

zsh - run the batch
$python batch_tts.py
done video-01
done video-02
done video-03
$ls audio/
video-01.mp3 video-02.mp3 video-03.mp3
$
Mind your character quota
Every file consumes characters from your monthly allowance. Check your script lengths before a big run, and start with a couple of files to confirm the voice and settings before processing the whole folder.
ElevenLabs - Usage dashboard
Usage this month
----------------------------------------
Characters used: 42,310 / 100,000
[##########----------------------] 42%
Resets on: the 1st
Watch the character meter so a big batch does not exhaust your plan.

Result: a single command turns a folder of scripts into a folder of finished voiceovers, ready to pair with your video edits.

Watch related tutorials

Tags
#api#batch#automation#python