def create_mediafile_fixture(self, ext='mp3', images=[]): """Copies a fixture mediafile with the extension to a temporary location and returns the path. It keeps track of the created locations and will delete the with `remove_mediafile_fixtures()` `images` is a subset of 'png', 'jpg', and 'tiff'. For each specified extension a cover art image is added to the media file. """ src = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext)) handle, path = mkstemp() os.close(handle) shutil.copyfile(src, path) if images: mediafile = MediaFile(path) imgs = [] for img_ext in images: file = util.bytestring_path('image-2x3.{0}'.format(img_ext)) img_path = os.path.join(_common.RSRC, file) with open(img_path, 'rb') as f: imgs.append(Image(f.read())) mediafile.images = imgs mediafile.save() if not hasattr(self, '_mediafile_fixtures'): self._mediafile_fixtures = [] self._mediafile_fixtures.append(path) return path
def test_SimpleConvertTask_can_keep_or_remove_album_art(tmp_path): """SimpleConvertTask can convert an audio file""" src = tmp_path.joinpath("sine.flac") mp3_art = tmp_path.joinpath("sine_art.mp3") mp3_noart = tmp_path.joinpath("sine_noart.mp3") ogg_art = tmp_path.joinpath("sine_art.ogg") ogg_noart = tmp_path.joinpath("sine_noart.ogg") sound = generators.Sine(440).to_audio_segment() sound.export(src, format="flac") # setup a source flac file with a cover image image_file = _utils.get_project_root().joinpath("tests/data/cover.jpg") with open(image_file, 'rb') as f: cover = f.read() cover = mediafile.Image(data=cover, desc=u'album cover', type=mediafile.ImageType.front) f = MediaFile(src) f.images = [cover] f.save() # by default, the converters preserve the album art # .. for PydubConverter tasks.SimpleConvertTask(src, mp3_art, converter=converters.PydubConverter()).start() g = MediaFile(mp3_art) assert f.images[0].data == cover.data assert f.images[0].mime_type == 'image/jpeg' assert f.images[0].data == g.images[0].data # ... for PydubConverter tasks.SimpleConvertTask(src, ogg_art, converter=converters.PydubConverter()).start() g = MediaFile(ogg_art) assert f.images[0].data == cover.data assert f.images[0].mime_type == 'image/jpeg' assert f.images[0].data == g.images[0].data # remove_art=True removes art # ... for PydubConverter tasks.SimpleConvertTask( src, mp3_noart, converter=converters.PydubConverter(remove_art=True)).start() g = MediaFile(mp3_noart) assert f.images[0].data == cover.data assert g.images == [] # remove_art=True removes art # ... for PydubConverter tasks.SimpleConvertTask( src, ogg_noart, converter=converters.PydubConverter(remove_art=True)).start() g = MediaFile(ogg_noart) assert f.images[0].data == cover.data assert g.images == []