forked from Neon-MT/NisWave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_image.py
More file actions
74 lines (52 loc) · 2.46 KB
/
Copy pathupdate_image.py
File metadata and controls
74 lines (52 loc) · 2.46 KB
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
import os
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC
from PIL import Image
def get_cover_art(mp3_file_path, size=640, output_dir=os.path.join(os.path.dirname(__file__), "temp_cover_art")):
"""
Extracts the cover art from an MP3 file using the mutagen library.
"""
print(mp3_file_path, output_dir)
try:
# Load the MP3 file with mutagen
audio = MP3(mp3_file_path)
# Check if there are any ID3 tags
if not audio.tags:
print(f"No ID3 tags found in {mp3_file_path}")
return
# Iterate over the tags to find the album art (APIC tag)
for tag in audio.tags.getall('APIC'):
if isinstance(tag, APIC):
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Determine file extension based on image mime type
if tag.mime == 'image/jpeg':
ext = 'jpg'
elif tag.mime == 'image/png':
ext = 'png'
else:
print(f"Unsupported image mime type: {tag.mime}")
continue
# Create the output filename
track_title = audio.get('TIT2', ['temp'])[0]
output_filename = f"{track_title.replace('/', '_')}_cover.{ext}"
output_path = os.path.join(output_dir, output_filename)
# Write the image data to a file
with open(output_path, 'wb') as img_file:
img_file.write(tag.data)
print(f"Successfully extracted cover art to: {output_path}")
img = Image.open(output_path)
width, height = img.size
img = img.resize((int(width*(size/height)), size), Image.Resampling.LANCZOS)
img.save(output_path)
return [int(width*(size/height)), size], output_path
print(f"No cover art (APIC tag) found in {mp3_file_path}")
except Exception as e:
print(f"No image found using default")
output_path = os.path.join(os.path.dirname(__file__), "assets/default_cover.jpg")
img = Image.open(output_path)
width, height = img.size
img = img.resize((int(width*(size/height)), size), Image.Resampling.LANCZOS)
img.save(output_path)
return [size, size], output_path