Module for embedding metadata into audio files using Mutagen.
embed_metadata(
output_file=Path("test.mp3"),
song=song_object,
file_format="mp3",
)
Bases: Exception
Base class for all exceptions related to metadata and id3 embedding.
Embed the album art in the audio file.
- audio_file: Audio file object.
- song: Song object.
Source code in spotdl/utils/metadata.py
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 | def embed_cover(audio_file, song: Song, encoding: str):
"""
Embed the album art in the audio file.
### Arguments
- audio_file: Audio file object.
- song: Song object.
"""
if not song.cover_url:
return audio_file
# Try to download the cover art
try:
cover_data = requests.get(song.cover_url, timeout=10).content
except Exception:
return audio_file
# Create the image object for the file type
if encoding in ["flac", "ogg", "opus"]:
picture = Picture()
picture.type = 3
picture.desc = "Cover"
picture.mime = "image/jpeg"
picture.data = cover_data
if encoding in ["ogg", "opus"]:
image_data = picture.write()
encoded_data = base64.b64encode(image_data)
vcomment_value = encoded_data.decode("ascii")
audio_file["metadata_block_picture"] = [vcomment_value]
elif encoding == "flac":
audio_file.add_picture(picture)
elif encoding == "m4a":
audio_file[M4A_TAG_PRESET["albumart"]] = [
MP4Cover(
cover_data,
imageformat=MP4Cover.FORMAT_JPEG,
)
]
elif encoding == "mp3":
audio_file["APIC"] = APIC(
encoding=3,
mime="image/jpeg",
type=3,
desc="Cover",
data=cover_data,
)
return audio_file
|
Detect lyrics type (lrc or txt) and embed them in the audio file.
- audio_file: Audio file object.
- song: Song object.
- encoding: Encoding type.
Source code in spotdl/utils/metadata.py
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343 | def embed_lyrics(audio_file, song: Song, encoding: str):
"""
Detect lyrics type (lrc or txt) and embed them in the audio file.
### Arguments
- audio_file: Audio file object.
- song: Song object.
- encoding: Encoding type.
"""
lyrics = song.lyrics
if not lyrics:
return audio_file
tag_preset = TAG_PRESET if encoding != "m4a" else M4A_TAG_PRESET
# Check if the lyrics are in lrc format
# using regex on the first 5 lines
lrc_lines = lyrics.splitlines()[:5]
lrc_lines = [line for line in lrc_lines if line and LRC_REGEX.match(line)]
if len(lrc_lines) == 0:
# Lyrics are not in lrc format
# Embed them normally
if encoding == "mp3":
audio_file.add(USLT(encoding=Encoding.UTF8, text=song.lyrics))
else:
audio_file[tag_preset["lyrics"]] = song.lyrics
else:
# Lyrics are in lrc format
# Embed them as SYLT id3 tag
if encoding == "mp3":
lrc_data = []
for line in lyrics.splitlines():
time_tag = line.split("]", 1)[0] + "]"
text = line.replace(time_tag, "")
time_tag = time_tag.replace("[", "")
time_tag = time_tag.replace("]", "")
time_tag = time_tag.replace(".", ":")
time_tag_vals = time_tag.split(":")
if len(time_tag_vals) != 3 or any(
not isinstance(tag, int) for tag in time_tag_vals
):
continue
minute, sec, millisecond = time_tag_vals
time = to_ms(min=minute, sec=sec, ms=millisecond)
lrc_data.append((text, time))
audio_file.add(USLT(encoding=3, text=song.lyrics))
audio_file.add(
SYLT(encoding=Encoding.UTF8, text=lrc_data, format=2, type=1)
)
else:
audio_file[tag_preset["lyrics"]] = song.lyrics
return audio_file
|
Set ID3 tags for generic files (FLAC, OPUS, OGG)
- output_file: Path to the output file.
- song: Song object.
Source code in spotdl/utils/metadata.py
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 | def embed_metadata(output_file: Path, song: Song, id3_separator: str = "/"):
"""
Set ID3 tags for generic files (FLAC, OPUS, OGG)
### Arguments
- output_file: Path to the output file.
- song: Song object.
"""
# Get the file extension for the output file
encoding = output_file.suffix[1:]
# Get the tag preset for the file extension
tag_preset = TAG_PRESET if encoding != "m4a" else M4A_TAG_PRESET
try:
audio_file = File(str(output_file.resolve()), easy=encoding == "mp3")
if audio_file is None:
raise MetadataError(
f"Unrecognized file format for {output_file} from {song.url}"
)
except Exception as exc:
raise MetadataError("Unable to load file.") from exc
# Embed basic metadata
audio_file[tag_preset["artist"]] = song.artists
audio_file[tag_preset["albumartist"]] = (
song.album_artist if song.album_artist else song.artist
)
audio_file[tag_preset["title"]] = song.name
audio_file[tag_preset["date"]] = song.date
audio_file[tag_preset["encodedby"]] = song.publisher
# Embed metadata that isn't always present
album_name = song.album_name
if album_name:
audio_file[tag_preset["album"]] = album_name
if song.genres:
audio_file[tag_preset["genre"]] = song.genres[0].title()
if song.copyright_text:
audio_file[tag_preset["copyright"]] = song.copyright_text
if song.download_url and encoding != "mp3":
audio_file[tag_preset["comment"]] = song.download_url
# Embed some metadata in format specific ways
if encoding in ["flac", "ogg", "opus"]:
# Zero fill the disc and track numbers
zfilled_disc_number = str(song.disc_number).zfill(len(str(song.disc_count)))
zfilled_track_number = str(song.track_number).zfill(len(str(song.tracks_count)))
audio_file[tag_preset["discnumber"]] = zfilled_disc_number
audio_file[tag_preset["tracknumber"]] = zfilled_track_number
audio_file[tag_preset["woas"]] = song.url
elif encoding == "m4a":
audio_file[tag_preset["discnumber"]] = [(song.disc_number, song.disc_count)]
audio_file[tag_preset["tracknumber"]] = [(song.track_number, song.tracks_count)]
audio_file[tag_preset["explicit"]] = (4 if song.explicit is True else 2,)
audio_file[tag_preset["woas"]] = song.url.encode("utf-8")
elif encoding == "mp3":
audio_file["tracknumber"] = f"{str(song.track_number)}/{str(song.tracks_count)}"
audio_file["discnumber"] = f"{str(song.disc_number)}/{str(song.disc_count)}"
# Mp3 specific encoding
if encoding == "mp3":
if id3_separator != "/":
audio_file.save(v23_sep=id3_separator, v2_version=3)
else:
audio_file.save(v2_version=3)
audio_file = ID3(str(output_file.resolve()))
audio_file.add(WOAS(encoding=3, url=song.url))
if song.download_url:
audio_file.add(COMM(encoding=3, text=song.download_url))
if song.popularity:
audio_file.add(
COMM(
encoding=3,
lang="eng",
text="Spotify Popularity: " + str(song.popularity),
)
)
# Embed album art
audio_file = embed_cover(audio_file, song, encoding)
# Embed lyrics
audio_file = embed_lyrics(audio_file, song, encoding)
# Mp3 specific encoding
if encoding == "mp3":
audio_file.save(v23_sep=id3_separator, v2_version=3)
else:
audio_file.save()
|
Get song metadata.
- OSError: If the file is not found.
- MetadataError: If the file is not a valid audio file.
Source code in spotdl/utils/metadata.py
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508 | def get_file_metadata(path: Path, id3_separator: str = "/") -> Optional[Dict[str, Any]]:
"""
Get song metadata.
### Arguments
- path: Path to the song.
### Returns
- Dict of song metadata.
### Raises
- OSError: If the file is not found.
- MetadataError: If the file is not a valid audio file.
"""
if path.exists() is False:
raise OSError(f"File not found: {path}")
audio_file = File(str(path.resolve()))
if audio_file is None or audio_file == {}:
return None
song_meta: Dict[str, Any] = {}
for key in TAG_PRESET:
if path.suffix == ".m4a":
val = audio_file.get(M4A_TAG_PRESET[key])
elif path.suffix == ".mp3":
val = audio_file.get(MP3_TAG_PRESET[key])
else:
val = audio_file.get(key)
# Cover art is a special case and
# has to be handled before checking the val
# M4A is handled in the m4a section since it
# has data in the val variable
if key == "albumart":
if path.suffix == ".mp3":
cover = audio_file.get("APIC:Cover")
if cover:
song_meta["album_art"] = cover.data
else:
song_meta["album_art"] = None
continue
if path.suffix == ".flac":
song_meta["album_art"] = audio_file.pictures[0].data
continue
if path.suffix in [".ogg", ".opus"]:
pictures = audio_file.get("metadata_block_picture")
if pictures and pictures[0]:
song_meta["album_art"] = pictures[0]
else:
song_meta["album_art"] = None
continue
# If the tag is empty, skip it
if val is None:
# If the tag is empty but it's key is in the
# song object, set it to None
empty_key = TAG_TO_SONG.get(key)
if empty_key:
song_meta[empty_key] = None
continue
# MP3 specific decoding
if path.suffix == ".mp3":
if key == "woas":
song_meta["url"] = val.url
elif key == "year":
song_meta["year"] = int(str(val.text[0])[:4])
elif key == "date":
song_meta["date"] = str(val.text[0])
elif key == "tracknumber":
count = val.text[0].split(id3_separator)
if len(count) == 2:
song_meta["track_number"] = int(count[0])
song_meta["tracks_count"] = int(count[1])
else:
song_meta["track_number"] = val.text[0]
elif key == "discnumber":
count = val.text[0].split(id3_separator)
if len(count) == 2:
song_meta["disc_number"] = int(count[0])
song_meta["disc_count"] = int(count[1])
else:
song_meta["disc_number"] = val.text[0]
elif key == "artist":
artists_val: str = (
val.text[0] if isinstance(val.text, list) else val.text
)
song_meta["artists"] = artists_val.split(id3_separator)
else:
meta_key = TAG_TO_SONG.get(key)
if meta_key:
song_meta[meta_key] = (
val.text[0]
if isinstance(val.text, list) and len(val.text) == 1
else val.text
)
# M4A specific decoding
elif path.suffix == ".m4a":
if key == "artist":
song_meta["artists"] = val
elif key == "woas":
song_meta["url"] = val[0].decode("utf-8")
elif key == "explicit":
song_meta["explicit"] = val == [4] if val else None
elif key == "year":
song_meta["year"] = int(str(val[0])[:4])
elif key == "discnumber":
song_meta["disc_number"] = val[0][0]
song_meta["disc_count"] = val[0][1]
elif key == "tracknumber":
song_meta["track_number"] = val[0][0]
song_meta["tracks_count"] = val[0][1]
else:
meta_key = TAG_TO_SONG.get(key)
if meta_key:
song_meta[meta_key] = (
val[0] if isinstance(val, list) and len(val) == 1 else val
)
# FLAC, OGG, OPUS specific decoding
else:
if key == "artist":
song_meta["artists"] = val
elif key == "tracknumber":
song_meta["track_number"] = int(val[0])
elif key == "discnumber":
song_meta["disc_count"] = int(val[0])
song_meta["disc_number"] = int(val[0])
else:
meta_key = TAG_TO_SONG.get(key)
if meta_key:
song_meta[meta_key] = (
val[0] if isinstance(val, list) and len(val) == 1 else val
)
# Make sure that artists is a list
if isinstance(song_meta["artists"], str):
song_meta["artists"] = [song_meta["artists"]]
elif song_meta["artists"] is not None:
song_meta["artists"] = list(song_meta["artists"])
else:
song_meta["artists"] = []
# Make sure that genres is a list
if isinstance(song_meta["genres"], str):
song_meta["genres"] = [song_meta["genres"]]
# Add main artist to the song meta object
if song_meta["artists"]:
song_meta["artist"] = song_meta["artists"][0]
else:
song_meta["artist"] = None
return song_meta
|