mutagen/id3.py
def MakeID3v1(id3):
    """Return an ID3v1.1 tag string from a dict of ID3v2.4 frames."""

    v1 = {}

    for v2id, name in {"TIT2": "title", "TPE1": "artist",
                       "TALB": "album"}.items():
        if v2id in id3:
            text = id3[v2id].text[0].encode('latin1', 'replace')[:30]
        else:
            text = b''
        v1[name] = text + (b'\x00' * (30 - len(text)))

    if "COMM" in id3:
        cmnt = id3["COMM"].text[0].encode('latin1', 'replace')[:28]
    else:
        cmnt = b''
    v1['comment'] = cmnt + (b'\x00' * (29 - len(cmnt)))

    if "TRCK" in id3:
        try:
            v1["track"] = chr_(+id3["TRCK"])
        except ValueError:
            v1["track"] = b'\x00'
    else:
        v1["track"] = b'\x00'

    if "TCON" in id3:
        try:
            genre = id3["TCON"].genres[0]
        except IndexError:
            pass
        else:
            if genre in TCON.GENRES:
                v1["genre"] = chr_(TCON.GENRES.index(genre))
    if "genre" not in v1:
        v1["genre"] = b"\xff"

    if "TDRC" in id3:
        year = text_type(id3["TDRC"]).encode('latin1', 'replace')
    elif "TYER" in id3:
        year = text_type(id3["TYER"]).encode('latin1', 'replace')
    else:
        year = b''
    v1['year'] = (year + b'\x00\x00\x00\x00')[:4]


    return (b'TAG' +
            v1['title'] +
            v1['artist'] +
            v1['album'] +
            v1['year'] +
            v1['comment'] +
            v1['track'] +
            v1['genre'])
def MakeID3v1(id3):
    """Return an ID3v1.1 tag string from a dict of ID3v2.4 frames."""

    v1 = {}

    for v2id, name in {"TIT2": "title", "TPE1": "artist",
                       "TALB": "album"}.items():
        if v2id in id3:
            text = id3[v2id].text[0].encode('latin1', 'replace')[:30]
        else:
            text = b''
        v1[name] = text + (b'\x00' * (30 - len(text)))

    if "COMM" in id3:
        cmnt = id3["COMM"].text[0].encode('latin1', 'replace')[:28]
    else:
        cmnt = b''
    v1['comment'] = cmnt + (b'\x00' * (29 - len(cmnt)))

    if "TRCK" in id3:
        try:
            v1["track"] = chr_(+id3["TRCK"])
        except ValueError:
            v1["track"] = b'\x00'
    else:
        v1["track"] = b'\x00'

    if "TCON" in id3:
        try:
            genre = id3["TCON"].genres[0]
        except IndexError:
            pass
        else:
            if genre in TCON.GENRES:
                v1["genre"] = chr_(TCON.GENRES.index(genre))
    if "genre" not in v1:
        v1["genre"] = b"\xff"

    if "TDRC" in id3:
        year = text_type(id3["TDRC"]).encode('latin1', 'replace')
    elif "TYER" in id3:
        year = text_type(id3["TYER"]).encode('latin1', 'replace')
    else:
        year = b''
    v1['year'] = (year + b'\x00\x00\x00\x00')[:4]


    return (b'TAG' +
            v1['title'] +
            v1['artist'] +
            v1['album'] +
            v1['year'] +
            v1['comment'] +
            v1['track'] +
            v1['genre'])
def MakeID3v1 ( id3 ) : v1 = { } for v2id , name in { "TIT2" : "title" , "TPE1" : "artist" , "TALB" : "album" } . items ( ) : if v2id in id3 : text = id3 [ v2id ] . text [ 0 ] . encode ( 'latin1' , 'replace' ) [ : 30 ] else : text = b'' v1 [ name ] = text + ( b'\x00' * ( 30 - len ( text ) ) ) if "COMM" in id3 : cmnt = id3 [ "COMM" ] . text [ 0 ] . encode ( 'latin1' , 'replace' ) [ : 28 ] else : cmnt = b'' v1 [ 'comment' ] = cmnt + ( b'\x00' * ( 29 - len ( cmnt ) ) ) if "TRCK" in id3 : try : v1 [ "track" ] = chr_ ( + id3 [ "TRCK" ] ) except ValueError : v1 [ "track" ] = b'\x00' else : v1 [ "track" ] = b'\x00' if "TCON" in id3 : try : genre = id3 [ "TCON" ] . genres [ 0 ] except IndexError : pass else : if genre in TCON . GENRES : v1 [ "genre" ] = chr_ ( TCON . GENRES . index ( genre ) ) if "genre" not in v1 : v1 [ "genre" ] = b"\xff" if "TDRC" in id3 : year = text_type ( id3 [ "TDRC" ] ) . encode ( 'latin1' , 'replace' ) elif "TYER" in id3 : year = text_type ( id3 [ "TYER" ] ) . encode ( 'latin1' , 'replace' ) else : year = b'' v1 [ 'year' ] = ( year + b'\x00\x00\x00\x00' ) [ : 4 ] return ( b'TAG' + v1 [ 'title' ] + v1 [ 'artist' ] + v1 [ 'album' ] + v1 [ 'year' ] + v1 [ 'comment' ] + v1 [ 'track' ] + v1 [ 'genre' ] )
Return an ID3v1.1 tag string from a dict of ID3v2.4 frames.
Return an ID3v1 . 1 tag string from a dict of ID3v2 . 4 frames .