From 88f7fe3b1f1f06fbf8320f5853ace46178b62a9e Mon Sep 17 00:00:00 2001 From: shellybotmoyer Date: Tue, 28 Jul 2026 09:31:43 -0700 Subject: [PATCH] fix(tts): improve Opus encoding quality for voice messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- plugins/platforms/matrix/adapter.py | 8 +++++++- tools/tts_tool.py | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/plugins/platforms/matrix/adapter.py b/plugins/platforms/matrix/adapter.py index d2de4061a80..c6b2dcdd615 100644 --- a/plugins/platforms/matrix/adapter.py +++ b/plugins/platforms/matrix/adapter.py @@ -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, diff --git a/tools/tts_tool.py b/tools/tts_tool.py index 947fe60ed74..5455b0a5eff 100644 --- a/tools/tts_tool.py +++ b/tools/tts_tool.py @@ -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, ]