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.
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.
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.
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.
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
12:48
10:22
11:30
16:05
14:18
13:40