mp3rgain
Lossless MP3 volume adjustment - a modern mp3gain replacement written in Rust
mp3rgain adjusts MP3 volume without re-encoding by modifying the global_gain field in each frame's side information. This preserves audio quality while achieving permanent volume changes.
Features
- Lossless: No re-encoding, preserves original audio quality
- Fast: Direct binary manipulation, no audio decoding required for gain adjustment
- Reversible: All changes can be undone (stored in APEv2 tags)
- ReplayGain: Track and album gain analysis (included by default)
- AAC/M4A Support: Analyze and tag M4A files with ReplayGain metadata
- Zero runtime dependencies: Single static binary (no ffmpeg, no mp3gain)
- Cross-platform: macOS, Linux, Windows (x86_64 and ARM64)
- mp3gain compatible: Full command-line compatibility with original mp3gain
- Pure Rust: Memory-safe implementation
Installation
Homebrew (macOS)
Cargo (all platforms)
# Standard installation (includes ReplayGain support)
# Minimal installation (gain adjustment only, no audio decoding)
Download binary
Download the latest release from GitHub Releases:
- macOS (Universal):
mp3rgain-*-macos-universal.tar.gz - Linux (x86_64):
mp3rgain-*-linux-x86_64.tar.gz - Windows (x86_64):
mp3rgain-*-windows-x86_64.zip - Windows (ARM64):
mp3rgain-*-windows-arm64.zip
All binaries include full ReplayGain support.
Quick Start
# Normalize a single track to ReplayGain reference level (89 dB)
# Normalize an album (all tracks get the same adjustment)
# Apply manual gain adjustment (+3.0 dB)
# Undo previous changes
Usage
Show file information (default)
Output:
song.mp3
Format: MPEG1 Layer III, Joint Stereo
Frames: 5765
Gain range: 89 - 217 (avg: 168.2)
Headroom: 38 steps (+57.0 dB)
Apply gain adjustment
# Apply +2 steps (+3.0 dB)
# Apply +4.5 dB (rounds to nearest step)
# Reduce volume by 3 steps (-4.5 dB)
# Apply gain and preserve file timestamp
ReplayGain
# Apply track gain (normalize each file to 89 dB)
# Apply album gain (normalize album to 89 dB)
AAC/M4A Support
# Analyze and tag M4A files with ReplayGain
# Album gain for M4A files
# Mix MP3 and M4A files
# Recursive directory processing includes M4A files
# Process specific audio track in multi-track files (e.g., video files)
Note: For M4A files, mp3rgain writes ReplayGain tags (iTunes freeform format) but does not modify the audio data, as AAC doesn't have a lossless gain adjustment mechanism like MP3's global_gain field.
Undo previous adjustment
# Undo gain changes (uses APEv2 tag info)
Clipping prevention
# Automatically reduce gain if clipping would occur
# Output: gain reduced from 5 to 3 steps to prevent clipping
# With ReplayGain
Recursive directory processing
# Process all MP3s in a directory recursively
Channel-specific gain (stereo balance)
# Apply +3 steps to left channel only
# Apply -2 steps to right channel only
Dry-run mode
# Preview changes without modifying files
Find maximum amplitude
# Only show max amplitude (no gain applied)
Modify suggested gain
# Apply track gain with +2 step offset
# Apply album gain with -1 step offset
Tag management
# Check stored tag info
# Delete stored ReplayGain tags
# Skip existing tags and force recalculation
Output formats
# Output in JSON format for scripting
# Tab-separated output (database-friendly)
# Combine with other options
Safe file writing
# Use temp file for atomic writes (safer)
# Apply gain with wrapping (for edge cases)
Example JSON output:
Command-Line Options
| Option | Description |
|---|---|
-g <i> |
Apply gain of i steps (each step = 1.5 dB) |
-d <n> |
Apply gain of n dB (rounded to nearest step) |
-l <c> <g> |
Apply gain to left (0) or right (1) channel only |
-m <i> |
Modify suggested gain by integer i |
-r |
Apply Track gain (ReplayGain analysis) |
-a |
Apply Album gain (ReplayGain analysis) |
-e |
Skip album analysis (even with multiple files) |
-i <n> |
Specify which audio track to process (default: 0) |
-u |
Undo gain changes (restore from APEv2 tag) |
-x |
Only find max amplitude of file |
-s <mode> |
Stored tag handling: c (check), d (delete), s (skip), r (recalc), i (ID3v2), a (APEv2) |
-p |
Preserve original file timestamp |
-c |
Ignore clipping warnings |
-k |
Prevent clipping (automatically limit gain) |
-w |
Wrap gain values (instead of clamping 0-255) |
-t |
Use temp file for writing (safer atomic writes) |
-f |
Assume MPEG 2 Layer III (compatibility flag) |
-q |
Quiet mode (less output) |
-R |
Process directories recursively |
-n, --dry-run |
Dry-run mode (show what would be done) |
-o <fmt> |
Output format: text (default), json, or tsv |
-v |
Show version |
-h |
Show help |
mp3gain Compatibility
mp3rgain is a drop-in replacement for the original mp3gain:
# These commands work identically in both mp3gain and mp3rgain
Technical Details
Gain Steps
Each gain step equals 1.5 dB (fixed by MP3 specification). The global_gain field is 8 bits, allowing values 0-255.
| Steps | dB Change |
|---|---|
| +1 | +1.5 dB |
| +2 | +3.0 dB |
| +4 | +6.0 dB |
| -2 | -3.0 dB |
How It Works
MP3 files contain a global_gain field in each frame's side information that controls playback volume. mp3rgain directly modifies these values without touching the audio data, making the adjustment completely lossless and reversible.
ReplayGain Analysis
mp3rgain uses the symphonia crate for audio decoding and implements the ReplayGain 1.0 algorithm:
- Decode MP3/AAC to PCM audio
- Apply equal-loudness filter (Yule-Walker + Butterworth)
- Calculate RMS loudness in 50ms windows
- Use 95th percentile for loudness measurement
- Calculate gain to reach 89 dB reference level
AAC/M4A Support
For AAC/M4A files, mp3rgain:
- Analyzes audio loudness using the same ReplayGain algorithm as MP3
- Writes ReplayGain tags in iTunes freeform format (
com.apple.iTunes:replaygain_*) - Does NOT modify audio data (AAC lacks a lossless gain mechanism)
Players that support ReplayGain tags will automatically apply volume normalization during playback.
Compatibility
- MPEG1 Layer III (MP3)
- MPEG2 Layer III
- MPEG2.5 Layer III
- AAC/M4A (ReplayGain tags only)
- Mono, Stereo, Joint Stereo, Dual Channel
- ID3v2 tags (preserved)
- APEv2 tags (for undo support)
- iTunes metadata (for M4A files)
- VBR and CBR files
Why mp3rgain?
The original mp3gain has been unmaintained since ~2015 and has compatibility issues with modern systems (including Windows 11). mp3rgain is a modern replacement that:
- Works on Windows 11, macOS, and Linux
- Has no runtime dependencies
- Is written in memory-safe Rust
- Uses the same command-line interface
- Includes a library API for integration
Library Usage
use ;
use Path;
// Apply +2 gain steps (+3.0 dB)
let frames = apply_gain?;
println!;
// Analyze file
let info = analyze?;
println!;
Acknowledgments
- symphonia - Pure Rust audio decoding library (used for ReplayGain analysis)
- Original mp3gain - The original C implementation that inspired this project
- aacgain - AAC/MP4 ReplayGain implementation
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
We especially welcome:
- Windows testing and compatibility reports
- Bug reports and feature requests
License
MIT License - see LICENSE for details.
See Also
- Original mp3gain - The original C implementation
- headroom - DJ audio loudness optimizer (uses mp3rgain internally)