-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.py
62 lines (57 loc) · 2.72 KB
/
metadata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Handle metadata for files"""
from mutagen.id3 import ID3
from mutagen.flac import FLAC
from mutagen.oggvorbis import OggVorbis
from mutagen.mp4 import MP4
import mimetypes
class MusicFile:
def __init__(self, track):
self.track = track
self.suffix = mimetypes.guess_extension(mimetypes.guess_type(track)[0])
#TODO: figure out issue with .m4a
def get_album(self):
if self.suffix == ".mp3" and len(ID3(self.track).getall("TALB")) > 0:
return ID3(self.track).getall("TALB")[0][0]
elif self.suffix == ".flac" and len(FLAC(self.track)["album"]) > 0:
return FLAC(self.track)["album"][0]
elif self.suffix == ".ogg" and len(OggVorbis(self.track)["album"]) > 0:
return OggVorbis(self.track)["album"][0]
elif self.suffix == ".m4a" and len(MP4(self.track)["\xa9alb"]) > 0:
return MP4(self.track)["\xa9alb"][0] #ewwww
else:
return "Unknown"
def get_artist(self):
if self.suffix == ".mp3" and len(ID3(self.track).getall("TPE1")) > 0:
return ID3(self.track).getall("TPE1")[0][0]
elif self.suffix == ".flac" and len(FLAC(self.track)["artist"]) > 0:
return FLAC(self.track)["artist"][0]
elif self.suffix == ".ogg" and len(OggVorbis(self.track)["artist"]) > 0:
return OggVorbis(self.track)["artist"][0]
elif self.suffix == ".m4a" and len(MP4(self.track)["\xa9ART"]):
return MP4(self.track)["\xa9ART"][0] #ewwww
else:
return "Unknown"
def get_title(self):
if self.suffix == ".mp3" and len(ID3(self.track).getall("TIT2")) > 0:
return ID3(self.track).getall("TIT2")[0][0]
elif self.suffix == ".flac" and len(FLAC(self.track)["title"]) > 0:
return FLAC(self.track)["title"][0]
elif self.suffix == ".ogg" and len(OggVorbis(self.track)["title"]) > 0:
return OggVorbis(self.track)["title"][0]
elif self.suffix == ".m4a" and len(MP4(self.track)["\xa9nam"]):
return MP4(self.track)["\xa9nam"][0] #ewwww
else:
return "Unknown"
def get_number(self):
if self.suffix == ".mp3" and len(ID3(self.track).getall("TRCK")) > 0:
return ID3(self.track).getall("TRCK")[0][0]
elif self.suffix == ".flac" and
len(FLAC(self.track)["tracknumber"]) > 0:
return FLAC(self.track)["tracknumber"][0]
elif self.suffix == ".ogg" and
len(OggVorbis(self.track)["tracknumber"]) > 0:
return OggVorbis(self.track)["tracknumber"][0]
elif self.suffix == ".m4a" and len(MP4(self.track)["trkn"]):
return MP4(self.track)["trkn"][0][0] #ewwww
else:
return "Unknown"