Skip to content

convert: write R128 gain tags to Opus files - #7127

Open
berettavexee wants to merge 1 commit into
beetbox:masterfrom
berettavexee:convert-opus-r128-gain
Open

berettavexee wants to merge 1 commit into
beetbox:masterfrom
berettavexee:convert-opus-r128-gain

Conversation

@berettavexee

@berettavexee berettavexee commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Description

When convert transcodes to Opus, the resulting files end up with no usable
gain information:

  • Opus players only honour R128_TRACK_GAIN / R128_ALBUM_GAIN (RFC 7845),
    while a library of e.g. FLAC files only stores ReplayGain values
    (rg_*), which write_metadata copies as REPLAYGAIN_* tags.
  • With auto_keep, the copy is made in an early import stage, i.e. before
    the replaygain plugin has analysed the imported items, so it gets no gain
    tags at all.
  • beet replaygain only processes library items, and converted copies are
    not in the library, so there is no way to fix them afterwards. Re-running
    beet convert skips them since the destination already exists.

This PR makes convert take care of the gain tags of the Opus files it
writes:

  • R128 gains are derived from the item's values: existing r128_* gains are
    used as is, otherwise rg_* gains are shifted by -5 dB (ReplayGain 2.0
    references -18 LUFS, R128 -23 LUFS).
  • If the item has no gain data and the file has none either, the track gain
    is computed on the converted file using the replaygain plugin's backend,
    when that plugin is enabled. The library item is not modified.
  • When a destination file already exists and is skipped, its gain tags are
    still updated (without re-encoding). Tags that are already up to date are
    not rewritten, so running convert again stays cheap. This lets users fix
    previously converted files with beet convert.

Linked/hardlinked files are left alone, since they are the library files
themselves and are handled by replaygain directly.

AI disclosure: I used Claude Code to write the patch,the tests.
I reviewed the change, ran the tests. I am one of the three one been that
care about Ogg Opus ;-)

Opus players only honour R128_* gain tags, but transcoded copies only
received the library's REPLAYGAIN_* values, and none at all when the
copy was made at import time, before replaygain ran.

Derive R128 track and album gains from the library's ReplayGain values
when writing Opus files. When the library has no gain data, compute the
track gain on the converted file with the replaygain plugin, if enabled.
Also update the gain tags of destination files that already exist, so
running convert again fills in previously converted files.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@berettavexee
berettavexee requested a review from a team as a code owner October 29, 2026 23:20
@codecov

codecov Bot commented Oct 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.44444% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.62%. Comparing base (ab4bdfd) to head (2ac4c5e).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
beetsplug/convert.py 84.44% 5 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #7127      +/-   ##
==========================================
- Coverage   80.63%   80.62%   -0.02%     
==========================================
  Files         149      149              
  Lines       21254    21299      +45     
  Branches     3284     3292       +8     
==========================================
+ Hits        17139    17172      +33     
- Misses       3288     3297       +9     
- Partials      827      830       +3     
Files with missing lines Coverage Δ
beetsplug/convert.py 76.98% <84.44%> (+0.93%) ⬆️

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant