예제 #1
0
def _audio_tso2(atuple):
    audio, atag, advanced, _, _ = atuple
    if advanced:
        param = ast.literal_eval(atag)
        audio.add(TSO2(3, param[1]))
    else:
        audio.add(TSO2(3, atag))
예제 #2
0
def set_file_tags(filename, tags):
    try:
        default_tags = ID3(filename)
    except ID3NoHeaderError:
        # Adding ID3 header
        default_tags = ID3()

    # Tag Breakdown
    # Track: TIT2
    # OG Filename: TOFN # Artist - Song Title.MP3
    # Artist: TOPE, TPE1, WOAR(official), TSO2(itunes), TPE2(band)
    # Lyrics: TEXT
    # Album: TOAL(original), TSO2(itunes), TSOA(sort), TALB
    # Genres: TCON
    # Year: TORY(release), TYER(record)
    # Publisher: TPUB, WPUB(info)

    default_tags["TOFN"] = TOFN(encoding=3, text=os.path.split(filename[0])[1])  # Original Filename
    default_tags["TIT2"] = TIT2(encoding=3, text=tags.song)  # Title
    default_tags["TRCK"] = TRCK(encoding=3, text=tags.track_number)  # Track Number

    # Artist tags
    default_tags["TOPE"] = TOPE(encoding=3, text=tags.artist)  # Original Artist/Performer
    default_tags["TPE1"] = TPE1(encoding=3, text=tags.artist)  # Lead Artist/Performer/Soloist/Group
    default_tags["TPE2"] = TPE2(encoding=3, text=tags.album_artist)  # Band/Orchestra/Accompaniment

    # Album tags
    default_tags["TOAL"] = TOAL(encoding=3, text=tags.album)  # Original Album
    default_tags["TALB"] = TALB(encoding=3, text=tags.album)  # Album Name
    default_tags["TSO2"] = TSO2(encoding=3, text=tags.album)  # iTunes Album Artist Sort
    # tags["TSOA"] = TSOA(encoding=3, text=tags.album[0]) # Album Sort Order key

    default_tags["TCON"] = TCON(encoding=3, text=tags.genres)  # Genre
    default_tags["TDOR"] = TORY(encoding=3, text=str(tags.year))  # Original Release Year
    default_tags["TDRC"] = TYER(encoding=3, text=str(tags.year))  # Year of recording
    default_tags["USLT"] = USLT(encoding=3, text=tags.lyrics)  # Lyrics
    default_tags.save(v2_version=3)

    # Album Cover
    if type(tags.album_cover) == str:
        r = requests.get(tags.album_cover, stream=True)
        r.raise_for_status()
        r.raw.decode_content = True
        with open('img.jpg', 'wb') as out_file:
            shutil.copyfileobj(r.raw, out_file)
        del r

        with open('img.jpg', 'rb') as albumart:
            default_tags.add(APIC(
                              encoding=3,
                              mime="image/jpg",
                              type=3, desc='Cover',
                              data=albumart.read()))
    elif type(tags.album_cover) == bytes:
        default_tags.add(APIC(
                                encoding=3,
                                mime="image/jpg",
                                type=3, desc='Cover',
                                data=tags.album_cover))
    default_tags.save(v2_version=3)
    def id3_metadata_append(self, file_path, cover_path, title, artist, album,
                            album_artist, track_num_current, year):
        # 检测文件是否有效
        try:
            audio_obj = MP3(file_path, ID3=ID3)
        except HeaderNotFoundError:
            print("不是有效的MP3文件")
            return "fail"

        # 如果音乐内没有ID3信息,新建ID3信息
        if audio_obj.tags is None:
            try:
                audio_obj.add_tags()
                audio_obj.save()
            except error as e:
                print("无法写入ID3信息,错误信息:" + str(e))
                return "fail"

        # 修改ID3标签
        id3_info = ID3(file_path)

        # 删除已有的APIC帧,避免重复写入
        if id3_info.getall("APIC"):
            id3_info.delall("APIC")

        # 添加封面
        id3_info.add(
            APIC(
                encoding=0,  # LATIN1编码
                mime="image/jpeg",  # jpeg格式
                type=3,  # 封面图像
                data=open(cover_path, "rb").read()))

        # 添加歌曲名
        id3_info.add(TIT2(
            encoding=3,  # UTF8编码
            text=title))

        # 添加歌手名
        id3_info.add(TPE1(encoding=3, text=artist))

        # 添加专辑名
        id3_info.add(TALB(encoding=3, text=album))

        # 添加唱片集艺术家
        id3_info.add(TSO2(encoding=3, text=album_artist))

        # 添加当前音轨
        id3_info.add(TRCK(encoding=3, text=track_num_current))

        # 添加年份
        id3_info.add(TYER(encoding=3, text=year))

        # 保存文件(v2.3格式)
        id3_info.save(v2_version=3)
                                    ' - Masstamilan.In', ''
                                ).replace('[Masstamilan.in]', '').replace(
                                    ' - MassTamilan.com', ''
                                ).replace(' - Masstamilan.in', '').replace(
                                    ' -Masstamilan.In',
                                    '').replace('Masstamilan.In', '').replace(
                                        'masstamilan.in', '').replace(
                                            ' (masstamilan.in)', '').replace(
                                                ' - MassTamilan.org',
                                                '').replace(
                                                    '-StarMusiQ.Com',
                                                    '').replace(
                                                        '-MassTamilan.com', '')
                        try:
                            id3.add(TPE2(encoding=3, text=x))
                            id3.add(TSO2(encoding=3, text=x))
                        except:
                            print "***encoding error***"

                    # composer
                    tcom = id3.getall('TCOM')
                    if tcom:
                        try:
                            id3.add(TCOM(encoding=3, text=x))
                            id3.add(TSOC(encoding=3, text=x))
                        except:
                            print "***encoding error***"

                    #ARTIST
                    tpe1 = id3.getall('TPE1')
                    if tpe1:
