public
Last active

Add ReplayGain information to id3 tags of mp3 files

  • Download Gist
replaygain-id3
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#!/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';
 
GetOptions(
'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_track_gain:$track->{gain}",
"replaygain_track_peak:$track->{peak}",
($album_mode
? ("replaygain_album_gain:$album{gain}",
"replaygain_album_peak:$album{peak}")
: ()
)
);
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
 
=head1 SYNOPSIS
 
B<replaygain-id3> [options] <files>
 
Options:
-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.
 
Examples:
 
# an album
replaygain-id3 -a My_artist-My_album/*
# unsorted mp3 files
replaygain-id3 misc_mp3/*
 
=head1 DESCRIPTION
 
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, hinrik.sig@gmail.com
 
=head1 LICENSE AND COPYRIGHT
 
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.
 
=cut

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.