def copy_album_art(self, album, dest_dir, path_formats, pretend=False, link=False, hardlink=False): """Copies or converts the associated cover art of the album. Album must have at least one track. """ if not album or not album.artpath: return album_item = album.items().get() # Album shouldn't be empty. if not album_item: return # Get the destination of the first item (track) of the album, we use # this function to format the path accordingly to path_formats. dest = album_item.destination(basedir=dest_dir, path_formats=path_formats) # Remove item from the path. dest = os.path.join(*util.components(dest)[:-1]) dest = album.art_destination(album.artpath, item_dir=dest) if album.artpath == dest: return if not pretend: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info(u'Skipping {0} (target file exists)', util.displayable_path(album.artpath)) return # Decide whether we need to resize the cover-art image. resize = False maxwidth = None if self.config['album_art_maxwidth']: maxwidth = self.config['album_art_maxwidth'].get(int) size = ArtResizer.shared.get_size(album.artpath) self._log.debug('image size: {}', size) if size: resize = size[0] > maxwidth else: self._log.warning(u'Could not get size of image (please see ' u'documentation for dependencies).') # Either copy or resize (while copying) the image. if resize: self._log.info(u'Resizing cover art from {0} to {1}', util.displayable_path(album.artpath), util.displayable_path(dest)) if not pretend: ArtResizer.shared.resize(maxwidth, album.artpath, dest) else: if pretend: msg = 'ln' if hardlink else ('ln -s' if link else 'cp') self._log.info(u'{2} {0} {1}', util.displayable_path(album.artpath), util.displayable_path(dest), msg) else: msg = 'Hardlinking' if hardlink \ else ('Linking' if link else 'Copying') self._log.info(u'{2} cover art from {0} to {1}', util.displayable_path(album.artpath), util.displayable_path(dest), msg) if hardlink: util.hardlink(album.artpath, dest) elif link: util.link(album.artpath, dest) else: util.copy(album.artpath, dest)
def convert_item(self, dest_dir, keep_new, path_formats, fmt, pretend=False, link=False, hardlink=False): """A pipeline thread that converts `Item` objects from a library. """ command, ext = get_format(fmt) item, original, converted = None, None, None while True: item = yield (item, original, converted) dest = item.destination(basedir=dest_dir, path_formats=path_formats) # When keeping the new file in the library, we first move the # current (pristine) file to the destination. We'll then copy it # back to its old path or transcode it to a new path. if keep_new: original = dest converted = item.path if should_transcode(item, fmt): converted = replace_ext(converted, ext) else: original = item.path if should_transcode(item, fmt): dest = replace_ext(dest, ext) converted = dest # Ensure that only one thread tries to create directories at a # time. (The existence check is not atomic with the directory # creation inside this function.) if not pretend: with _fs_lock: util.mkdirall(dest) if os.path.exists(util.syspath(dest)): self._log.info(u'Skipping {0} (target file exists)', util.displayable_path(item.path)) continue if keep_new: if pretend: self._log.info(u'mv {0} {1}', util.displayable_path(item.path), util.displayable_path(original)) else: self._log.info(u'Moving to {0}', util.displayable_path(original)) util.move(item.path, original) if should_transcode(item, fmt): linked = False try: self.encode(command, original, converted, pretend) except subprocess.CalledProcessError: continue else: linked = link or hardlink if pretend: msg = 'ln' if hardlink else ('ln -s' if link else 'cp') self._log.info(u'{2} {0} {1}', util.displayable_path(original), util.displayable_path(converted), msg) else: # No transcoding necessary. msg = 'Hardlinking' if hardlink \ else ('Linking' if link else 'Copying') self._log.info(u'{1} {0}', util.displayable_path(item.path), msg) if hardlink: util.hardlink(original, converted) elif link: util.link(original, converted) else: util.copy(original, converted) if pretend: continue id3v23 = self.config['id3v23'].as_choice([True, False, 'inherit']) if id3v23 == 'inherit': id3v23 = None # Write tags from the database to the converted file. item.try_write(path=converted, id3v23=id3v23) if keep_new: # If we're keeping the transcoded file, read it again (after # writing) to get new bitrate, duration, etc. item.path = converted item.read() item.store() # Store new path and audio data. if self.config['embed'] and not linked: album = item._cached_album if album and album.artpath: self._log.debug(u'embedding album art from {}', util.displayable_path(album.artpath)) art.embed_item(self._log, item, album.artpath, itempath=converted, id3v23=id3v23) if keep_new: plugins.send('after_convert', item=item, dest=dest, keepnew=True) else: plugins.send('after_convert', item=item, dest=converted, keepnew=False)