dotfiles/config/nix/scripts/replayGain

75 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env nix-shell
#! nix-shell -i python3 --pure
#! nix-shell -p python3 python3Packages.coloredlogs python3Packages.r128gain
# TODO r128gain is not maintainted anymore
2018-08-04 12:43:13 +02:00
# Normalisation is done at the default of each program,
# which is usually -89.0 dB
2019-11-01 18:34:45 +01:00
# TODO The simplifications/fixes I've done makes it consider
# multi-discs albums as multiple albums
2019-10-17 12:44:30 +02:00
import logging
2018-08-04 12:43:13 +02:00
import os
2019-10-17 12:44:30 +02:00
import sys
import typing
2018-08-04 12:43:13 +02:00
import coloredlogs
2018-08-07 16:09:41 +02:00
import r128gain
2018-08-04 12:43:13 +02:00
2021-06-13 11:49:21 +02:00
coloredlogs.install(level="DEBUG", fmt="%(levelname)s %(message)s")
2018-08-04 12:43:13 +02:00
log = logging.getLogger()
2018-08-07 16:09:41 +02:00
# TODO Remove debug
2018-08-04 12:43:13 +02:00
# Constants
2021-06-13 11:49:21 +02:00
FORCE = "-f" in sys.argv
2018-08-07 16:09:41 +02:00
if FORCE:
2021-06-13 11:49:21 +02:00
sys.argv.remove("-f")
2019-10-17 12:44:30 +02:00
if len(sys.argv) >= 2:
SOURCE_FOLDER = os.path.realpath(sys.argv[1])
else:
SOURCE_FOLDER = os.path.join(os.path.expanduser("~"), "Musiques")
2018-08-04 12:43:13 +02:00
2019-10-17 12:44:30 +02:00
def isMusic(f: str) -> bool:
2018-08-07 16:09:41 +02:00
ext = os.path.splitext(f)[1][1:].lower()
return ext in r128gain.AUDIO_EXTENSIONS
2018-08-04 12:43:13 +02:00
2019-10-17 12:44:30 +02:00
2018-08-04 12:43:13 +02:00
# Get album paths
2018-08-07 16:09:41 +02:00
log.info("Listing albums and tracks")
2019-10-17 12:44:30 +02:00
albums = list()
singleFiles = list()
2018-08-04 12:43:13 +02:00
for root, dirs, files in os.walk(SOURCE_FOLDER):
2019-10-17 12:44:30 +02:00
folder_has_music = False
for f in files:
if isMusic(f):
folder_has_music = True
fullPath = os.path.join(root, f)
singleFiles.append(fullPath)
2018-08-04 12:43:13 +02:00
2019-10-17 12:44:30 +02:00
if folder_has_music:
albums.append(root)
# log.info("Processing single files")
# r128gain.process(singleFiles, album_gain=False,
# skip_tagged=not FORCE, report=True)
2018-08-04 12:43:13 +02:00
for album in albums:
albumName = os.path.relpath(album, SOURCE_FOLDER)
log.info("Processing album {}".format(albumName))
2019-10-17 12:44:30 +02:00
musicFiles = list()
for f in os.listdir(album):
if isMusic(f):
fullPath = os.path.join(album, f)
musicFiles.append(fullPath)
2018-08-04 12:43:13 +02:00
2019-10-17 12:44:30 +02:00
if not musicFiles:
2018-08-07 16:09:41 +02:00
continue
2018-08-04 12:43:13 +02:00
2021-06-13 11:49:21 +02:00
r128gain.process(musicFiles, album_gain=True, skip_tagged=not FORCE, report=True)
2019-10-17 12:44:30 +02:00
print("==============================")