Beispiel #1
0
    def test_update_v22_add(self):
        id3 = ID3Tags()
        tt1 = TT1(encoding=0, text=u'whatcha staring at?')
        id3.loaded_frame(tt1)
        tit1 = id3['TIT1']

        self.assertEquals(tt1.encoding, tit1.encoding)
        self.assertEquals(tt1.text, tit1.text)
        self.assert_('TT1' not in id3)
Beispiel #2
0
    def test_update_v22_add(self):
        id3 = ID3Tags()
        tt1 = TT1(encoding=0, text=u'whatcha staring at?')
        id3.loaded_frame(tt1)
        tit1 = id3['TIT1']

        self.assertEquals(tt1.encoding, tit1.encoding)
        self.assertEquals(tt1.text, tit1.text)
        self.assert_('TT1' not in id3)
Beispiel #3
0
    if rg == None:
        return 0
    try:
        id3 = id3.ID3(file)
    except mutagen.id3.ID3NoHeaderError:
        print "No ID3 header found; creating a new tag"
        id3 = mutagen.id3.ID3()
    except Exception, err:
        print str(err)
        return 0

    if "REPLAYGAIN_ALBUM_GAIN" in rg:
        frame = mutagen.id3.Frames["TXXX"](
            encoding=3, desc="replaygain_album_gain", text=rg["REPLAYGAIN_ALBUM_GAIN"][0]
        )
        id3.loaded_frame(frame)
        albumgain = float(rg["REPLAYGAIN_ALBUM_GAIN"][0].split(" ")[0])
        if "REPLAYGAIN_ALBUM_PEAK" in rg:
            frame = mutagen.id3.Frames["TXXX"](
                encoding=3, desc="replaygain_album_peak", text=rg["REPLAYGAIN_ALBUM_PEAK"][0]
            )
            id3.loaded_frame(frame)
            albumpeak = float(rg["REPLAYGAIN_ALBUM_PEAK"][0])
            frame = mutagen.id3.Frames["RVA2"](desc="album", channel=1, gain=albumgain, peak=albumpeak)
            id3.loaded_frame(frame)
        else:
            frame = mutagen.id3.Frames["RVA2"](desc="album", channel=1, gain=albumgain)
            id3.loaded_frame(frame)

    if "REPLAYGAIN_TRACK_GAIN" in rg:
        frame = mutagen.id3.Frames["TXXX"](
Beispiel #4
0
def add_to_id3(rg, id3):
    """
    Adds/updates replaygain/mp3gain headers in ID3v2 tag, returns the
    number of updated tags.

    :param id3: `mutagen.id3.ID3` to read
    :rtype: int
    """

    old_rg = read_from_id3(id3)
    updated = 0

    # for tagname in _floattags + _stringtags:
    #    id3.delall('TXXX:' + tagname)

    for tagname in rg.keys():
        if tagname not in old_rg or old_rg[tagname] != rg[tagname] \
            or len(id3.getall('TXXX:' + tagname.lower())):
            updated += 1
            id3.delall('TXXX:' + tagname.upper())
            id3.delall('TXXX:' + tagname.lower())
            frame = mutagen.id3.Frames['TXXX'](encoding=3,
                    desc=tagname, text=str(rg[tagname]))
            id3.loaded_frame(frame)

    for tagname in old_rg.keys():
        if tagname not in rg.keys():
            updated += 1
            id3.delall('TXXX:' + tagname.upper())
            id3.delall('TXXX:' + tagname.lower())

    for tagname in _stringtags + _floattags:
        matches = id3.getall('TXXX:' + tagname.lower())
        if len(matches) > 0:
            updated += 1
            id3.delall('TXXX:' + tagname.lower())
            frame = mutagen.id3.Frames['TXXX'](encoding=3,
                    desc=tagname, text=matches[0].text)
            id3.loaded_frame(frame)

    rva2_tags = id3.getall('RVA2')
    for gtype in ['album', 'track']:
        gain_tag = 'REPLAYGAIN_' + gtype.upper() + '_GAIN'
        peak_tag = 'REPLAYGAIN_' + gtype.upper() + '_PEAK'
        if gain_tag in rg:
            rva2 = None
            for r in rva2_tags:
                if r.desc == gtype and r.channel == 1:
                    rva2 = r
                    try:
                        rva2_tags.remove(r)
                    except:
                        pass
                    break
            if not rva2:
                rva2 = mutagen.id3.Frames['RVA2'](channel=1, desc=gtype)
            peak = None
            gain = float(rg[gain_tag])
            if peak_tag in rg:
                peak = float(rg[peak_tag])
            rva2.gain = gain
            rva2.peak = peak
            rva2_tags.append(rva2)
    id3.delall('RVA2')
    for r in rva2_tags:
        id3.loaded_frame(r)

    return updated