Hack:rg.py
From Music Player Daemon Community Wiki
A wrapper around mp3gain that adds ReplayGain info to ID3v2 tags instead of APEv2 tags. Also see Hack:ape2id3.py.
Requires mp3gain and pyid3lib. Note: Neither are available as packages in Ubuntu.
Also available from [1].
#! /usr/bin/env python
import os
import sys
from subprocess import Popen, PIPE
import pyid3lib
files = []
mp3gain = ["mp3gain", "-o", "-s", "s"]
cmd = mp3gain + sys.argv[1:]
pipe = Popen(cmd, stdout=PIPE).stdout
pipe.next()
for line in pipe:
parts = line.rstrip("\n").split("\t")
if parts[0] != '"Album"':
files.append(parts)
else:
for file in files:
frames = {
"replaygain_track_gain": "%.2f dB" % (float(file[2])),
"replaygain_album_gain": "%.2f dB" % (float(parts[2])),
"replaygain_track_peak": "%.6f" % (float(file[3])/32768.0),
"replaygain_album_peak": "%.6f" % (float(parts[3])/32768.0),
}
tag = pyid3lib.tag(file[0])
for key, value in frames.iteritems():
frame = {
"frameid": "TXXX",
"textenc": 1,
"description": key,
"text": value,
}
changed = False
for i in range(0, len(tag)):
if tag[i]["frameid"] == "TXXX":
if tag[i]["description"] == key:
tag[i] = frame
changed = True
if not changed:
tag.append(frame)
tag.update()
files = []
