fix(telegram): pass explicit duration to send_voice/send_audio for long clips - #36009
fix(telegram): pass explicit duration to send_voice/send_audio for long clips#36009liuhao1024 wants to merge 1 commit into
Conversation
…ng clips Telegram only auto-derives duration from container metadata for short recordings. For clips longer than ~4 min 50 s it delivers the message with duration 0 unless the sender passes an explicit `duration` kwarg to sendVoice / sendAudio. Add `_probe_audio_duration()` helper that tries mutagen (if installed) for accurate metadata, then falls back to a file-size estimate so the duration is always populated. The helper is called once per send_voice invocation and the result is passed to both the .ogg/.opus voice path and the .mp3/.m4a audio path. Fixes NousResearch#36005
|
@alt-glitch Thanks for the triage. Looking at #36020, I agree it has a more comprehensive probe chain (wave→mutagen→ffprobe). My PR only uses mutagen with a file-size fallback. Given that #36020 covers more cases and has a better design, I'll defer to that approach. Feel free to close this PR as superseded. |
teknium1
left a comment
There was a problem hiding this comment.
Thanks for isolating the two native Telegram send paths. The underlying issue remains present on current main: plugins/platforms/telegram/adapter.py:5860-5869 and :5886-5895 do not pass duration.
Problems
- The PR targets
gateway/platforms/telegram.py, but commit5600105478ffde29d7566b45421b100eaa29c4efmigrated the live adapter toplugins/platforms/telegram/adapter.py. This is not a clean cherry-pick. - The fallback added at
gateway/platforms/telegram.py:355in this PR derives duration from fixed guessed bitrates. That can provide an incorrect Telegram duration for variable- or differently-encoded files; the new tests validate only the estimate on synthetic byte files.
Suggested changes
- Port the behavior into
plugins/platforms/telegram/adapter.py, retaining its current chat-ID normalization and retry/DM-topic wrapper. - Send
durationonly when it is derived from actual readable media metadata; otherwise preserve prior behavior by omitting it. Add assertions for both current plugin send paths alongsidetests/gateway/test_telegram_documents.py:577.
Automated hermes-sweeper review.
| size_bytes = os.path.getsize(audio_path) | ||
| ext = os.path.splitext(audio_path)[1].lower() | ||
| # OGG/Opus voice ≈ 16 kbps; MP3/M4A ≈ 128 kbps | ||
| bytes_per_sec = 2000 if ext in {".ogg", ".opus"} else 16000 |
There was a problem hiding this comment.
This is a guessed bitrate rather than a duration probe: OGG/Opus and MP3/M4A can use different or variable bitrates, so this may make Telegram display an incorrect duration. Prefer omitting duration when metadata cannot be read rather than sending an estimate.
|
Closing as superseded: the same issue (#36005) was fixed via #65535, a salvage of #36020 by @szafranski, which was chosen for its deeper probe chain (stdlib wave → mutagen → ffprobe, run off-thread) and coverage of the standalone send_message path including the thread-retry. Your PR correctly identified the fix site — thanks for the contribution, and sorry we could only take one of the two. |
What does this PR do?
Passes an explicit
durationkwarg to Telegram'ssendVoiceandsendAudioAPI calls so that voice/audio clips longer than ~4 min 50 s display the correct duration instead of 0:00.Related Issue
Fixes #36005
Type of Change
Changes Made
gateway/platforms/telegram.py: Add_probe_audio_duration()helper that probes audio file duration via mutagen (if installed) with a file-size fallback, and pass the result asdurationto bothsend_voiceandsend_audioBot API calls.tests/gateway/test_telegram_voice_duration.py: Add regression tests — unit tests for_probe_audio_duration(missing file, OGG/MP3 file-size fallback, minimum duration) and integration tests verifyingsend_voicepassesdurationfor both voice (OGG) and audio (MP3) paths.How to Test
pytest tests/gateway/test_telegram_voice_duration.py -v— all 6 tests should pass.Checklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests passDocumentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/ACode Intelligence
gateway/platforms/telegram.py:send_voice(caller chain: GatewayRunner._send_voice_reply → TelegramAdapter.send_voice → _bot.send_voice / _bot.send_audio)plugins/platforms/discord/adapter.py:1880-1886); this PR follows the same pattern for Telegram