Prompt
How do I batch-generate audio files from text at scale?
Latest observation
To batch-generate audio from text at scale, you typically want a text-to-speech (TTS) pipeline that can ingest many text items, synthesize audio in parallel, and save/track the outputs reliably.
Recommended approach
1) Prepare your input data
Put your text in a structured format such as:
- CSV
- JSONL
- a database table
Each record should include:
idor filename keytext- optional metadata like
voice,language,speed,format
Example JSONL:
{"id":"001","text":"Hello world.","voice":"alloy"}
{"id":"002","text":"Welcome to the demo.","voice":"alloy"}
2) Choose a TTS provider
Options include:
- OpenAI TTS / speech generation APIs
- Amazon Polly
- Google Cloud Text-to-Speech
- Azure Speech
- ElevenLabs
- open-source models like Piper, Coqui TTS, or XTTS
If you need high quality and easy scaling, a managed API is usually simplest.
3) Chunk long text
Most TTS services have input length limits or perform better on shorter segments.
Break text into:
- sentences
- paragraphs
- chunks of ~1–3k characters, depending on the service
Then synthesize each chunk and concatenate audio if needed.
4) Run jobs in parallel
For scale, use:
- async I/O for API calls
- a worker pool for local synthesis
- a queue system like:
- Celery
- RQ
- Sidekiq
- SQS + Lambda / ECS
- Cloud Tasks
Important:
- respect API rate limits
- add retries with exponential backoff
- make jobs idempotent
5) Store outputs
Save files to:
- local disk for small runs
- object storage for large runs:
- S3
- GCS
- Azure Blob
Use a consistent naming scheme:
audio/001.mp3
audio/002.mp3
6) Track job status
For large batches, keep a manifest/table with:
- input id
- text hash
- status: pending/running/done/failed
- output path
- error message
- retry count
This prevents duplicates and helps resume failed jobs.
7) Normalize and post-process
You may want to:
- normalize loudness
- trim silence
- convert formats (WAV → MP3/AAC)
- add metadata tags
Tools:
ffmpegsoxpydub
Example Python batch script
Here’s a simple pattern using an API-based TTS workflow:
import asyncio
import aiofiles
import os
from pathlib import Path
OUTPUT_DIR = Path("outputs")
OUTPUT_DIR.mkdir(exist_ok=True)
texts = [
{"id": "001", "text": "Hello world."},
{"id": "002", "text": "This is a batch TTS example."},
]
async def synthesize(text):
# Replace this with your TTS API call
await asyncio.sleep(0.5)
return b"FAKE_AUDIO_BYTES"
async def process_item(item):
out_path = OUTPUT_DIR / f"{item['id']}.mp3"
if out_path.exists():
return f"skipped {item['id']}"
audio = await synthesize(item["text"])
async with aiofiles.open(out_path, "wb") as f:
await f.write(audio)
return f"done {item['id']}"
async def main():
sem = asyncio.Semaphore(10) # limit concurrency
async def guarded(item):
async with sem:
return await process_item(item)
results = await asyncio.gather(*(guarded(item) for item in texts))
print("\n".join(results))
asyncio.run(main())
Scaling tips
- Use concurrency limits to avoid throttling
- Cache by text hash so repeated text isn’t re-synthesized
- Save partial results frequently
- Separate job submission from job execution
- Use observability:
- logs
- metrics
- dead-letter queues
- failure alerts
When to use local vs API
Use an API if:
- you need fast setup
- quality matters
- you want elastic scale
- you don’t want to manage models/GPU infrastructure
Use local/open-source if:
- privacy is critical
- you need very high volume at low marginal cost
- you can manage GPUs/servers
- you want custom voices or offline operation
If you want, I can give you:
- a production-ready Python batch TTS script,
- a cloud architecture for millions of files, or
- an example for a specific provider like OpenAI, Polly, or ElevenLabs.