From 322038550a5842e11cf6702da7728bedc49b8bb4 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sun, 10 Apr 2011 14:44:25 +0200 Subject: [PATCH 1/5] Add support for albumart in MediaFile --- beets/mediafile.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/beets/mediafile.py b/beets/mediafile.py index 7e0328985..0f7a4f4e4 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -663,7 +663,7 @@ class MediaFile(object): etc = StorageStyle('compilation') ) albumartist = MediaField( - mp3 = StorageStyle('TXXX', id3_desc='Album Artist'), + mp3 = StorageStyle('TXXX', id3_desc=u'Album Artist'), mp4 = StorageStyle( '----:com.apple.iTunes:Album Artist', as_type=str), @@ -671,12 +671,23 @@ class MediaFile(object): StorageStyle('albumartist')] ) albumtype = MediaField( - mp3 = StorageStyle('TXXX', id3_desc='MusicBrainz Album Type'), + mp3 = StorageStyle('TXXX', id3_desc=u'MusicBrainz Album Type'), mp4 = StorageStyle( '----:com.apple.iTunes:MusicBrainz Album Type', as_type=str), etc = StorageStyle('musicbrainz_albumtype') ) + albumart = MediaField( + mp3 = StorageStyle('APIC', id3_desc=u'Cover'), + ) + albumart_mime = MediaField( + mp3 = StorageStyple('APIC', mime=u''), + ) + albumart_data = MediaField( + mp3 = StorageStyple('APIC', data=u''), + mp4 = StorageStyle('covr'), + etc = StorageStyle('picture') + ) # MusicBrainz IDs. mb_trackid = MediaField( @@ -688,14 +699,14 @@ class MediaFile(object): etc = StorageStyle('musicbrainz_trackid') ) mb_albumid = MediaField( - mp3 = StorageStyle('TXXX', id3_desc='MusicBrainz Album Id'), + mp3 = StorageStyle('TXXX', id3_desc=u'MusicBrainz Album Id'), mp4 = StorageStyle( '----:com.apple.iTunes:MusicBrainz Album Id', as_type=str), etc = StorageStyle('musicbrainz_albumid') ) mb_artistid = MediaField( - mp3 = StorageStyle('TXXX', id3_desc='MusicBrainz Artist Id'), + mp3 = StorageStyle('TXXX', id3_desc=u'MusicBrainz Artist Id'), mp4 = StorageStyle( '----:com.apple.iTunes:MusicBrainz Artist Id', as_type=str), @@ -703,7 +714,7 @@ class MediaFile(object): ) mb_albumartistid = MediaField( mp3 = StorageStyle('TXXX', - id3_desc='MusicBrainz Album Artist Id'), + id3_desc=u'MusicBrainz Album Artist Id'), mp4 = StorageStyle( '----:com.apple.iTunes:MusicBrainz Album Artist Id', as_type=str), From 0628d8df0eb59e1b47d8bc01cb9c3bd566829d6e Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sun, 10 Apr 2011 14:51:01 +0200 Subject: [PATCH 2/5] Add initial version of the embed coverart plugin. This plugin allows users to embed the cover into the audio file. Probaly still has a few bugs but it should work in most cases right now. --- beetsplug/embedcoverart.py | 45 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 beetsplug/embedcoverart.py diff --git a/beetsplug/embedcoverart.py b/beetsplug/embedcoverart.py new file mode 100644 index 000000000..75ffafba8 --- /dev/null +++ b/beetsplug/embedcoverart.py @@ -0,0 +1,45 @@ +from beets.plugins import BeetsPlugin +from beets import mediafile + +import os, logging + +from email.mime.image import MIMEImage + +log = logging.getLogger('beets') +log.addHandler(logging.StreamHandler()) + + +class EmbedAlbumartPlugin(BeetsPlugin): + '''Allows albumart to be embedded into the actual files''' + + def __init__(self): + self.register_listener('loaded', self.loaded) + self.register_listener('album_imported', self.album_imported) + + def configure(self, config): + pass + + def loaded(self): + pass + + def album_imported(self, album): + albumart = album.artpath + ALLOWED_MIMES = ('jpeg','png') + + if albumart: + albumart_raw = open(albumart, 'rb').read() + img = MIMEImage(albumart_raw) + mime_img = img.get_content_subtype() + + if mime_img in ALLOWED_MIMES: + mime_type = 'image/%s' % mime_img + + for item in album.items(): + f = mediafile.MediaFile(item) + + if "mp3" in item.type: + f.albumart_mime = mime_type + + f.albumart_data = albumart_raw + f.save() + From 1859375593f61d57ef761b151664f45f557bf5da Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sun, 10 Apr 2011 14:54:32 +0200 Subject: [PATCH 3/5] Only try to add albumart if the MIME-type is correct, log if it's not. --- beetsplug/embedcoverart.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/beetsplug/embedcoverart.py b/beetsplug/embedcoverart.py index 75ffafba8..4a5b3a5a6 100644 --- a/beetsplug/embedcoverart.py +++ b/beetsplug/embedcoverart.py @@ -34,12 +34,13 @@ class EmbedAlbumartPlugin(BeetsPlugin): if mime_img in ALLOWED_MIMES: mime_type = 'image/%s' % mime_img - for item in album.items(): - f = mediafile.MediaFile(item) - - if "mp3" in item.type: - f.albumart_mime = mime_type - - f.albumart_data = albumart_raw - f.save() - + for item in album.items(): + f = mediafile.MediaFile(item) + + if "mp3" in item.type: + f.albumart_mime = mime_type + + f.albumart_data = albumart_raw + f.save() + else: + log.error('Sorry, a file of type %s is not allowed as a coverart' % mime_img) From 00c5a275a4c9c191a53c0363e3e186c3ca26dbd6 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sun, 10 Apr 2011 16:12:21 +0200 Subject: [PATCH 4/5] *facepalm* and fix some Engrish. --- beetsplug/embedcoverart.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/beetsplug/embedcoverart.py b/beetsplug/embedcoverart.py index 4a5b3a5a6..d3b59f3a2 100644 --- a/beetsplug/embedcoverart.py +++ b/beetsplug/embedcoverart.py @@ -9,8 +9,8 @@ log = logging.getLogger('beets') log.addHandler(logging.StreamHandler()) -class EmbedAlbumartPlugin(BeetsPlugin): - '''Allows albumart to be embedded into the actual files''' +class EmbedCoverArtPlugin(BeetsPlugin): + '''Allows albumart to be embedded into the actual files.''' def __init__(self): self.register_listener('loaded', self.loaded) @@ -43,4 +43,4 @@ class EmbedAlbumartPlugin(BeetsPlugin): f.albumart_data = albumart_raw f.save() else: - log.error('Sorry, a file of type %s is not allowed as a coverart' % mime_img) + log.error('Sorry, a file of type %s is not allowed as coverart.' % mime_img) From b67e24fa814a81ed175aa4b3fd898e92fe4e98e7 Mon Sep 17 00:00:00 2001 From: Daniele Sluijters Date: Sun, 10 Apr 2011 16:26:39 +0200 Subject: [PATCH 5/5] Specify the type of covr. --- beets/mediafile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beets/mediafile.py b/beets/mediafile.py index 0f7a4f4e4..12df16e41 100644 --- a/beets/mediafile.py +++ b/beets/mediafile.py @@ -685,7 +685,7 @@ class MediaFile(object): ) albumart_data = MediaField( mp3 = StorageStyple('APIC', data=u''), - mp4 = StorageStyle('covr'), + mp4 = StorageStyle('covr', as_type=str), etc = StorageStyle('picture') )