Merge pull request #1333 from brunal/improve-art-detection

Improve art detection
This commit is contained in:
Adrian Sampson 2015-02-28 19:12:52 -05:00
commit a4dd46dea3
3 changed files with 32 additions and 11 deletions

View file

@ -1246,18 +1246,31 @@ class DateItemField(MediaField):
class CoverArtField(MediaField):
"""A descriptor that provides access to the *raw image data* for the
first image on a file. This is used for backwards compatibility: the
cover image on a file. This is used for backwards compatibility: the
full `ImageListField` provides richer `Image` objects.
When there are multiple images we try to pick the most likely to be a front
cover.
"""
def __init__(self):
pass
def __get__(self, mediafile, _):
try:
return mediafile.images[0].data
except IndexError:
candidates = mediafile.images
if candidates:
return self.guess_cover_image(candidates).data
else:
return None
@staticmethod
def guess_cover_image(candidates):
if len(candidates) == 1:
return candidates[0]
try:
return next(c for c in candidates if c.type == ImageType.front)
except StopIteration:
return candidates[0]
def __set__(self, mediafile, data):
if data:
mediafile.images = [Image(data=data)]

View file

@ -124,11 +124,8 @@ class EmbedartCliTest(_common.TestCase, TestHelper):
self.run_command('extractart', '-n', 'extracted')
path = os.path.join(albumpath.decode('utf-8'), 'extracted.')
found = any(map(lambda ext: os.path.exists(path + ext),
['png', 'jpeg', 'jpg']))
self.assertTrue(found, 'Extracted image was not found!')
self.assertExists(os.path.join(albumpath.decode('utf-8'),
'extracted.png'))
class EmbedartTest(unittest.TestCase):

View file

@ -28,7 +28,7 @@ from test import _common
from test._common import unittest
from beets.mediafile import MediaFile, MediaField, Image, \
MP3DescStorageStyle, StorageStyle, MP4StorageStyle, \
ASFStorageStyle, ImageType
ASFStorageStyle, ImageType, CoverArtField
from beets.library import Item
from beets.plugins import BeetsPlugin
@ -161,6 +161,13 @@ class ImageStructureTestMixin(ArtTestMixin):
mediafile = MediaFile(mediafile.path)
self.assertEqual(len(mediafile.images), 0)
def test_guess_cover(self):
mediafile = self._mediafile_fixture('image')
self.assertEqual(len(mediafile.images), 2)
cover = CoverArtField.guess_cover_image(mediafile.images)
self.assertEqual(cover.desc, 'album cover')
self.assertEqual(mediafile.art, cover.data)
def assertExtendedImageAttributes(self, image, **kwargs):
"""Ignore extended image attributes in the base tests.
"""
@ -662,7 +669,7 @@ class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin,
errors.append('Tag %s does not exist' % key)
else:
if value2 != value:
errors.append('Tag %s: %s != %s' % (key, value2, value))
errors.append('Tag %s: %r != %r' % (key, value2, value))
if any(errors):
errors = ['Tags did not match'] + errors
self.fail('\n '.join(errors))
@ -758,6 +765,10 @@ class MP4Test(ReadWriteTestBase, PartialTestMixin,
with self.assertRaises(ValueError):
mediafile.images = [Image(data=self.tiff_data)]
def test_guess_cover(self):
# There is no metadata associated with images, we pick one at random
pass
class AlacTest(ReadWriteTestBase, unittest.TestCase):
extension = 'alac.m4a'