Help - Search - Members - Calendar
Full Version: Replaygain into GNU/Linux
Hydrogenaudio Forums > Lossy Audio Compression > MP3 > MP3 - General
iGold
I'm using Linux Ubuntu 5.10 for my work desktop and most time I'm listen music under Linux. Now de'facto standard of lossy codec under Linux is Ogg Vorbis (and FLAC for loseless). Replaygain into these formats supported by many linux players. But not into mp3. I'm using MPD for playing music (daemon with control thru tcp port) and it's current svn version has support for replaygain tags into ID3v2. But only one program for linux I know with ability to calculate these tags is mp3gain and it writes info into APEv2 tag not compatible with MPD replaygain support.

I've write simple shell script for calculate ReplayGain and convert it into ID3v2 tag (mp3 file must already has ID3v2 tag). Maybe it will be usefull for someone.

Calculate ReplayGain for all .mp3 files into current directory as one album.
Tested only on my system.
Required executables id3v2, mp3gain and eyeD3.

CODE
#!/bin/sh

if ! which id3v2 >/dev/null; then
echo "id3v2 executable not found." >&2
exit 1
fi

if ! which mp3gain >/dev/null; then
echo "mp3gain executable not found." >&2
exit 1
fi

if ! which eyeD3 >/dev/null; then
echo "eyeD3 executable not found." >&2
exit 1
fi

