perf: enhance AMR audio quality and simplify opus logic#8153
Open
WenqiOfficial wants to merge 2 commits into
Open
perf: enhance AMR audio quality and simplify opus logic#8153WenqiOfficial wants to merge 2 commits into
WenqiOfficial wants to merge 2 commits into
Conversation
- Route Opus conversion directly through the underlying convert_audio_format. - Remove redundant FFmpeg processing chains to improve code reusability.
- Enhance AMR audio quality via built-in FFmpeg filters.
Contributor
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- The new
convert_audio_to_opusimplementation no longer short‑circuits when the input is already an.opusfile, so existing opus audio will now be re-encoded; consider preserving the previous fast-path to avoid unnecessary quality loss and CPU usage. - The AMR
-affilter chain is embedded as a long inline string; extracting it into a named constant (or helper) would make the intent clearer and the parameters easier to tweak or reuse. - For AMR conversion you now specify both
-ar 8000and-af ... aresample=8000, which is redundant; consider keeping only one to avoid confusion about which stage is responsible for resampling.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new `convert_audio_to_opus` implementation no longer short‑circuits when the input is already an `.opus` file, so existing opus audio will now be re-encoded; consider preserving the previous fast-path to avoid unnecessary quality loss and CPU usage.
- The AMR `-af` filter chain is embedded as a long inline string; extracting it into a named constant (or helper) would make the intent clearer and the parameters easier to tweak or reuse.
- For AMR conversion you now specify both `-ar 8000` and `-af ... aresample=8000`, which is redundant; consider keeping only one to avoid confusion about which stage is responsible for resampling.
## Individual Comments
### Comment 1
<location path="astrbot/core/utils/media_utils.py" line_range="70-72" />
<code_context>
+async def convert_audio_to_opus(audio_path: str, output_path: str | None = None) -> str:
</code_context>
<issue_to_address>
**issue (bug_risk):** The new implementation changes behavior for already-opus inputs and may introduce unnecessary re-encoding.
Previously, this function returned the original path and skipped work when `audio_path` already ended with `.opus`. Now, delegating to `convert_audio_format` forces ffmpeg to run even for existing opus files, which may re-encode (wasting CPU and degrading quality) and return a different path. If any callers assume idempotence for `.opus` inputs or that the returned path equals the input, this changes their behavior. To preserve compatibility while using the new helper, keep the `.opus` short-circuit and only call `convert_audio_format` for non-opus inputs.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Contributor
There was a problem hiding this comment.
Code Review
This pull request refactors the convert_audio_to_opus function to utilize a centralized convert_audio_format utility and introduces a series of audio filters—including highpass, lowpass, equalizer, and normalization—to the AMR conversion process. The review feedback suggests restoring the detailed docstrings for the refactored function to maintain documentation quality and IDE support.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
主要实现Issue #7283 提及的AMR语音听觉体验优化,利用贝叶斯算法获取最佳参数。
以及小小的代码优化。
Modifications / 改动点
增加-af参数,优化AMR语音
精简opus格式转码逻辑,复用已有代码
This is NOT a breaking change. / 这不是一个破坏性变更。
Screenshots or Test Results / 运行截图或测试结果
无参/有参优化效果对比
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Improve AMR audio conversion quality and simplify opus conversion handling in media utilities.
Enhancements: