Skip to content

@hinrik /replaygain-id3

Embed URL


Subversion checkout URL

You can clone with
Download ZIP
Add ReplayGain information to id3 tags of mp3 files
#!/usr/bin/env perl
use strict;
use warnings;
use File::Which;
use Getopt::Long qw(:config auto_help);
use Pod::Usage;
use String::ShellQuote;
my $VERSION = '0.01';
'a|album' => \my $album_mode,
'v|version' => sub { print "replaygain-id3 $VERSION\n"; exit },
) or pod2usage();
die "mp3gain not installed\n" if !defined which('mp3gain');
die "eyeD3 not installed\n" if !defined which('eyeD3');
@ARGV = grep { /\.mp3$/i } @ARGV;
die "No mp3 files specified\n" if !@ARGV;
my $files = shell_quote(@ARGV);
# calculate ReplayGain data with mp3gain(1)
my @tracks_raw = split /\n\n/, qx{mp3gain -s s $files};
my $album_raw = splice @tracks_raw, -1;
my %album;
$album{gain} = sprintf('%.2f dB', $album_raw =~ /^Recommended "Album" dB change for all files: (.+)$/m);
for my $track_raw (@tracks_raw) {
my %track;
my ($file) = $track_raw =~ /\A(.+)\n/m;
next if $file !~ /\.mp3$/i;
($track{gain}) = $track_raw =~ /^Recommended "Track" dB change: (.+)$/m;
$track{gain} = sprintf('%.2f dB', $track{gain});
($track{peak}) = $track_raw =~ /^Max PCM sample at current gain: (.+)$/m;
$album{peak} = $track{peak} if !defined $album{peak} || $track{peak} > $album{peak};
$track{peak} = sprintf('%.6f', $track{peak} / 32768);
$album{tracks}{$file} = \%track;
$album{peak} = sprintf('%.6f', $album{peak} / 32768);
# tag the files with eyeD3(1)
while (my ($file, $track) = each %{ $album{tracks} }) {
my @frames = (
? ("replaygain_album_gain:$album{gain}",
: ()
system "eyeD3 ".join(' ', map({ "--set-user-text-frame='$_'" } @frames)).' '.shell_quote($file)." >/dev/null 2>&1";
=head1 NAME
replaygain-id3 - Add ReplayGain information to id3 tags of mp3 files
B<replaygain-id3> [options] <files>
-a, --album Enable album mode (see below)
-h, --help Print this help message
-v, --version Print version
If you enable album mode, the affected files will be considered to be part
of the same album, and the corresponding album-global ReplayGain info will
be added.
# an album
replaygain-id3 -a My_artist-My_album/*
# unsorted mp3 files
replaygain-id3 misc_mp3/*
This little script uses C<mp3gain> to calculate ReplayGain information for
mp3 files specified on the command line. It will then add that information
to id3 v2 tags in the files using the C<eyeD3> program.
=head1 AUTHOR
Hinrik E<Ouml>rn SigurE<eth>sson,
Copyright 2009 Hinrik E<Ouml>rn SigurE<eth>sson
This program is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.