Ejemplo n.º 1
0
def delete_frames(deletes, filenames):

    try:
        deletes = frame_from_fsnative(deletes)
    except ValueError as err:
        print_(text_type(err), file=sys.stderr)

    frames = deletes.split(",")

    for filename in filenames:
        with _sig.block():
            if verbose:
                print_(u"deleting %s from" % deletes, filename,
                       file=sys.stderr)
            try:
                id3 = mutagen.id3.ID3(filename)
            except mutagen.id3.ID3NoHeaderError:
                if verbose:
                    print_(u"No ID3 header found; skipping.", file=sys.stderr)
            except Exception as err:
                print_(text_type(err), file=sys.stderr)
                raise SystemExit(1)
            else:
                for frame in frames:
                    id3.delall(frame)
                id3.save()
Ejemplo n.º 2
0
def delete_frames(deletes, filenames):

    try:
        deletes = frame_from_fsnative(deletes)
    except ValueError as err:
        print_(text_type(err), file=sys.stderr)

    frames = deletes.split(",")

    for filename in filenames:
        with _sig.block():
            if verbose:
                print_(u"deleting %s from" % deletes,
                       filename,
                       file=sys.stderr)
            try:
                id3 = mutagen.id3.ID3(filename)
            except mutagen.id3.ID3NoHeaderError:
                if verbose:
                    print_(u"No ID3 header found; skipping.", file=sys.stderr)
            except Exception as err:
                print_(text_type(err), file=sys.stderr)
                raise SystemExit(1)
            else:
                for frame in frames:
                    id3.delall(frame)
                id3.save()
Ejemplo n.º 3
0
def copy(src, dst, merge, write_v1=True, excluded_tags=None, verbose=False):
    """Returns 0 on success"""

    if excluded_tags is None:
        excluded_tags = []

    try:
        id3 = mutagen.id3.ID3(src, translate=False)
    except mutagen.id3.ID3NoHeaderError:
        print_(u"No ID3 header found in ", src, file=sys.stderr)
        return 1
    except Exception as err:
        print_(str(err), file=sys.stderr)
        return 1

    if verbose:
        print_(u"File", src, u"contains:", file=sys.stderr)
        print_(id3.pprint(), file=sys.stderr)

    for tag in excluded_tags:
        id3.delall(tag)

    if merge:
        try:
            target = mutagen.id3.ID3(dst, translate=False)
        except mutagen.id3.ID3NoHeaderError:
            # no need to merge
            pass
        except Exception as err:
            print_(str(err), file=sys.stderr)
            return 1
        else:
            for frame in id3.values():
                target.add(frame)

            id3 = target

    # if the source is 2.3 save it as 2.3
    if id3.version < (2, 4, 0):
        id3.update_to_v23()
        v2_version = 3
    else:
        id3.update_to_v24()
        v2_version = 4

    try:
        id3.save(dst, v1=(2 if write_v1 else 0), v2_version=v2_version)
    except Exception as err:
        print_(u"Error saving",
               dst,
               u":\n%s" % text_type(err),
               file=sys.stderr)
        return 1
    else:
        if verbose:
            print_(u"Successfully saved", dst, file=sys.stderr)
        return 0
Ejemplo n.º 4
0
def copy(src, dst, merge, write_v1=True, excluded_tags=None, verbose=False):
    """Returns 0 on success"""

    if excluded_tags is None:
        excluded_tags = []

    try:
        id3 = mutagen.id3.ID3(src, translate=False)
    except mutagen.id3.ID3NoHeaderError:
        print_(u"No ID3 header found in ", src, file=sys.stderr)
        return 1
    except Exception as err:
        print_(str(err), file=sys.stderr)
        return 1

    if verbose:
        print_(u"File", src, u"contains:", file=sys.stderr)
        print_(id3.pprint(), file=sys.stderr)

    for tag in excluded_tags:
        id3.delall(tag)

    if merge:
        try:
            target = mutagen.id3.ID3(dst, translate=False)
        except mutagen.id3.ID3NoHeaderError:
            # no need to merge
            pass
        except Exception as err:
            print_(str(err), file=sys.stderr)
            return 1
        else:
            for frame in id3.values():
                target.add(frame)

            id3 = target

    # if the source is 2.3 save it as 2.3
    if id3.version < (2, 4, 0):
        id3.update_to_v23()
        v2_version = 3
    else:
        id3.update_to_v24()
        v2_version = 4

    try:
        id3.save(dst, v1=(2 if write_v1 else 0), v2_version=v2_version)
    except Exception as err:
        print_(u"Error saving", dst, u":\n%s" % text_type(err),
               file=sys.stderr)
        return 1
    else:
        if verbose:
            print_(u"Successfully saved", dst, file=sys.stderr)
        return 0
Ejemplo n.º 5
0
def website_delete(id3, key):
    id3.delall("WOAR")
Ejemplo n.º 6
0
def website_set(id3, key, value):
    id3.delall("WOAR")
    for v in value:
        id3.add(mutagen.id3.WOAR(url=v))
Ejemplo n.º 7
0
def website_delete(id3, key):
    id3.delall("WOAR")
Ejemplo n.º 8
0
def website_set(id3, key, value):
    id3.delall("WOAR")
    for v in value:
        id3.add(mutagen.id3.WOAR(url=v))
Ejemplo n.º 9
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
Ejemplo n.º 10
0
def url_frame_delete(frameid, unused_id3, unused_key):
  id3.delall(frameid)
Ejemplo n.º 11
0
def url_frame_set(frameid, id3, key, value):
  id3.delall(frameid)
  for v in value:
    id3.add(getattr(mutagen.id3, frameid)(url=v))