def collect_files(music_dir, files, cache, supported_formats): i = 0 for dirpath, dirnames, filenames in os.walk(music_dir): for filename in filenames: filepath = un(relpath(os.path.join(dirpath, filename), music_dir), sys.getfilesystemencoding()) properpath = os.path.join(dirpath, filename) mtime = os.path.getmtime(properpath) # check the cache if filepath in cache: cache[filepath] = True record = files[filepath] if mtime <= record[1]: # the file's still ok continue ext = os.path.splitext(filename)[1] if ext in supported_formats: i += 1 print ou(u" [%i] %s |" % (i, filepath)), album_id = get_album_id(music_dir, filepath) print ou(album_id or u"<single track>") # fields here: album_id, mtime, already_processed files[filepath] = (album_id, mtime, False)
def show_rgain_info(filenames, mp3_format="ql"): formats_map = rgio.BaseFormatsMap(mp3_format) for filename in filenames: filename = un(filename, sys.getfilesystemencoding()) print ou(filename) try: trackdata, albumdata = formats_map.read_gain(filename) except Exception, 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 do_collectiongain(music_dir, ref_level=89, force=False, dry_run=False, mp3_format="ql", ignore_cache=False): music_dir = un(music_dir, sys.getfilesystemencoding()) 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 files = read_cache(cache_file) # yeah, side-effects are bad, I know validate_cache(files) cache = dict.fromkeys(files.iterkeys(), False) print "Collecting files ..." # whenever this part is stopped (KeyboardInterrupt/other exception), the # cache is written to disk so all progress persists try: collect_files(music_dir, files, cache, rgio.BaseFormatsMap(mp3_format).supported_formats) # clean cache for filepath, visited in cache.items(): if not visited: del cache[filepath] del files[filepath] # hopefully gets rid of at least one huge data structure del cache albums, single_tracks = transform_cache(files, ignore_cache) print # gain everything that has survived the cleansing do_gain_all(music_dir, albums, single_tracks, files, ref_level, force, dry_run, mp3_format) finally: validate_cache(files) write_cache(cache_file, files) print "All finished."
def do_gain(files, ref_level=89, force=False, dry_run=False, album=True, mp3_format="ql"): files = [un(filename, sys.getfilesystemencoding()) for filename in files] formats_map = rgio.BaseFormatsMap(mp3_format) newfiles = [] for filename in files: if not os.path.splitext(filename)[1] in formats_map.supported_formats: print ou(u"%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 ou(u" %s:" % filename), try: trackdata, albumdata = formats_map.read_gain(filename) except Exception, exc: raise Error(u"%s: error - %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