speech_engine¶
speech_engine
¶
Speech engine for subtitle generation (STT) and voice generation (TTS).
Speech-to-Text backends:
- faster-whisper: local/offline transcription via CTranslate2.
- Google Cloud Speech-to-Text v1 REST API (longrunningrecognize).
Audio is converted to FLAC via FFmpeg before sending to the Google API.
Text-to-Speech backends: - Edge TTS: free, async synthesis via Microsoft Edge online service. - ElevenLabs TTS: high-quality neural voice synthesis via REST API. - Google Cloud Text-to-Speech v1 REST API. Text is split into API-sized chunks, each synthesized to a temp file, then concatenated via FFmpeg. Memory-safe by design — audio data is written to disk immediately, never accumulated in memory.
check_ffmpeg_available
¶
_get_speech_language_code
¶
Maps a language label to a BCP-47 code for Speech-to-Text.
| PARAMETER | DESCRIPTION |
|---|---|
src_lang
|
Language label (e.g. "Vietnamese"). Empty for auto-detect.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
BCP-47 language code (e.g. "vi-VN"), or empty string for auto. |
Source code in src/core/speech_engine.py
_extract_audio_to_flac
¶
Converts an audio/video file to FLAC format using FFmpeg.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the source audio/video file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Path
|
Path to the temporary FLAC file. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
If FFmpeg is not available or conversion fails. |
Source code in src/core/speech_engine.py
_call_long_running_recognize
¶
Sends a longrunningrecognize request and returns the operation name.
| PARAMETER | DESCRIPTION |
|---|---|
audio_content_b64
|
Base64-encoded audio content.
TYPE:
|
language_code
|
BCP-47 language code (e.g. "en-US").
TYPE:
|
api_key
|
Google Cloud API key.
TYPE:
|
model
|
Google Cloud STT model name.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Operation name string for polling. |
Source code in src/core/speech_engine.py
_poll_operation
¶
Polls a long-running operation until completion.
| PARAMETER | DESCRIPTION |
|---|---|
operation_name
|
The operation name to poll.
TYPE:
|
api_key
|
Google Cloud API key.
TYPE:
|
is_cancelled
|
Optional callback to check for cancellation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
The completed operation response dict. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the operation fails. |
Source code in src/core/speech_engine.py
_parse_results_to_srt
¶
Converts Speech-to-Text results to SRT subtitle format.
Groups words into segments of reasonable length/duration.
| PARAMETER | DESCRIPTION |
|---|---|
results
|
The
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
SRT-formatted subtitle string. |
Source code in src/core/speech_engine.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 | |
_parse_duration
¶
Parses a Google API duration string (e.g. '1.500s') to seconds.
Source code in src/core/speech_engine.py
_format_srt_time
¶
Formats seconds to SRT timestamp (HH:MM:SS,mmm).
Source code in src/core/speech_engine.py
_transcribe_whisper
¶
Transcribes audio using faster-whisper (local, offline).
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the audio/video file.
TYPE:
|
src_lang
|
Source language label. Empty for auto-detect.
TYPE:
|
model_size
|
Whisper model size (tiny, base, small, medium, large).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
SRT-formatted subtitle string. |
Source code in src/core/speech_engine.py
_transcribe_google_cloud
¶
Transcribes audio using Google Cloud Speech-to-Text API.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the audio/video file.
TYPE:
|
src_lang
|
Source language label. Empty for auto-detect.
TYPE:
|
model
|
Google Cloud STT model name.
TYPE:
|
is_cancelled
|
Optional callback for cancellation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
SRT-formatted subtitle string. |
Source code in src/core/speech_engine.py
transcribe_audio
¶
transcribe_audio(
file_path,
src_lang="",
*,
stt_method="",
model_size="base",
google_model="default",
is_cancelled=None,
)
Transcribes an audio/video file to SRT subtitle format.
Dispatches to Whisper (local) or Google Cloud STT based on
stt_method.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the audio/video file.
TYPE:
|
src_lang
|
Source language label (e.g. "Vietnamese"). Empty for auto.
TYPE:
|
stt_method
|
STT engine ("Whisper" or "Google Cloud").
TYPE:
|
model_size
|
Whisper model size (only for Whisper).
TYPE:
|
google_model
|
Google Cloud STT model (only for Google Cloud).
TYPE:
|
is_cancelled
|
Optional callback for cancellation.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
SRT-formatted subtitle string. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
On API errors or missing credentials. |
RuntimeError
|
On FFmpeg errors. |
Source code in src/core/speech_engine.py
_get_tts_language_code
¶
Maps a language label to a Google Cloud TTS language code.
| PARAMETER | DESCRIPTION |
|---|---|
lang_label
|
Language label (e.g. "Vietnamese").
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
TTS language code (e.g. "vi-VN"). Falls back to "en-US". |
Source code in src/core/speech_engine.py
extract_subtitle_text
¶
Extracts plain text from subtitle file content.
| PARAMETER | DESCRIPTION |
|---|---|
content
|
Raw subtitle file content (SRT, VTT, ASS, SSA).
TYPE:
|
suffix
|
File extension for format detection.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Concatenated text lines without timestamps or metadata. |
Source code in src/core/speech_engine.py
_split_text_for_tts
¶
Splits text into chunks that fit within the TTS API byte limit.
Splits at sentence boundaries first, then word boundaries if needed.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Input text to split.
TYPE:
|
max_bytes
|
Maximum bytes per chunk.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of text chunks, each within the byte limit. |
Source code in src/core/speech_engine.py
_split_long_sentence
¶
Splits a long sentence by words, appending complete chunks.
| PARAMETER | DESCRIPTION |
|---|---|
sentence
|
The sentence to split.
TYPE:
|
max_bytes
|
Maximum bytes per chunk.
TYPE:
|
chunks
|
List to append complete chunks to.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The remaining incomplete chunk. |
Source code in src/core/speech_engine.py
_split_oversized_word
¶
Splits a single oversized "word" at codepoint boundaries.
Used only as the last-resort fallback inside
:func:_split_long_sentence when a whitespace-bounded token's
UTF-8 encoding already exceeds max_bytes (typical for CJK
runs with no inner whitespace). Walks character-by-character so
each emitted chunk stays under the limit AND every chunk
boundary lands on a codepoint boundary — slicing by byte index
would corrupt multi-byte sequences. Appends complete chunks to
chunks and returns nothing (no remaining partial: the entire
oversized word is consumed).
Source code in src/core/speech_engine.py
_get_mp3_duration
¶
Returns the duration of an MP3 file in seconds.
Uses ffprobe for accurate measurement. Falls back to file-size estimation if ffprobe is unavailable.
| PARAMETER | DESCRIPTION |
|---|---|
file_path
|
Path to the MP3 file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Duration in seconds. |
Source code in src/core/speech_engine.py
_generate_silence
¶
Generates a silent MP3 file of the specified duration.
| PARAMETER | DESCRIPTION |
|---|---|
duration
|
Duration in seconds.
TYPE:
|
output_path
|
Path to write the silent MP3 file.
TYPE:
|
Source code in src/core/speech_engine.py
_speed_up_audio
¶
Speeds up an audio file using FFmpeg's atempo filter.
FFmpeg atempo only accepts values in [0.5, 100.0], so factors above 2.0 are chained (e.g. 3.0 → atempo=2.0,atempo=1.5).
| PARAMETER | DESCRIPTION |
|---|---|
input_path
|
Path to the source audio file.
TYPE:
|
output_path
|
Path to write the sped-up audio.
TYPE:
|
factor
|
Speed-up factor (e.g. 1.5 = 50% faster). Clamped to
TYPE:
|
Source code in src/core/speech_engine.py
_parse_srt_timestamp
¶
Parses an SRT/VTT timestamp string to seconds.
Supports both SRT (comma) and VTT (dot) formats:
HH:MM:SS,mmm or HH:MM:SS.mmm or MM:SS,mmm.
| PARAMETER | DESCRIPTION |
|---|---|
ts
|
Timestamp string.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
Time in seconds. |
Source code in src/core/speech_engine.py
_get_edge_voice
¶
Maps a language label + gender to an Edge TTS voice name.
| PARAMETER | DESCRIPTION |
|---|---|
lang_label
|
Language label (e.g. "Vietnamese").
TYPE:
|
gender
|
"MALE" or "FEMALE".
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Edge TTS voice name (e.g. "vi-VN-HoaiMyNeural"). |
Source code in src/core/speech_engine.py
_synthesize_chunk_edge
¶
Synthesizes a single text chunk using Edge TTS with retry.
Retries on NoAudioReceived (transient service error) with
exponential backoff.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
voice
|
Edge TTS voice name (e.g. "vi-VN-HoaiMyNeural").
TYPE:
|
output_path
|
Path to write the MP3 audio file.
TYPE:
|
max_retries
|
Maximum number of retry attempts.
TYPE:
|
base_delay
|
Initial delay in seconds between retries.
TYPE:
|
Source code in src/core/speech_engine.py
get_elevenlabs_voices_for_gender
¶
Returns the curated ElevenLabs voices matching gender.
Falls back to the female list for unknown values so the UI never renders an empty combo.
| PARAMETER | DESCRIPTION |
|---|---|
gender
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, str]
|
Tuple of |
...
|
data: |
Source code in src/core/speech_engine.py
_get_elevenlabs_voice
¶
Returns the gender-default ElevenLabs voice ID.
get_elevenlabs_default_voice_id
¶
Public accessor for the gender-default ElevenLabs voice ID.
Used by the settings UI to pick the recommended voice when the
user hasn't saved a preference — the catalogue itself is now
sorted strictly A→Z so the default can no longer be inferred
from position 0 of ELEVENLABS_VOICES_BY_GENDER.
Source code in src/core/speech_engine.py
_synthesize_chunk_elevenlabs
¶
_synthesize_chunk_elevenlabs(
text, api_key, output_path, voice_id="", model_id="", *, gender="FEMALE"
)
Synthesizes a single text chunk using ElevenLabs TTS.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
api_key
|
ElevenLabs API key.
TYPE:
|
output_path
|
Path to write the MP3 audio file.
TYPE:
|
voice_id
|
ElevenLabs voice ID. When empty, falls back to the gender-default voice (Rachel for FEMALE, George for MALE).
TYPE:
|
model_id
|
ElevenLabs model ID. Uses
TYPE:
|
gender
|
Used as the fallback selector when
TYPE:
|
Source code in src/core/speech_engine.py
get_gemini_voices_for_gender
¶
Returns the curated Gemini voices matching gender.
Falls back to the female list for unknown values so the UI never renders an empty combo.
| PARAMETER | DESCRIPTION |
|---|---|
gender
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[str, ...]
|
Tuple of voice names from :data: |
Source code in src/core/speech_engine.py
_get_gemini_voice
¶
Returns the default Gemini prebuilt voice name for the given gender.
| PARAMETER | DESCRIPTION |
|---|---|
gender
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
A voice name from the Gemini prebuilt catalogue. |
Source code in src/core/speech_engine.py
get_gemini_default_voice
¶
Public accessor for the gender-default Gemini voice name.
Used by the settings UI to pick the recommended voice when the
user hasn't saved a preference — the catalogue itself is now
sorted strictly A→Z so the default can no longer be inferred
from position 0 of GEMINI_TTS_VOICES_BY_GENDER.
Source code in src/core/speech_engine.py
_synthesize_chunk_gemini
¶
Synthesizes a single text chunk using Gemini TTS.
Posts a JSON request asking for responseModalities=["AUDIO"],
receives base64-encoded raw PCM (s16le, 24 kHz mono), then pipes
those bytes through ffmpeg to land at output_path in the
requested audio_format. Per-chunk ffmpeg is fine — chunks are
short enough (~5 KB text → ~1 s audio) that the encode cost is
negligible compared to the network round-trip.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
api_key
|
Gemini API key.
TYPE:
|
output_path
|
Path to write the audio file.
TYPE:
|
voice_name
|
Gemini prebuilt voice name (e.g.
TYPE:
|
audio_format
|
Output container —
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
With a tagged code ( |
RuntimeError
|
|
Source code in src/core/speech_engine.py
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 | |
get_piper_voice_for
¶
Resolves (target_lang, gender) to a Piper voice ID.
Resolution order:
- The voice mapped to the requested gender for target_lang.
- The voice mapped to the OTHER gender for target_lang — some languages (Italian, Dutch, Chinese (Simplified) → female; Portuguese → male) only ship a single voice in the rhasspy catalogue, so a request for the missing gender falls back to the available one rather than dropping the user to en_US.
- Empty string when the language isn't in the curated catalogue at all — the caller is expected to interpret this as "no Piper coverage" and route to a different backend (Edge TTS) rather than synthesise English audio for, say, a Japanese translation. Returning a usable-but-wrong-language voice (the old en_US-amy fallback) silently mismatched audio to text for any user translating into a Piper-unsupported language.
| PARAMETER | DESCRIPTION |
|---|---|
target_lang
|
Language label from
TYPE:
|
gender
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Piper voice ID like |
str
|
no Piper voice exists for target_lang. |
Source code in src/core/speech_engine.py
piper_voice_paths
¶
Returns the on-disk (model_path, config_path) for a voice ID.
Both files may not exist yet — call :func:is_piper_voice_installed
first or :func:download_piper_voice to fetch them.
Source code in src/core/speech_engine.py
is_piper_voice_installed
¶
Returns True iff both the ONNX model and its JSON config are on disk.
installed_piper_languages
¶
Returns the English language labels with at least one installed voice.
Walks :data:PIPER_VOICES_BY_GENDER_AND_LANGUAGE and tests each
voice with :func:is_piper_voice_installed; a language is counted
as installed when ANY of its catalogued voices (across genders) has
its .onnx + .onnx.json pair on disk.
Used by the settings UI to show a Tesseract-style banner above the Piper picker — "Piper TTS: 3 language(s) installed" — without the user having to click through every voice row to check.
Source code in src/core/speech_engine.py
_piper_voice_url
¶
Builds the HuggingFace URL for a voice file.
Voice IDs follow <lang>_<region>-<voice>-<quality>. The HF
layout is {lang}/{lang_region}/{voice}/{quality}/{voice_id}.{suffix}.
Source code in src/core/speech_engine.py
download_piper_voice
¶
Downloads the ONNX + JSON pair for a Piper voice from HuggingFace.
Atomic-rename pattern: each file is fetched to a .partial path
first, then renamed on completion. A failed/cancelled download
leaves no half-written file masquerading as a complete voice.
| PARAMETER | DESCRIPTION |
|---|---|
voice_id
|
Voice ID like
TYPE:
|
on_progress
|
Optional callback
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple[Path, Path]
|
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
Source code in src/core/speech_engine.py
_download_to_file
¶
Streams url into dest, atomic-rename via .partial suffix.
Source code in src/core/speech_engine.py
_load_piper_voice
¶
Returns a cached :class:PiperVoice for voice_id.
Raises ValueError("PIPER_VOICE_NOT_INSTALLED") when the voice
files aren't on disk — the UI is expected to gate synthesis on
:func:is_piper_voice_installed and prompt the user to download,
rather than silently auto-fetching mid-translation.
Source code in src/core/speech_engine.py
_synthesize_chunk_piper
¶
Synthesizes text with Piper and writes to output_path.
Piper's native output format is WAV (16-bit PCM, 22.05 kHz mono). We synthesize to a temp WAV, then transcode to the requested format via FFmpeg — same pattern as the Gemini path.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
output_path
|
Final audio file path. Container format is controlled by audio_format.
TYPE:
|
voice_id
|
Voice ID like
TYPE:
|
audio_format
|
Output container —
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
|
RuntimeError
|
|
Source code in src/core/speech_engine.py
1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 | |
_synthesize_chunk
¶
_synthesize_chunk(
text,
language_code,
voice_gender,
api_key,
output_path,
speaking_rate=1.0,
audio_format=".mp3",
voice_name="",
)
Synthesizes a single text chunk to audio and writes to disk.
Memory-safe: decoded audio is written immediately, not accumulated.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
language_code
|
TTS language code (e.g. "vi-VN").
TYPE:
|
voice_gender
|
Voice gender ("MALE" or "FEMALE").
TYPE:
|
api_key
|
Google Cloud API key.
TYPE:
|
output_path
|
Path to write the audio file.
TYPE:
|
speaking_rate
|
Speech speed multiplier (0.25–4.0).
TYPE:
|
audio_format
|
Output format (".mp3" or ".wav").
TYPE:
|
voice_name
|
Optional specific voice name (e.g. "en-US-Chirp3-HD-Charon").
When set, the server ignores
TYPE:
|
Source code in src/core/speech_engine.py
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 | |
_concatenate_mp3_files
¶
Concatenates multiple MP3 files using FFmpeg.
Memory-safe: FFmpeg processes files on disk.
| PARAMETER | DESCRIPTION |
|---|---|
audio_files
|
List of MP3 file paths to concatenate.
TYPE:
|
output_path
|
Path for the concatenated output.
TYPE:
|
Source code in src/core/speech_engine.py
synthesize_speech
¶
synthesize_speech(
text,
target_lang="",
voice_gender="FEMALE",
output_path="",
*,
tts_method="",
audio_format=".mp3",
is_cancelled=None,
on_progress=None,
)
Synthesizes speech from text using the configured TTS engine.
Dispatches to Google Cloud TTS or Edge TTS based on tts_method.
| PARAMETER | DESCRIPTION |
|---|---|
text
|
Text to synthesize.
TYPE:
|
target_lang
|
Target language label (e.g. "Vietnamese").
TYPE:
|
voice_gender
|
Voice gender ("MALE" or "FEMALE").
TYPE:
|
output_path
|
Path for the output audio file.
TYPE:
|
tts_method
|
TTS engine ("Edge TTS" or "Google Cloud").
TYPE:
|
audio_format
|
Output format (".mp3" or ".wav").
TYPE:
|
is_cancelled
|
Optional callback to check for cancellation.
TYPE:
|
on_progress
|
Optional callback (current_chunk, total_chunks).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The output file path. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
On API errors, empty text, or missing credentials. |
RuntimeError
|
On FFmpeg errors. |
Source code in src/core/speech_engine.py
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 | |
synthesize_timed_speech
¶
synthesize_timed_speech(
entries,
target_lang="",
voice_gender="FEMALE",
output_path="",
*,
tts_method="",
audio_format=".mp3",
is_cancelled=None,
on_progress=None,
)
Synthesizes timed speech from subtitle entries.
Dispatches to Google Cloud TTS or Edge TTS based on tts_method.
Each entry is synthesized individually and placed at its original
timestamp. Silence is inserted for gaps.
| PARAMETER | DESCRIPTION |
|---|---|
entries
|
List of SubtitleEntry objects with start/end timestamps.
TYPE:
|
target_lang
|
Target language label (e.g. "Vietnamese").
TYPE:
|
voice_gender
|
Voice gender ("MALE" or "FEMALE").
TYPE:
|
output_path
|
Path for the output audio file.
TYPE:
|
tts_method
|
TTS engine ("Edge TTS" or "Google Cloud").
TYPE:
|
audio_format
|
Output format (".mp3" or ".wav").
TYPE:
|
is_cancelled
|
Optional callback to check for cancellation.
TYPE:
|
on_progress
|
Optional callback (current_entry, total_entries).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The output file path. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
On API errors, empty entries, or missing credentials. |
RuntimeError
|
On FFmpeg errors. |
Source code in src/core/speech_engine.py
2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 | |
mix_audio_into_video
¶
Replaces a video's audio track with a new audio file.
The video stream is copied (not re-encoded), so this is fast.
| PARAMETER | DESCRIPTION |
|---|---|
video_path
|
Path to the original video file.
TYPE:
|
audio_path
|
Path to the new audio file (MP3/WAV).
TYPE:
|
output_path
|
Path for the output video file.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
The output file path. |
| RAISES | DESCRIPTION |
|---|---|
RuntimeError
|
On FFmpeg errors. |