Exemple #1
0
 def test_keep_some_v24_tag(self):
     tags = compatid3.CompatID3()
     tags.add(id3.TSOP(encoding=0, text=["foo"]))
     tags.add(id3.TSOA(encoding=0, text=["foo"]))
     tags.add(id3.TSOT(encoding=0, text=["foo"]))
     tags.update_to_v23()
     self.assertEqual(tags["TSOP"].text, ["foo"])
     self.assertEqual(tags["TSOA"].text, ["foo"])
     self.assertEqual(tags["TSOT"].text, ["foo"])
def add_id3_data_to_mp3(mp3_path,
                        track_name,
                        artist_name,
                        album_name,
                        genres,
                        cover_art_path=None,
                        remove_cover_art=True):
    mp3_path = Path(mp3_path).resolve()
    cover_art_path = Path(cover_art_path).resolve()

    try:
        mp3_meta = id3.ID3(mp3_path)
    except mutagen.id3.ID3NoHeaderError:
        mp3_meta = mutagen.File(mp3_path, easy=True)
        mp3_meta.add_tags()
        mp3_meta.save()
        mp3_meta = id3.ID3(mp3_path)

    # track
    mp3_meta.add(id3.TIT2(text=track_name, encoding=3))
    mp3_meta.add(id3.TSOT(text=track_name, encoding=3))

    # artist
    mp3_meta.add(id3.TOPE(text=artist_name, encoding=3))
    mp3_meta.add(id3.TPE1(text=artist_name, encoding=3))
    mp3_meta.add(id3.TSO2(text=artist_name, encoding=3))

    # album
    mp3_meta.add(id3.TALB(text=album_name, encoding=3))
    mp3_meta.add(id3.TOAL(text=album_name, encoding=3))
    mp3_meta.add(id3.TSOA(text=album_name, encoding=3))

    # genre
    if len(genres) > 0:
        mp3_meta.add(id3.TCON(text=genres[0], encoding=3))

    # comment
    mp3_meta.add(
        id3.COMM(text='finessed', desc=u'finessed', encoding=3, lang='ENG'))

    # cover art
    if cover_art_path:
        ext = Path(cover_art_path).suffix[1:]
        just_name = Path(cover_art_path).stem
        parent_dir = Path(cover_art_path).parent

        if ext == 'jpg' or ext == 'jpeg' or ext == 'webp' or ext == 'png':
            if ext == 'jpg' or ext == 'jpeg' or ext == 'webp':
                with Image.open(cover_art_path).convert("RGB") as cover_art:
                    cover_art.save(f"{parent_dir / just_name}.png", "png")
                    og_cover_art = str(cover_art_path)
                    cover_art_path = f"{parent_dir / just_name}.png"

            with open(cover_art_path, 'rb') as cover_art:
                mp3_meta.add(
                    id3.APIC(encoding=3,
                             mime=f'image/png',
                             type=3,
                             desc='youtube thumbnail',
                             data=cover_art.read()))

            if remove_cover_art:
                Path(cover_art_path).unlink()
                Path(og_cover_art).unlink()

    mp3_meta.save()