From 75b62b723fa796b393d068a1e64e6efcf7c9628f Mon Sep 17 00:00:00 2001 From: kerobaros Date: Thu, 23 Oct 2014 15:27:15 -0500 Subject: [PATCH 1/5] Added "ifempty" config to embedart plugin, re: issue 1020 --- beetsplug/embedart.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index 83c64f004..3ff9cc69d 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -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): From e06f492f9124870f6692ce77ece6374aba40644d Mon Sep 17 00:00:00 2001 From: kerobaros Date: Thu, 23 Oct 2014 15:31:38 -0500 Subject: [PATCH 2/5] Whoops, take two. --- beetsplug/embedart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index 3ff9cc69d..bd14ad5e9 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -120,7 +120,7 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None, if ifempty: with NamedTemporaryFile(delete=True) as f: art = extract(f.name, item) - if not art: continue + if not art: pass else: log.debug(u'embedart: media file contained art already {0}'.format( displayable_path(imagepath) From 9be753736b9f29f3a0920ff2c5ffa784d916f7af Mon Sep 17 00:00:00 2001 From: kerobaros Date: Thu, 23 Oct 2014 21:13:30 -0500 Subject: [PATCH 3/5] Slight style tweaks. --- beetsplug/embedart.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index bd14ad5e9..0a9b3a199 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -120,8 +120,9 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None, if ifempty: with NamedTemporaryFile(delete=True) as f: art = extract(f.name, item) - if not art: pass - else: + if not art: + pass + else: log.debug(u'embedart: media file contained art already {0}'.format( displayable_path(imagepath) )) From 7f37514bb9c241333c39ed1d766986f3ed8613fa Mon Sep 17 00:00:00 2001 From: kerobaros Date: Thu, 23 Oct 2014 21:17:42 -0500 Subject: [PATCH 4/5] Whoops, trailing space. Take 2. --- beetsplug/embedart.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index 0a9b3a199..aae2382ae 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -120,7 +120,7 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None, if ifempty: with NamedTemporaryFile(delete=True) as f: art = extract(f.name, item) - if not art: + if not art: pass else: log.debug(u'embedart: media file contained art already {0}'.format( From ca431b0bf8f26ed51a2d48a0cc83f608cd16e37b Mon Sep 17 00:00:00 2001 From: kerobaros Date: Thu, 23 Oct 2014 22:37:19 -0500 Subject: [PATCH 5/5] Seperated some code out from extract() into get_art(item). Cleaner and quicker way to check for embedded art. --- beetsplug/embedart.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/beetsplug/embedart.py b/beetsplug/embedart.py index aae2382ae..1112f6d69 100644 --- a/beetsplug/embedart.py +++ b/beetsplug/embedart.py @@ -118,8 +118,7 @@ def embed_item(item, imagepath, maxwidth=None, itempath=None, log.warn(u'Image not similar; skipping.') return if ifempty: - with NamedTemporaryFile(delete=True) as f: - art = extract(f.name, item) + art = get_art(item) if not art: pass else: @@ -213,13 +212,7 @@ def _mediafile_image(image_path, maxwidth=None): return mediafile.Image(data, type=mediafile.ImageType.front) -# 'extractart' command. - -def extract(outpath, item): - if not item: - log.error(u'No item matches query.') - return - +def get_art(item): # Extract the art. try: mf = mediafile.MediaFile(syspath(item.path)) @@ -229,7 +222,18 @@ def extract(outpath, item): )) return - art = mf.art + return mf.art + +# 'extractart' command. + + +def extract(outpath, item): + if not item: + log.error(u'No item matches query.') + return + + art = get_art(item) + if not art: log.error(u'No album art present in {0} - {1}.' .format(item.artist, item.title))