def get_images(self): try: audio = self.MutagenType(self["~filename"]) except EnvironmentError: return [] # metadata_block_picture images = [] for data in audio.get("metadata_block_picture", []): try: cover = Picture(base64.b64decode(data)) except (TypeError, FLACError): continue f = get_temp_cover_file(cover.data) images.append( EmbeddedImage(f, cover.mime, cover.width, cover.height, cover.depth, cover.type)) # coverart + coverartmime cover = audio.get("coverart") try: cover = cover and base64.b64decode(cover[0]) except TypeError: cover = None if cover: mime = audio.get("coverartmime") mime = (mime and mime[0]) or "image/" f = get_temp_cover_file(cover) images.append(EmbeddedImage(f, mime)) images.sort(key=lambda c: c.sort_key) return images
def test_set_image(self): self.assertTrue(self.song.has_images) fileobj = BytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) self.song.set_image(image) image = self.song.get_primary_image() self.assertTrue(image) self.assertEqual(image.read(), b"foo") self.assertTrue(self.song.has_images)
def test_set_image(self): self.assertTrue(self.song.has_images) fileobj = cBytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) self.song.set_image(image) image = self.song.get_primary_image() self.assertTrue(image) self.assertEqual(image.read(), b"foo") self.assertTrue(self.song.has_images)
def test_from_path(self): image = EmbeddedImage.from_path(self.filename) self.assertTrue(image) self.assertEqual(image.file.name, self.filename) self.assertEqual(image.mime_type, "image/png") self.assertEqual(image.width, 150) self.assertEqual(image.height, 10) self.assertEqual(image.color_depth, 8)
def test_from_path_empty(self): h, empty = mkstemp() os.close(h) try: image = EmbeddedImage.from_path(empty) self.assertFalse(image) finally: os.remove(empty)
def test_set_image(self): fileobj = cBytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) song = FLACFile(self.filename) self.assertFalse(song.get_primary_image()) song.set_image(image) self.assertEqual(song.get_primary_image().width, 10)
def test_set_image_no_tag(self): m = mutagen.apev2.APEv2(self.f) m.delete() fileobj = cBytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) self.s.set_image(image) images = self.s.get_images() self.assertEqual(len(images), 1)
def test_set_image_no_tag(self): f = mutagen.File(self.filename) f.delete() song = MP3File(self.filename) fileobj = StringIO.StringIO("foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) song.set_image(image) song = MP3File(self.filename) self.assertTrue(song.has_images)
def test_set_image_no_tag(self): f = mutagen.File(self.filename) f.delete() song = self.KIND(self.filename) fileobj = BytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) song.set_image(image) song = self.KIND(self.filename) self.assertTrue(song.has_images)
def test_set_image(self): fileobj = cBytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) self.s.set_image(image) self.assertTrue(self.s.has_images) images = self.s.get_images() self.assertEqual(len(images), 1) self.assertEqual(images[0].mime_type, "image/") self.assertEqual(images[0].read(), b"foo")
def test_set_image(self): fileobj = StringIO.StringIO("foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) self.assertFalse(self.song.has_images) self.song.set_image(image) self.assertTrue(self.song.has_images) image = self.song.get_primary_image() self.assertEqual(image.mime_type, "image/jpeg") self.assertEqual(image.file.read(), "foo")
def get_primary_image(self): """Returns the primary embedded image""" try: audio = self.MutagenType(self["~filename"]) except EnvironmentError: return None pictures = [] for data in audio.get("metadata_block_picture", []): try: pictures.append(Picture(base64.b64decode(data))) except (TypeError, FLACError): pass cover = None for pic in pictures: if pic.type == APICType.COVER_FRONT: cover = pic break cover = cover or pic if cover: f = get_temp_cover_file(cover.data) return EmbeddedImage(f, cover.mime, cover.width, cover.height, cover.depth, cover.type) cover = audio.get("coverart") try: cover = cover and base64.b64decode(cover[0]) except TypeError: cover = None if not cover: self.has_images = False return mime = audio.get("coverartmime") mime = (mime and mime[0]) or "image/" f = get_temp_cover_file(cover) return EmbeddedImage(f, mime)
def test_set_image(self): fileobj = BytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) song = self.KIND(self.filename) self.failIf(song.has_images) song.set_image(image) self.assertTrue(song.has_images) song = self.KIND(self.filename) self.assertTrue(song.has_images) self.assertEqual(song.get_primary_image().mime_type, "image/jpeg")
def get_primary_image(self): """Returns the primary embedded image or None""" try: tag = mutagen.asf.ASF(self["~filename"]) except Exception: return for image in tag.get("WM/Picture", []): try: (mime, desc, data, type_) = unpack_image(image.value) except ValueError: continue if type_ == APICType.COVER_FRONT: # Only cover images f = get_temp_cover_file(data) return EmbeddedImage(f, mime, type_=type_)
def get_images(self): """Returns a list of embedded images""" images = [] try: tag = mutagen.id3.ID3(self["~filename"]) except Exception: return images for frame in tag.getall("APIC"): f = get_temp_cover_file(frame.data) images.append(EmbeddedImage(f, frame.mime, type_=frame.type)) images.sort(key=lambda c: c.sort_key) return images
def get_images(self): images = super(FLACFile, self).get_images() try: tag = FLAC(self["~filename"]) except EnvironmentError: return images for cover in tag.pictures: fileobj = get_temp_cover_file(cover.data) images.append( EmbeddedImage(fileobj, cover.mime, cover.width, cover.height, cover.depth, cover.type)) images.sort(key=lambda c: c.sort_key) return images
def get_primary_image(self): try: tag = MP4(self["~filename"]) except Exception: return for cover in tag.get("covr", []): if cover.imageformat == MP4Cover.FORMAT_JPEG: mime = "image/jpeg" elif cover.imageformat == MP4Cover.FORMAT_PNG: mime = "image/png" else: mime = "image/" f = get_temp_cover_file(cover) return EmbeddedImage(f, mime)
def parse_cover(key, value): """Returns a EmbeddedImage or None""" # http://www.hydrogenaud.io/forums/index.php?showtopic=40603 cover_type = get_cover_type(key, value) if cover_type is None: return parts = value.value.split(b"\x00", 1) if len(parts) != 2: return f = get_temp_cover_file(parts[-1]) if not f: return return EmbeddedImage(f, "image/", type_=cover_type)
def get_primary_image(self): """Returns the primary embedded image""" try: tag = FLAC(self["~filename"]) except EnvironmentError: return None covers = tag.pictures if not covers: return super(FLACFile, self).get_primary_image() covers.sort(key=lambda c: APICType.sort_key(c.type)) cover = covers[0] fileobj = get_temp_cover_file(cover.data) return EmbeddedImage(fileobj, cover.mime, cover.width, cover.height, cover.depth, cover.type)
def __set_image(self, menu_item, songs): win = WritingWindow(self.plugin_window, len(songs)) win.show() for song in songs: if song.can_change_images: fileobj = app.cover_manager.get_cover(song) if fileobj: path = fileobj.name image = EmbeddedImage.from_path(path) if image: song.set_image(image) if win.step(): break win.destroy() self.plugin_finish()
def __set_image(self, menu_item, songs): win = WritingWindow(self.plugin_window, len(songs)) win.show() for song in songs: if song.can_change_images: fileobj = song.find_cover() if fileobj: path = fileobj.name image = EmbeddedImage.from_path(path) if image: song.set_image(image) if win.step(): break win.destroy() self.plugin_finish()
def get_images(self): images = [] try: tag = mutagen.asf.ASF(self["~filename"]) except Exception: return images for image in tag.get("WM/Picture", []): try: (mime, desc, data, type_) = unpack_image(image.value) except ValueError: continue f = get_temp_cover_file(data) images.append(EmbeddedImage(f, mime, type_=type_)) images.sort(key=lambda c: c.sort_key) return images
def get_primary_image(self): """Returns the primary embedded image""" try: tag = mutagen.id3.ID3(self["~filename"]) except Exception: return # get the APIC frame with type == 3 (cover) or the first one cover = None for frame in tag.getall("APIC"): cover = cover or frame if frame.type == APICType.COVER_FRONT: cover = frame break if cover: f = get_temp_cover_file(cover.data) return EmbeddedImage(f, cover.mime, type_=cover.type)
def test_set_image(self): data = _get_jpeg() song = self.MutagenType(self.filename) song["coverart"] = base64.b64encode(data).decode("ascii") song["coverartmime"] = "image/jpeg" song.save() fileobj = cBytesIO(b"foo") image = EmbeddedImage(fileobj, "image/jpeg", 10, 10, 8) song = self.QLType(self.filename) self.assertTrue(song.has_images) self.assertTrue(song.get_primary_image()) self.assertTrue(song.has_images) song.set_image(image) self.assertTrue(song.has_images) self.assertEqual(song.get_primary_image().width, 10) song = self.MutagenType(self.filename) self.assertTrue("coverart" not in song) self.assertTrue("coverartmime" not in song)
def get_images(self): images = [] try: tag = MP4(self["~filename"]) except Exception: return [] for cover in tag.get("covr", []): if cover.imageformat == MP4Cover.FORMAT_JPEG: mime = "image/jpeg" elif cover.imageformat == MP4Cover.FORMAT_PNG: mime = "image/png" else: mime = "image/" f = get_temp_cover_file(cover) images.append(EmbeddedImage(f, mime)) return images
def test_main(self): # embedd one cover, move one to the other dir MP3File(self.file1).set_image(EmbeddedImage.from_path(self.cover1)) os.unlink(self.cover1) dest = os.path.join(self.dir2, "cover.png") shutil.move(self.cover2, dest) self.cover2 = dest # move one audio file in each dir shutil.move(self.file1, self.dir1) self.file1 = os.path.join(self.dir1, os.path.basename(self.file1)) shutil.move(self.file2, self.dir2) self.file2 = os.path.join(self.dir2, os.path.basename(self.file2)) song1 = MP3File(self.file1) song2 = MP3File(self.file2) def is_embedded(fileobj): return not path_equal(fileobj.name, self.cover2) # each should find a cover self.assertTrue(is_embedded(self.manager.get_cover(song1))) self.assertTrue(not is_embedded(self.manager.get_cover(song2))) # both settings should search both songs before giving up config.set("albumart", "prefer_embedded", True) self.assertTrue( is_embedded(self.manager.get_cover_many([song1, song2]))) self.assertTrue( is_embedded(self.manager.get_cover_many([song2, song1]))) config.set("albumart", "prefer_embedded", False) self.assertTrue( not is_embedded(self.manager.get_cover_many([song1, song2]))) self.assertTrue( not is_embedded(self.manager.get_cover_many([song2, song1])))
def test_repr(self): image = EmbeddedImage.from_path(self.filename) repr(image)
def test_from_path_bogus(self): image = EmbeddedImage.from_path(self.filename + "nope") self.assertFalse(image)
def test_not_an_image(self): path = os.path.join(DATA_DIR, 'test-2.wma') image = EmbeddedImage.from_path(path) self.assertFalse(image)
def test_get_extensions(self): image = EmbeddedImage.from_path(self.filename) self.assertTrue("png" in image.extensions)