-
Notifications
You must be signed in to change notification settings - Fork 176
/
metadata.py
142 lines (115 loc) · 4.58 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import logging
from mutagen.flac import FLAC, Picture
from mutagen.mp3 import EasyMP3
logger = logging.getLogger(__name__)
def get_title(track_dict):
title = track_dict["title"]
version = track_dict.get("version")
if version:
title = f"{title} ({version})"
# for classical works
if track_dict.get("work"):
title = "{}: {}".format(track_dict["work"], title)
return title
# Use KeyError catching instead of dict.get to avoid empty tags
def tag_flac(filename, root_dir, final_name, d, album, istrack=True, em_image=False):
"""
Tag a FLAC file
:param str filename: FLAC file path
:param str root_dir: Root dir used to get the cover art
:param str final_name: Final name of the FLAC file (complete path)
:param dict d: Track dictionary from Qobuz_client
:param dict album: Album dictionary from Qobuz_client
:param bool istrack
:param bool em_image: Embed cover art into file
"""
audio = FLAC(filename)
audio["TITLE"] = get_title(d)
audio["TRACKNUMBER"] = str(d["track_number"]) # TRACK NUMBER
if "Disc " in final_name:
audio["DISCNUMBER"] = str(d["media_number"])
try:
audio["COMPOSER"] = d["composer"]["name"] # COMPOSER
except KeyError:
pass
try:
audio["ARTIST"] = d["performer"]["name"] # TRACK ARTIST
except KeyError:
if istrack:
audio["ARTIST"] = d["album"]["artist"]["name"] # TRACK ARTIST
else:
audio["ARTIST"] = album["artist"]["name"]
try:
audio["LABEL"] = album["label"]["name"]
except KeyError:
pass
if istrack:
audio["GENRE"] = ", ".join(d["album"]["genres_list"]) # GENRE
audio["ALBUMARTIST"] = d["album"]["artist"]["name"] # ALBUM ARTIST
audio["TRACKTOTAL"] = str(d["album"]["tracks_count"]) # TRACK TOTAL
audio["ALBUM"] = d["album"]["title"] # ALBUM TITLE
audio["DATE"] = d["album"]["release_date_original"]
else:
audio["GENRE"] = ", ".join(album["genres_list"]) # GENRE
audio["ALBUMARTIST"] = album["artist"]["name"] # ALBUM ARTIST
audio["TRACKTOTAL"] = str(album["tracks_count"]) # TRACK TOTAL
audio["ALBUM"] = album["title"] # ALBUM TITLE
audio["DATE"] = album["release_date_original"]
if em_image:
emb_image = os.path.join(root_dir, "cover.jpg")
multi_emb_image = os.path.join(
os.path.abspath(os.path.join(root_dir, os.pardir)), "cover.jpg"
)
cover_image = emb_image if os.path.isfile(emb_image) else multi_emb_image
try:
image = Picture()
image.type = 3
image.mime = "image/jpeg"
image.desc = "cover"
with open(cover_image, "rb") as img:
image.data = img.read()
audio.add_picture(image)
except Exception as e:
logger.error(f"Error embedding image: {e}", exc_info=True)
audio.save()
os.rename(filename, final_name)
def tag_mp3(filename, root_dir, final_name, d, album, istrack=True, em_image=False):
"""
Tag a mp3 file
:param str filename: mp3 file path
:param str root_dir: Root dir used to get the cover art
:param str final_name: Final name of the mp3 file (complete path)
:param dict d: Track dictionary from Qobuz_client
:param bool istrack: Embed cover art into file
:param bool em_image: Embed cover art into file
"""
# TODO: add embedded cover art support for mp3
audio = EasyMP3(filename)
audio["title"] = get_title(d)
audio["tracknumber"] = str(d["track_number"])
if "Disc " in final_name:
audio["discnumber"] = str(d["media_number"])
try:
audio["composer"] = d["composer"]["name"]
except KeyError:
pass
try:
audio["artist"] = d["performer"]["name"] # TRACK ARTIST
except KeyError:
if istrack:
audio["artist"] = d["album"]["artist"]["name"] # TRACK ARTIST
else:
audio["artist"] = album["artist"]["name"]
if istrack:
audio["genre"] = ", ".join(d["album"]["genres_list"]) # GENRE
audio["albumartist"] = d["album"]["artist"]["name"] # ALBUM ARTIST
audio["album"] = d["album"]["title"] # ALBUM TITLE
audio["date"] = d["album"]["release_date_original"]
else:
audio["genre"] = ", ".join(album["genres_list"]) # GENRE
audio["albumartist"] = album["artist"]["name"] # ALBUM ARTIST
audio["album"] = album["title"] # ALBUM TITLE
audio["date"] = album["release_date_original"]
audio.save()
os.rename(filename, final_name)