if [[ $# > 1 || $# == 1 && $1 != "-f" ]]; then
echo "Usage: `basename $0` [-f]" >&2
echo " for ReplayGain'ing all mp3 file into current directory" >&2
echo " -f -- force re-ReplayGain'ing for already ReplayGain'ed files" >&2
exit 2
fi

if [[ $# == 0 ]]; then
if id3v2 -l *.mp3 | egrep -qi '^TXXX .*\(replaygain_album_gain\)';then
echo "Files already ReplayGain'ed." >&2
exit 1
fi
fi

mp3gain *.mp3

TMPFILE=`tempfile`

for n in *.mp3; do
mp3gain -s c "$n" > $TMPFILE
mp3gain -s d "$n"
TRACK_GAIN=`cat $TMPFILE|awk '/^Recommended "Track" dB / {printf("%+.2f dB", $5)}'`
TRACK_PEAK=`cat $TMPFILE|awk '/^Max PCM / {printf("%.6f", $7/32768)}'`
ALBUM_GAIN=`cat $TMPFILE|awk '/^Recommended "Album" dB / {printf("%+.2f dB", $5)}'`
ALBUM_PEAK=`cat $TMPFILE|awk '/^Max Album PCM / {printf("%.6f", $8/32768)}'`
eyeD3 \
--set-user-text-frame="replaygain_track_gain:$TRACK_GAIN" \
--set-user-text-frame="replaygain_track_peak:$TRACK_PEAK" \
--set-user-text-frame="replaygain_album_gain:$ALBUM_GAIN" \
--set-user-text-frame="replaygain_album_peak:$ALBUM_PEAK" \
"$n"
done

rm -f $TMPFILE

Edit: change code to codebox
MJT
Very nice. I didn't know abut eyeD3, thanks.

CODE
   TRACK_GAIN=`cat $TMPFILE|awk '/^Recommended "Track" dB / {printf("%+.2f dB", $5)}'`


Watch out for that Useless Use Of Cat...

CODE
   TRACK_GAIN=$(awk '/^Recommended "Track" dB / {printf("%+.2f dB", $5)}' $TMPFILE)


should work just as well.
augustob
Seeing as I just started rebuilding my MP3 collection, I had to run this for a LOT of discs,
The following scripts runs iGold's script recursively for you if you structure your collection like me:

CODE
/[Artist]/[Album]/[Files].mp3


Keep in mind my shell script skills suck and this is a quick hack, but might be helpful for someone in my situation. Before running it, cd to the root of your collection. Any improvement suggestions or syntax corrections, I'd be happy to hear them.

CODE
#!/bin/sh

TARGET=$1
REPLAYGAIN=/home/augusto/replaygain.sh

cd "$TARGET"

for artist in *; do
   cd "$artist"
   for album in *; do
       cd "$album"
       $REPLAYGAIN
       cd ..
   done
   cd ..
done
xmixahlx
you might want to let us know about your fancy replaygain script for that to be exciting...


later
augustob
You might want to check the fancy first post of the thread, I assumed that was clear enough.

Quoting myself:

"...runs iGold's script recursively for you..."
iGold
I made some changes in the script:
  • not need id3v2 anymore (it's now check replaygain tags with eyeD3)
  • set numeric formatting to english style for locale aware awk program (GNU awk in russian locale generates gain values like "-5,14 dB")
CODE
#!/bin/sh

LC_NUMERIC=POSIX; export LC_NUMERIC

MP3GAIN=`which mp3gain`
if [[ $? != 0 ]]; then
echo "mp3gain executable not found." >&2
exit 1
fi

EYED3=`which eyeD3`
if [[ $? != 0 ]]; then
echo "eyeD3 executable not found." >&2
exit 1
fi

if [[ $# > 1 || $# == 1 && $1 != "-f" ]] ; then
echo "Usage: `basename $0` [-f]" >&2
echo " for ReplayGain'ing all mp3 file into current directory" >&2
echo " -f -- force re-ReplayGain'ing for already ReplayGain'ed files" >&2
exit 2
fi

if [[ $# == 0 ]]; then
$EYED3 --no-color *.mp3 \
| egrep -i '^UserTextFrame: \[Description: replaygain_album_gain\]$' >/dev/null 2>&1
if [[ $? == 0 ]]; then
echo "Files already ReplayGain'ed." >&2
exit 1
fi
fi

$MP3GAIN *.mp3

TMPFILE=`tempfile`

for n in *.mp3; do
$MP3GAIN -s c "$n" > $TMPFILE
$MP3GAIN -s d "$n"
TRACK_GAIN=`awk '/^Recommended "Track" dB / { printf("%+.2f dB", $5) }' $TMPFILE`
ALBUM_GAIN=`awk '/^Recommended "Album" dB / { printf("%+.2f dB", $5) }' $TMPFILE`
TRACK_PEAK=`awk '/^Max PCM / { printf("%.6f", $7/32768) }' $TMPFILE`
ALBUM_PEAK=`awk '/^Max Album PCM / { printf("%.6f", $8/32768) }' $TMPFILE`
$EYED3 \
--set-user-text-frame="replaygain_track_gain:$TRACK_GAIN" \
--set-user-text-frame="replaygain_track_peak:$TRACK_PEAK" \
--set-user-text-frame="replaygain_album_gain:$ALBUM_GAIN" \
--set-user-text-frame="replaygain_album_peak:$ALBUM_PEAK" \
"$n"
done

rm -f $TMPFILE
Pusherman
Which win32 tool can i use to write replaygain values that MPD understand?

(sorry bad english)
iGold
foobar2000 (and maybe new Winamp 5.3)

Edit: MPD must be at least at version 0.12.0 (released 4 days ago).
goweropolis
QUOTE (iGold @ Sep 27 2006, 08:48) *
foobar2000 (and maybe new Winamp 5.3)

Edit: MPD must be at least at version 0.12.0 (released 4 days ago).


Hey iGold, thanks for the headsup on the new MPD ReplayGain feature. I just started using MPD recently, and I love it. And I still use foobar2000 with wine for my tagging, converting, and organizing because it rocks, so it will be nice to have MPD recognizing the ReplayGain values that foobar2000 writes.
Pusherman
Thanks for info, it was nice to see that fb2k uses both cores for calculations.
frodoontop
Mp3's handled with mp3gain are played correctly in latest Amarok audio player. But with Ubuntu you're probably more on the Gnome side.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.