mixing.dubbing¶
Text-to-speech dubbing: re-voice or translate a video from its SRT.
The transcript side of mixing turns speech into SRT (ElevenLabs
Scribe); this subpackage closes the loop — turning SRT back into speech with
an ElevenLabs voice and muxing it over the video, optionally translating the
SRT first to produce a foreign-language dub.
Building blocks:
text_to_speech()/synthesize_to_file()— ElevenLabs TTS.
list_voices()/find_voice()— discover account voices.
parse_srt()/dump_srt()/translate_srt()— SRT I/O and LLM-backed translation.
dub_video_from_srt()— the end-to-end pipeline.
The HTTP clients use stdlib only (no requests / elevenlabs SDK), so
this adds no required deps. Needs ffmpeg on PATH and an ElevenLabs API key
(env var ELEVENLABS_API_KEY or explicit api_key=). Translation needs
either the aix package (pip install 'mixing[llm]') or a custom
translate_fn.
Lazy by design. Like the top-level mixing facade, this subpackage
uses PEP 562 __getattr__ so that the TTS / SRT building blocks
(text_to_speech(), list_voices(), parse_srt(),
Cue, …) import with no moviepy dependency. Only
dub_video_from_srt() — the end-to-end pipeline that muxes audio over a
video — pulls moviepy, and only when it is first accessed.
Quick start:
>>> from mixing.dubbing import dub_video_from_srt, translate_srt
>>> dub_video_from_srt("promo.mp4", "promo.srt", voice_id="...", output="promo.en.mp4")
>>> fr_srt = translate_srt(open("promo.srt").read(), "French")
Functions
Default on-disk cache for synthesized audio. |
|
|
Synthesize |
|
Synthesize |
|
List the voices available on the account. |
|
Return the first voice whose name or labels contain |
|
Search ElevenLabs' public shared voice library (not just the account). |
|
Add a shared-library voice to the account so it can be synthesized. |
|
Parse SRT text into a list of |
|
Serialize cues back to SRT text, renumbering from 1. |
|
Parse an |
|
Translate the cue text of an SRT to |
|
LLM-backed translator (segment-count preserving) using |
Classes
|
One SRT subtitle cue. |
Add a shared-library voice to the account so it can be synthesized.
Idempotent-ish: if the voice (by name) is already on the account, returns its existing id instead of erroring on a duplicate add.
- Parameters:
- Return type:
- Returns:
The account-local
voice_idto pass totext_to_speech().
- mixing.dubbing.default_cache_dir()[source]¶
Default on-disk cache for synthesized audio.
Honors
$MIXING_TTS_CACHE_DIR, then$XDG_CACHE_HOME, then~/.cache/. Final segment is alwaysmixing/tts.- Return type:
- mixing.dubbing.default_translate_fn(texts, target_language, source_language=None, *, model=None)[source]¶
LLM-backed translator (segment-count preserving) using
aix.chat.Translates all segments in a single call, returning a JSON array so the one-to-one mapping with the input cues is preserved. Falls back to a clear error if
aixis not importable — pass your owntranslate_fnthen.
- mixing.dubbing.dump_srt(cues)[source]¶
Serialize cues back to SRT text, renumbering from 1.
- Return type:
- mixing.dubbing.find_voice(query, *, api_key=None, voices=None)[source]¶
Return the first voice whose name or labels contain
query(case-insensitive).Convenience for picking a voice by a human name (e.g.
"Brian") or a label keyword (e.g."french") without memorizing voice ids.
- mixing.dubbing.list_voices(*, api_key=None, timeout=60.0)[source]¶
List the voices available on the account.
- Parameters:
- Return type:
- Returns:
A list of voice dicts. Each has at least
voice_id,name,category, and alabelsdict (accent,description,age,gender,use_case) describing the voice.- Raises:
RuntimeError – No API key supplied and
ELEVENLABS_API_KEYunset.
- mixing.dubbing.parse_srt(srt_text)[source]¶
Parse SRT text into a list of
Cueobjects.Tolerant of blank-line spacing variations and of either
,or.as the millisecond separator. Cues without a valid time line are skipped.
Search ElevenLabs’ public shared voice library (not just the account).
Useful for finding a native voice in a target language (e.g. a French advertising voice) that is not yet on the account. Add the chosen voice with
add_shared_voice(), then synthesize with the returned id.- Parameters:
- Return type:
- Returns:
A list of shared-voice dicts. Each has
voice_idandpublic_owner_id(both needed byadd_shared_voice()), plusname,language,accent,gender,use_case,description.
- mixing.dubbing.srt_time_to_seconds(timestamp)[source]¶
Parse an
HH:MM:SS,mmm(or.mmm) SRT timestamp to seconds.>>> srt_time_to_seconds('00:43:12,187') 2592.187 >>> srt_time_to_seconds('00:00:01.500') 1.5- Return type:
- mixing.dubbing.synthesize_to_file(text, voice_id, path, **kwargs)[source]¶
Synthesize
textand write the audio topath.Thin convenience wrapper over
text_to_speech(). Extra keyword arguments are forwarded unchanged.- Return type:
- Returns:
The path written.
- mixing.dubbing.text_to_speech(text, voice_id, *, api_key=None, model_id='eleven_multilingual_v2', output_format='mp3_44100_128', voice_settings=None, language_code=None, timeout=600.0, cache=True, refresh=False, return_cache_status=False)[source]¶
Synthesize
textto speech with ElevenLabs and return audio bytes.- Parameters:
text (
str) – The text to speak.voice_id (
str) – ElevenLabs voice id (seelist_voices()).api_key (
str|None) – API key. Falls back to env varELEVENLABS_API_KEY.model_id (
str) – TTS model id. Defaults toeleven_multilingual_v2.output_format (
str) – ElevenLabsoutput_formatquery value, e.g.mp3_44100_128(default),mp3_44100_192(needs a paid tier), orpcm_44100.voice_settings (
Optional[Mapping[str,Any]]) – Override the default voice settings (stability, similarity_boost, style, use_speaker_boost). Merged overDFLT_VOICE_SETTINGS.language_code (
str|None) – Optional ISO-639-1 hint (e.g."fr"). Only some models honor it; ignored by others.timeout (
float) – Total request timeout in seconds.cache (
Union[bool,str,Path]) –True(default) → usedefault_cache_dir().False→ no cache. A path → use that directory. The cache key is a SHA-256 of the text plus every parameter that affects the audio.refresh (
bool) – WhenTrueandcacheis enabled, force a re-call and overwrite the cached entry.return_cache_status (
bool) – WhenTrue, return(audio, was_cached)instead of justaudio.was_cachedisTrueiff the bytes came from the on-disk cache (no ElevenLabs call = no spend), so a caller can attribute real cost. DefaultFalsekeeps thebytesreturn.
- Return type:
- Returns:
Raw audio bytes in
output_format(MP3 by default) — or, whenreturn_cache_statusisTrue, a(audio, was_cached)tuple.- Raises:
RuntimeError – No API key supplied and
ELEVENLABS_API_KEYunset.HTTPError – Request failed (4xx/5xx).
- mixing.dubbing.translate_srt(srt, target_language, *, source_language=None, translate_fn=None)[source]¶
Translate the cue text of an SRT to
target_language, keeping timings.- Parameters:
srt (
Union[str,Sequence[Cue]]) – SRT text or a list ofCueobjects.target_language (
str) – Human-readable target language (e.g."French").source_language (
str|None) – Optional source language hint.translate_fn (
Optional[Callable[[Sequence[str],str,str|None],list[str]]]) – A callable(texts, target_language, source_language) -> list[str]returning one translation per input text, in order. Defaults todefault_translate_fn()(LLM-backed).
- Return type:
- Returns:
Translated SRT text with the original cue timings.
- Raises:
ValueError – The translator returned a different number of segments than it was given.
Modules
SRT translation for dubbing — built on the canonical |
|
ElevenLabs text-to-speech (TTS) HTTP client. |