fixed encoding bug with mp3 files

This commit is contained in:
Lars Noack 2022-10-21 10:56:22 +02:00
parent be2d4738f4
commit c6d8c7e18e

View File

@ -3,7 +3,7 @@ import os.path
import pandas as pd import pandas as pd
import mutagen import mutagen
from mutagen.easyid3 import EasyID3 from mutagen.easyid3 import EasyID3
import mutagen.mp3 from pydub import AudioSegment
import json import json
import logging import logging
@ -77,6 +77,38 @@ dict_keys(
]) ])
""" """
ID3_TAGS = {
"TALB": "album",
"TBPM": "bpm",
"TCMP": "compilation", # iTunes extension
"TCOM": "composer",
"TCOP": "copyright",
"TENC": "encodedby",
"TEXT": "lyricist",
"TLEN": "length",
"TMED": "media",
"TMOO": "mood",
"TIT1": "grouping",
"TIT2": "title",
"TIT3": "version",
"TPE1": "artist",
"TPE2": "albumartist",
"TPE3": "conductor",
"TPE4": "arranger",
"TPOS": "discnumber",
"TPUB": "organization",
"TRCK": "tracknumber",
"TOLY": "author",
"TSO2": "albumartistsort", # iTunes extension
"TSOA": "albumsort",
"TSOC": "composersort", # iTunes extension
"TSOP": "artistsort",
"TSOT": "titlesort",
"TSRC": "isrc",
"TSST": "discsubtitle",
"TLAN": "language",
}
class Download: class Download:
def __init__(self, session: requests.Session = requests.Session(), file: str = ".cache3.csv", temp: str = "temp"): def __init__(self, session: requests.Session = requests.Session(), file: str = ".cache3.csv", temp: str = "temp"):
self.session = session self.session = session
@ -123,11 +155,9 @@ class Download:
logging.info("finished") logging.info("finished")
def write_metadata(self, row, filePath): def write_metadata(self, row, filePath):
try: AudioSegment.from_file(filePath).export(filePath, format="mp3")
audiofile = EasyID3(filePath)
except mutagen.id3.ID3NoHeaderError: audiofile = EasyID3(filePath)
audiofile = mutagen.mp3.EasyMP3(filePath)
audiofile.add_tags()
valid_keys = list(EasyID3.valid_keys.keys()) valid_keys = list(EasyID3.valid_keys.keys())
@ -142,6 +172,7 @@ class Download:
audiofile.save(filePath, v1=2) audiofile.save(filePath, v1=2)
if __name__ == "__main__": if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
s = requests.Session() s = requests.Session()