[gnome-music/wip/mschraal/core-thumb-property: 11/16] Bring back embedded art support
- From: Marinus Schraal <mschraal src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-music/wip/mschraal/core-thumb-property: 11/16] Bring back embedded art support
- Date: Sun, 14 Jun 2020 22:22:58 +0000 (UTC)
commit 4f571e20182bc6d0b94fb73116dfe9696dc48812
Author: Marinus Schraal <mschraal gnome org>
Date: Sun Jun 14 23:09:36 2020 +0200
Bring back embedded art support
gnomemusic/embeddedart.py | 187 ++++++++++++++++++++++++++
gnomemusic/grilowrappers/grltrackerwrapper.py | 3 +-
2 files changed, 189 insertions(+), 1 deletion(-)
---
diff --git a/gnomemusic/embeddedart.py b/gnomemusic/embeddedart.py
new file mode 100644
index 00000000..8f2c0254
--- /dev/null
+++ b/gnomemusic/embeddedart.py
@@ -0,0 +1,187 @@
+# Copyright 2020 The GNOME Music developers
+#
+# GNOME Music is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# GNOME Music 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 GNOME Music; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# The GNOME Music authors hereby grant permission for non-GPL compatible
+# GStreamer plugins to be used and distributed together with GStreamer
+# and GNOME Music. This permission is above and beyond the permissions
+# granted by the GPL license by which GNOME Music is covered. If you
+# modify this code, you may extend this exception to your version of the
+# code, but you are not obligated to do so. If you do not wish to do so,
+# delete this exception statement from your version.
+
+import gi
+gi.require_versions({"GstPbutils": "1.0", "GstTag": "1.0", "MediaArt": "2.0"})
+from gi.repository import GLib, GObject, MediaArt, Gst, GstTag, GstPbutils
+
+from gnomemusic.albumart import AlbumArt
+from gnomemusic.corealbum import CoreAlbum
+from gnomemusic.coresong import CoreSong
+from gnomemusic.musiclogger import MusicLogger
+from gnomemusic.storeart import StoreArt
+
+
+class EmbeddedArt(GObject.GObject):
+ """Lookup local art
+
+ 1. Embedded art using GStreamer
+ 2. Available in the directory using MediaArt
+ """
+
+ _log = MusicLogger()
+
+ def __init__(self):
+ """Initialize EmbeddedArt
+ """
+ super().__init__()
+
+ try:
+ Gst.init(None)
+ GstPbutils.pb_utils_init()
+ except GLib.Error as error:
+ self._log.warning(
+ "Error: {}, {}".format(error.domain, error.message))
+ return
+
+ self._media_art = MediaArt.Process.new()
+
+ self._album = None
+ self._artist = None
+ self._coreobject = None
+ self._path = None
+
+ def query(self, coreobject):
+ """Start the local query
+
+ :param coreobject: The CoreAlbum or CoreSong to search art for
+ """
+ self._coreobject = coreobject
+
+ try:
+ if coreobject.props.url is None:
+ coreobject.props.thumbnail = "generic"
+ return
+ except AttributeError:
+ coreobject.props.thumbnail = "generic"
+ return
+
+ if isinstance(self._coreobject, CoreSong):
+ self._album = self._coreobject.props.album
+ elif isinstance(self._coreobject, CoreAlbum):
+ self._album = self._coreobject.props.title
+ self._artist = self._coreobject.props.artist
+
+ try:
+ discoverer = GstPbutils.Discoverer.new(Gst.SECOND)
+ except GLib.Error as error:
+ self._log.warning(
+ "Error: {}, {}".format(error.domain, error.message))
+ self._lookup_cover_in_directory()
+ return
+
+ discoverer.connect('discovered', self._discovered)
+ discoverer.start()
+
+ success, path = MediaArt.get_path(self._artist, self._album, "album")
+
+ if not success:
+ self._coreobject.props.thumbnail = "generic"
+ discoverer.stop()
+ return
+
+ self._path = path
+
+ success = discoverer.discover_uri_async(self._coreobject.props.url)
+
+ if not success:
+ self._log.warning("Could not add url to discoverer.")
+ self._coreobject.props.thumbnail = "generic"
+ discoverer.stop()
+ return
+
+ def _discovered(self, discoverer, info, error):
+ tags = info.get_tags()
+ index = 0
+
+ if (error is not None
+ or tags is None):
+ if error:
+ self._log.warning("Discoverer error: {}, {}".format(
+ Gst.CoreError(error.code), error.message))
+ discoverer.stop()
+ self._coreobject.props.thumbnail = "generic"
+ return
+
+ while True:
+ success, sample = tags.get_sample_index(Gst.TAG_IMAGE, index)
+ if not success:
+ break
+ index += 1
+ struct = sample.get_info()
+ if struct is None:
+ break
+ success, image_type = struct.get_enum(
+ 'image-type', GstTag.TagImageType)
+ if not success:
+ continue
+ if image_type != GstTag.TagImageType.FRONT_COVER:
+ continue
+
+ buf = sample.get_buffer()
+ success, map_info = buf.map(Gst.MapFlags.READ)
+ if not success:
+ continue
+
+ try:
+ mime = sample.get_caps().get_structure(0).get_name()
+ MediaArt.buffer_to_jpeg(map_info.data, mime, self._path)
+ AlbumArt(None, self._coreobject)
+ discoverer.stop()
+
+ return
+ except GLib.Error as error:
+ self._log.warning("Error: {}, {}".format(
+ MediaArt.Error(error.code), error.message))
+
+ discoverer.stop()
+
+ self._lookup_cover_in_directory()
+
+ def _lookup_cover_in_directory(self):
+ # Find local art in cover.jpeg files.
+ self._media_art.uri_async(
+ MediaArt.Type.ALBUM, MediaArt.ProcessFlags.NONE,
+ self._coreobject.props.url, self._artist, self._album,
+ GLib.PRIORITY_LOW, None, self._uri_async_cb, None)
+
+ def _uri_async_cb(self, src, result, data):
+ try:
+ success = self._media_art.uri_finish(result)
+ if success:
+ AlbumArt(None, self._coreobject)
+
+ return
+ except GLib.Error as error:
+ if MediaArt.Error(error.code) == MediaArt.Error.SYMLINK_FAILED:
+ # This error indicates that the coverart has already
+ # been linked by another concurrent lookup.
+ AlbumArt(None, self._coreobject)
+
+ return
+ else:
+ self._log.warning("Error: {}, {}".format(
+ MediaArt.Error(error.code), error.message))
+
+ self._coreobject.props.thumbnail = "generic"
diff --git a/gnomemusic/grilowrappers/grltrackerwrapper.py b/gnomemusic/grilowrappers/grltrackerwrapper.py
index 5f4340fe..e69f595d 100644
--- a/gnomemusic/grilowrappers/grltrackerwrapper.py
+++ b/gnomemusic/grilowrappers/grltrackerwrapper.py
@@ -30,6 +30,7 @@ from gnomemusic.corealbum import CoreAlbum
from gnomemusic.coreartist import CoreArtist
from gnomemusic.coredisc import CoreDisc
from gnomemusic.coresong import CoreSong
+from gnomemusic.embeddedart import EmbeddedArt
from gnomemusic.grilowrappers.grltrackerplaylists import GrlTrackerPlaylists
from gnomemusic.storeart import StoreArt
@@ -997,7 +998,7 @@ class GrlTrackerWrapper(GObject.GObject):
thumbnail_uri = resolved_media.get_thumbnail()
if thumbnail_uri is None:
- corealbum.props.thumbnail = "generic"
+ EmbeddedArt().query(corealbum)
else:
media.set_thumbnail(thumbnail_uri)
StoreArt(corealbum)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]