Added "ifempty" config to embedart plugin, re: issue 1020

This commit is contained in:
kerobaros 2014-10-23 15:27:15 -05:00
parent e9ae40d1a3
commit 75b62b723f

View file

@ -41,6 +41,7 @@ class EmbedCoverArtPlugin(BeetsPlugin):
'maxwidth': 0,
'auto': True,
'compare_threshold': 0,
'ifempty': False
})
if self.config['maxwidth'].get(int) and not ArtResizer.shared.local:
@ -63,13 +64,14 @@ class EmbedCoverArtPlugin(BeetsPlugin):
)
maxwidth = config['embedart']['maxwidth'].get(int)
compare_threshold = config['embedart']['compare_threshold'].get(int)
ifempty = config['embedart']['ifempty'].get()
def embed_func(lib, opts, args):
if opts.file:
imagepath = normpath(opts.file)
for item in lib.items(decargs(args)):
embed_item(item, imagepath, maxwidth, None,
compare_threshold)
compare_threshold, ifempty)
else:
for album in lib.albums(decargs(args)):
embed_album(album, maxwidth)
@ -108,13 +110,23 @@ def album_imported(lib, album):
def embed_item(item, imagepath, maxwidth=None, itempath=None,
compare_threshold=0):
compare_threshold=0, ifempty=False):
"""Embed an image into the item's media file.
"""
if compare_threshold:
if not check_art_similarity(item, imagepath, compare_threshold):
log.warn(u'Image not similar; skipping.')
return
if ifempty:
with NamedTemporaryFile(delete=True) as f:
art = extract(f.name, item)
if not art: continue
else:
log.debug(u'embedart: media file contained art already {0}'.format(
displayable_path(imagepath)
))
return
try:
log.debug(u'embedart: embedding {0}'.format(
displayable_path(imagepath)
@ -148,7 +160,8 @@ def embed_album(album, maxwidth=None, quiet=False):
for item in album.items():
embed_item(item, imagepath, maxwidth, None,
config['embedart']['compare_threshold'].get(int))
config['embedart']['compare_threshold'].get(int),
config['embedart']['ifempty'])
def check_art_similarity(item, imagepath, compare_threshold):