Overview
Thetranslate.py module provides AI-powered voice dubbing using the ElevenLabs Dubbing API. It supports 30+ languages with automatic voice cloning and synchronization.
Key Functions
translate_video
video_path(str): Path to input video fileoutput_path(str): Path to save dubbed videotarget_language(str): Target language code (e.g.,"es","fr","de")api_key(str): ElevenLabs API keysource_language(str, optional): Source language code (auto-detected ifNone)max_wait_seconds(int): Timeout in seconds (default: 600 = 10 minutes)poll_interval(int): Status check interval in seconds (default: 5)
str: Path to the dubbed video file
Exception: If dubbing fails or times out
- Creates dubbing project via
create_dubbing_project() - Polls status every 5 seconds using
get_dubbing_status() - Downloads result when status is
"dubbed" - Raises exception if status is
"failed"or timeout exceeded
create_dubbing_project
video_path(str): Path to video filetarget_language(str): Target language codeapi_key(str): ElevenLabs API keysource_language(str, optional): Source language (auto-detected if omitted)
dictwith keys:dubbing_id(str): Unique project ID for status pollingexpected_duration_sec(int): Estimated processing time
get_dubbing_status
dubbing_id(str): Project ID fromcreate_dubbing_project()api_key(str): ElevenLabs API key
dictwith keys:status(str):"dubbing"(in progress),"dubbed"(complete), or"failed"error(str, optional): Error message if status is"failed"
download_dubbed_video
dubbing_id(str): Project IDtarget_language(str): Target language codeoutput_path(str): Path to save dubbed videoapi_key(str): ElevenLabs API key
str: Path to downloaded file (same asoutput_path)
httpx.stream() with 8KB chunks for memory efficiency.
get_supported_languages
dict: Mapping of language codes to full names
Supported Languages
TheSUPPORTED_LANGUAGES constant includes 30+ languages:
| Code | Language | Code | Language | Code | Language |
|---|---|---|---|---|---|
en | English | es | Spanish | fr | French |
de | German | it | Italian | pt | Portuguese |
pl | Polish | hi | Hindi | ja | Japanese |
ko | Korean | zh | Chinese | ar | Arabic |
ru | Russian | tr | Turkish | nl | Dutch |
sv | Swedish | id | Indonesian | fil | Filipino |
ms | Malay | vi | Vietnamese | th | Thai |
uk | Ukrainian | el | Greek | cs | Czech |
fi | Finnish | ro | Romanian | da | Danish |
bg | Bulgarian | hr | Croatian | sk | Slovak |
ta | Tamil |
API Configuration
Base URL
Endpoints
- Create Project:
POST /dubbing - Get Status:
GET /dubbing/{dubbing_id} - Download:
GET /dubbing/{dubbing_id}/audio/{target_language}
Timeouts
- Create Project: 300 seconds (5 minutes)
- Get Status: 30 seconds
- Download: 120 seconds (2 minutes, streaming)
Example Workflows
Simple Translation
With Source Language
Batch Translation
Manual Workflow (Non-blocking)
Error Handling
Dependencies
httpx: HTTP client with streaming support (timeout: 30-300s)typing: Type hints (Optional)time: Polling delaysos: File path operations
Notes
- Pricing: ElevenLabs charges per minute of dubbed audio (check their pricing page)
- Quality: Automatic voice cloning attempts to match original speaker characteristics
- Sync: Lip-sync is NOT perfect (audio dubbing only, not visual deepfake)
- Watermark: Set to
falsein this implementation (requires paid plan) - Speakers: Auto-detects number of speakers (
num_speakers: "0")