def show_rgain_info(filenames, mp3_format=None): formats_map = rgio.BaseFormatsMap(mp3_format) for filename in filenames: print(filename) try: trackdata, albumdata = formats_map.read_gain(filename) except Exception as exc: print(" <Error reading Replay Gain: %r>" % (exc,)) continue if not trackdata and not albumdata: print(" <No Replay Gain information>") if trackdata and trackdata.ref_level: ref_level = trackdata.ref_level elif albumdata and albumdata.ref_level: ref_level = albumdata.ref_level else: ref_level = None if ref_level is not None: print(" Reference loudness %i dB" % ref_level) if trackdata: print(" Track gain %.2f dB" % trackdata.gain) print(" Track peak %.8f" % trackdata.peak) if albumdata: print(" Album gain %.2f dB" % albumdata.gain) print(" Album peak %.8f" % albumdata.peak)
def replaygain(mp3s: list, target_gain: float) -> None: """Calculates and writes replaygain data to ID3 tag. Args: mp3s: list of mp3s to process target_gain: the target gain to adjust the track to """ forms_map = rgio.BaseFormatsMap(None) tracks_data, album_data = _replaygain_calc(mp3s, target_gain) for filename, track_data in tracks_data.items(): forms_map.write_gain(filename, track_data, album_data)
def do_collectiongain(music_dir, ref_level=89, force=False, dry_run=False, mp3_format=None, ignore_cache=False, jobs=0): music_abspath = os.path.abspath(music_dir) musicpath_hash = md5(music_abspath.encode("utf-8")).hexdigest() cache_file = os.path.join(os.path.expanduser("~"), ".cache", "collectiongain-cache.%s" % musicpath_hash) # load the cache, if desired if not ignore_cache: files = read_cache(cache_file) else: files = {} print("Collecting files ...") # whenever this part is stopped (KeyboardInterrupt/other exception), the # cache is written to disk so all progress persists try: visited_cache = dict.fromkeys(iter(files.keys()), False) collect_files(music_dir, files, visited_cache, rgio.BaseFormatsMap(mp3_format).is_supported) # clean cache for filepath, visited in list(visited_cache.items()): if not visited: del visited_cache[filepath] del files[filepath] # hopefully gets rid of at least one huge data structure del visited_cache albums, single_tracks = transform_cache(files) # gain everything that has survived the cleansing do_gain_all(music_dir, albums, single_tracks, files, ref_level, force, dry_run, mp3_format, jobs) finally: write_cache(cache_file, files) print("All finished.")
def do_gain(files, ref_level=89, force=False, dry_run=False, album=True, # noqa mp3_format=None): formats_map = rgio.BaseFormatsMap(mp3_format) newfiles = [] for filename in files: if not formats_map.is_supported_format(os.path.splitext(filename)[1]): print("%s: not supported, ignoring it" % filename) else: newfiles.append(filename) files = newfiles if not force: print("Checking for Replay Gain information ...") newfiles = [] for filename in files: print(" %s:" % filename, end='') try: trackdata, albumdata = formats_map.read_gain(filename) except Exception as exc: raise Error("%s: %s" % (filename, exc)) else: if trackdata and albumdata: print("track and album") elif not trackdata and albumdata: print("album only") newfiles.append(filename) elif trackdata and not albumdata: print("track only") if album: newfiles.append(filename) else: print("none") newfiles.append(filename) if not album: files = newfiles elif not len(newfiles): files = newfiles if not files: # no files left print("Nothing to do.") return 0 # calculate gain print("Calculating Replay Gain information ...") try: tracks_data, albumdata = calculate_gain(files, ref_level) if album: print(" Album gain: %.2f dB" % (albumdata.gain,)) except Exception as exc: raise Error("Error while calculating gain - %s" % exc) if not album: albumdata = None # write gain if not dry_run: print("Writing Replay Gain information to files ...") for filename, trackdata in tracks_data.items(): print(" %s:" % filename, end='') try: formats_map.write_gain(filename, trackdata, albumdata) except Exception as exc: raise Error("%s: %s" % (filename, exc)) else: print("done") print("Done")