-
Notifications
You must be signed in to change notification settings - Fork 1
/
coverart_search_tracks.py
221 lines (176 loc) · 6.94 KB
/
coverart_search_tracks.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
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
# -*- Mode: python; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; -*-
#
# Copyright (C) 2012 - fossfreedom
# Copyright (C) 2012 - Agustin Carrasco
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of thie GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
import base64
from mimetypes import MimeTypes
import os
import tempfile
import importlib
from gi.repository import RB
from gi.repository import Gio
from PIL import Image
def mutagen_library(module_name):
module = None
def lookfor(library):
if module_name == "":
return library
else:
return library + "." + module_name
try:
module = importlib.import_module(lookfor('mutagen'))
except ImportError:
module = importlib.import_module(lookfor('mutagenx'))
return module
IGNORED_SCHEMES = ('http', 'cdda', 'daap', 'mms')
def anyTrue(pred, seq):
"""Returns True if a True predicate is found, False
otherwise. Quits as soon as the first True is found
"""
return True in map(pred, seq)
class CoverArtTracks(object):
def __init__(self):
pass
def get_mimetype(self, art_location):
mime = MimeTypes()
mimetype = mime.guess_type(art_location)
return mimetype[0]
def embed_ogg(self, art_location, search, mimetypestr):
"""
:art_location: file path to the picture to embed
:search: file path to the music track file
:mimetypestr: mimetype of the picture to embed
"""
try:
module = mutagen_library('oggvorbis')
o = module.OggVorbis(search)
# lets get all tags into a dict
# lets also remove the deprecated coverart tag and any
# old pictures
tags = {}
for orig, values in list(o.tags.items()):
if orig.lower() != 'coverart' and \
orig.lower() != 'metadata_block_picture':
tags[orig] = values
module = mutagen_library('flac')
image = module.Picture()
image.type = 3 # Cover image
image.data = open(art_location, "rb").read()
image.mime = mimetypestr
image.desc = 'cover description'
tags.setdefault("METADATA_BLOCK_PICTURE",
[]).append(base64.b64encode(image.write()))
o.tags.update(tags)
o.save()
except:
pass
def embed_flac(self, art_location, search, mimetypestr):
"""
:art_location: file path to the picture to embed
:search: file path to the music track file
:mimetypestr: mimetype of the picture to embed
"""
try:
module = mutagen_library('')
music = module.File(search)
# lets remove any old pictures
music.clear_pictures()
module = mutagen_library('flac')
image = module.Picture()
image.type = 3 # Cover image
image.data = open(art_location, "rb").read()
image.mime = mimetypestr
image.desc = 'cover description'
music.add_picture(image)
music.save()
except:
pass
def embed_mp4(self, art_location, search, mimetypestr):
"""
:art_location: file path to the picture to embed
:search: file path to the music track file
:mimetypestr: mimetype of the picture to embed
"""
try:
module = mutagen_library('mp4')
music = module.MP4(search)
covr = []
data = open(art_location, "rb").read()
if mimetypestr == "image/jpeg":
covr.append(module.MP4Cover(data, module.MP4Cover.FORMAT_JPEG))
elif mimetypestr == "image/png":
covr.append(module.MP4Cover(data, module.MP4Cover.FORMAT_PNG))
if covr:
music.tags[b"covr"] = covr
music.save()
except:
pass
def embed_mp3(self, art_location, search, mimetypestr):
"""
:art_location: file path to the picture to embed
:search: file path to the music track file
:mimetypestr: mimetype of the picture to embed
"""
try:
module = mutagen_library('id3')
music = module.ID3(search)
# lets remove any old pictures
music.delall('APIC')
music.add(module.APIC(encoding=0, mime=mimetypestr, type=3,
desc='', data=open(art_location, "rb").read()))
music.save()
except:
pass
def embed(self, track_uri, key, resize=-1):
"""
embed tracks with the coverart
:track_uri: nominally RB.RhythmDBPropType.LOCATION
:key: RB.ExtDBKey containing the lookup for the cover to apply
:resize: int this is the size of the embedded image to resize to
returns True or False depending if the routine completed successfully
"""
the = anyTrue # for readability
art_location = RB.ExtDB(name='album-art').lookup(key)
if not isinstance(art_location, str):
art_location = art_location[0]
if not art_location:
print("not a valid key to a file containing art")
return False
image = Image.open(art_location)
f, art_location = tempfile.mkstemp(suffix=".jpg")
print("resizing?")
print(resize)
if resize > 0:
tosave = image.resize((resize, resize), Image.ANTIALIAS)
else:
tosave = image
print(art_location)
tosave.save(art_location)
search = Gio.file_new_for_uri(track_uri)
if search.get_uri_scheme() in IGNORED_SCHEMES:
print('not a valid scheme %s' % (search.get_uri()))
return False
if search.get_path().lower().endswith('.ogg'):
self.embed_ogg(art_location, search.get_path(), 'image/jpeg')
if search.get_path().lower().endswith('.flac'):
self.embed_flac(art_location, search.get_path(), 'image/jpeg')
if search.get_path().lower().endswith('.mp3'):
self.embed_mp3(art_location, search.get_path(), 'image/jpeg')
if the(search.get_path().lower().endswith, (".m4a", ".m4b", ".m4p", ".mp4")):
self.embed_mp4(art_location, search.get_path(), 'image/jpeg')
os.remove(art_location)
return True