예제 #5
0
 def __init_id3_tags(id3, major=3):
     """
     Attributes:
         id3 ID3 Tag object
         major ID3 major version, e.g.: 3 for ID3v2.3
     """
     from mutagen.id3 import TRCK, TPOS, TXXX, TPUB, TALB, UFID, TPE2, \
         TSO2, TMED, TIT2, TPE1, TSRC, IPLS, TORY, TDAT, TYER
     id3.add(TRCK(encoding=major, text="1/10"))
     id3.add(TPOS(encoding=major, text="1/1"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Release Group Id",
              text="e00305af-1c72-469b-9a7c-6dc665ca9adc"))
     id3.add(TXXX(encoding=major, desc="originalyear", text="2011"))
     id3.add(
         TXXX(encoding=major, desc="MusicBrainz Album Type", text="album"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Album Id",
              text="e7050302-74e6-42e4-aba0-09efd5d431d8"))
     id3.add(TPUB(encoding=major, text="J&R Adventures"))
     id3.add(TXXX(encoding=major, desc="CATALOGNUMBER", text="PRAR931391"))
     id3.add(TALB(encoding=major, text="Don\'t Explain"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Album Status",
              text="official"))
     id3.add(TXXX(encoding=major, desc="SCRIPT", text="Latn"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Album Release Country",
              text="US"))
     id3.add(TXXX(encoding=major, desc="BARCODE", text="804879313915"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Album Artist Id",
              text=[
                  "3fe817fc-966e-4ece-b00a-76be43e7e73c",
                  "984f8239-8fe1-4683-9c54-10ffb14439e9"
              ]))
     id3.add(TPE2(encoding=major, text="Beth Hart & Joe Bonamassa"))
     id3.add(TSO2(encoding=major, text="Hart, Beth & Bonamassa, Joe"))
     id3.add(TXXX(encoding=major, desc="ASIN", text="B005NPEUB2"))
     id3.add(TMED(encoding=major, text="CD"))
     id3.add(
         UFID(encoding=major,
              owner="http://musicbrainz.org",
              data=b"f151cb94-c909-46a8-ad99-fb77391abfb8"))
     id3.add(TIT2(encoding=major, text="Sinner's Prayer"))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Artist Id",
              text=[
                  "3fe817fc-966e-4ece-b00a-76be43e7e73c",
                  "984f8239-8fe1-4683-9c54-10ffb14439e9"
              ]))
     id3.add(TPE1(encoding=major, text=["Beth Hart & Joe Bonamassa"]))
     id3.add(
         TXXX(encoding=major,
              desc="Artists",
              text=["Beth Hart", "Joe Bonamassa"]))
     id3.add(TSRC(encoding=major, text=["NLB931100460", "USMH51100098"]))
     id3.add(
         TXXX(encoding=major,
              desc="MusicBrainz Release Track Id",
              text="d062f484-253c-374b-85f7-89aab45551c7"))
     id3.add(
         IPLS(encoding=major,
              people=[["engineer", "James McCullagh"],
                      ["engineer",
                       "Jared Kvitka"], ["arranger", "Jeff Bova"],
                      ["producer", "Roy Weisman"], ["piano", "Beth Hart"],
                      ["guitar", "Blondie Chaplin"],
                      ["guitar", "Joe Bonamassa"],
                      ["percussion", "Anton Fig"], ["drums", "Anton Fig"],
                      ["keyboard", "Arlan Schierbaum"],
                      ["bass guitar", "Carmine Rojas"],
                      ["orchestra", "The Bovaland Orchestra"],
                      ["vocals", "Beth Hart"], ["vocals",
                                                "Joe Bonamassa"]])),
     id3.add(TORY(encoding=major, text="2011"))
     id3.add(TYER(encoding=major, text="2011"))
     id3.add(TDAT(encoding=major, text="2709"))