-
Notifications
You must be signed in to change notification settings - Fork 0
/
filetypes.py
75 lines (57 loc) · 1.67 KB
/
filetypes.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
63
64
65
66
67
68
69
70
71
72
73
74
75
from collections import namedtuple
from mutagen.mp3 import MP3
from mutagen.mp3 import BitrateMode
from mutagen.oggvorbis import OggVorbis
from mutagen.easyid3 import EasyID3
from mutagen.flac import FLAC
MusicTag = namedtuple(
"MusicTag",
["title", "title_number", "album", "artist", "album_artist"]
)
MusicFile = namedtuple(
"MusicFile",
["path", "file", "bitrate", "tag"]
)
types = dict()
types["mp3"] = dict()
types["mp3"]["ext"] = ".mp3"
types["mp3"]["dir"] = "MP3"
types["flac"] = dict()
types["flac"]["ext"] = ".flac"
types["flac"]["dir"] = "FLAC"
types["ogg"] = dict()
types["ogg"]["ext"] = ".ogg"
types["ogg"]["dir"] = "OGG"
def get_file_type(f: str):
out_type = None
for t in types:
if f.endswith(types[t]["ext"]):
out_type = t
return out_type
def get_file_bitrate(f):
bitrate = -1
if f.endswith(types["mp3"]["ext"]):
bitrate_mode = MP3(f).info.bitrate_mode
if bitrate_mode == BitrateMode.VBR:
bitrate = -1
else:
bitrate = MP3(f).info.bitrate
elif f.endswith(types["ogg"]["ext"]):
bitrate = OggVorbis(f).info.bitrate
return bitrate
def get_file_tag(f):
system_tag = None
if f.endswith(types["mp3"]["ext"]):
system_tag = EasyID3(f)
elif f.endswith(types["flac"]["ext"]):
system_tag = FLAC(f)
elif f.endswith(types["ogg"]["ext"]):
system_tag = OggVorbis(f)
out_tag = MusicTag(
title=system_tag["title"][0],
title_number=system_tag["tracknumber"][0],
album=system_tag["album"][0],
artist=system_tag["artist"][0],
album_artist=system_tag["albumartist"][0]
)
return out_tag