fix(tts): improve Opus encoding quality for voice messages

Salvaged from PR #18861 (@shellybotmoyer). Switch the central Opus
transcoders from CBR 64k (-vbr off) to VBR 48k with -application voip and
-compression_level 10. The old CBR settings produced lower-quality speech
audio and occasionally failed to trigger Telegram's native voice-bubble
rendering. Applied to _ffmpeg_transcode_to_opus (the single transcoder
behind _convert_to_opus and _repair_ogg_container), the Gemini WAV→OGG
path, and the Matrix adapter boundary transcoder added in this branch.

Fixes #18818
This commit is contained in:
shellybotmoyer 2026-07-28 09:31:43 -07:00 committed by Teknium
parent 33313e88f8
commit 88f7fe3b1f
2 changed files with 11 additions and 3 deletions

View file

@ -249,7 +249,13 @@ def _matrix_transcode_voice_to_ogg(path: str) -> Optional[str]:
"-ac",
"1",
"-b:a",
"64k",
"48k",
"-vbr",
"on",
"-application",
"voip",
"-compression_level",
"10",
ogg_path,
],
capture_output=True,

View file

@ -1083,7 +1083,8 @@ def _ffmpeg_transcode_to_opus(input_path: str, ogg_path: str) -> Optional[str]:
try:
result = subprocess.run(
["ffmpeg", "-i", input_path, "-acodec", "libopus",
"-ac", "1", "-b:a", "64k", "-vbr", "off", "-f", "ogg",
"-ac", "1", "-b:a", "48k", "-vbr", "on",
"-application", "voip", "-compression_level", "10", "-f", "ogg",
work_path, "-y"],
capture_output=True, timeout=30,
stdin=subprocess.DEVNULL,
@ -2202,7 +2203,8 @@ def _generate_gemini_tts(text: str, output_path: str, tts_config: Dict[str, Any]
cmd = [
ffmpeg, "-i", wav_path,
"-acodec", "libopus", "-ac", "1",
"-b:a", "64k", "-vbr", "off",
"-b:a", "48k", "-vbr", "on",
"-application", "voip", "-compression_level", "10",
"-y", "-loglevel", "error",
output_path,
]