dotfiles/scripts/replayGain

69 lines
1.8 KiB
Plaintext
Raw Normal View History

2018-08-04 12:43:13 +02:00
#!/usr/bin/env python3
# Normalisation is done at the default of each program,
# which is usually -89.0 dB
import os
import coloredlogs
import logging
2018-08-07 16:09:41 +02:00
import r128gain
import sys
2018-08-04 12:43:13 +02:00
coloredlogs.install(level='DEBUG', fmt='%(levelname)s %(message)s')
log = logging.getLogger()
2018-08-07 16:09:41 +02:00
# TODO Remove debug
2018-08-04 12:43:13 +02:00
# Constants
2018-08-07 16:09:41 +02:00
FORCE = '-f' in sys.argv
if FORCE:
sys.argv.remove('-f')
SOURCE_FOLDER = os.path.realpath(sys.argv[1]) if len(sys.argv) >= 2 else os.path.join(os.path.expanduser("~"), "Musique")
2018-08-04 12:43:13 +02:00
2018-08-07 16:09:41 +02:00
def isMusic(f):
ext = os.path.splitext(f)[1][1:].lower()
return ext in r128gain.AUDIO_EXTENSIONS
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")
2018-08-04 12:43:13 +02:00
albums = set()
2018-08-07 16:09:41 +02:00
singleFiles = set()
2018-08-04 12:43:13 +02:00
for root, dirs, files in os.walk(SOURCE_FOLDER):
relRoot = os.path.relpath(root, SOURCE_FOLDER)
head, tail = os.path.split(relRoot)
2018-08-07 16:09:41 +02:00
# 1 component in the path: save files path as single
2018-08-04 12:43:13 +02:00
if not len(head):
2018-08-07 16:09:41 +02:00
for f in files:
if isMusic(f):
fullPath = os.path.join(root, f)
singleFiles.add(fullPath)
2018-08-04 12:43:13 +02:00
head, tail = os.path.split(head)
if len(head):
continue
2018-08-07 16:09:41 +02:00
# 2 components in the path: save album path
2018-08-04 12:43:13 +02:00
albums.add(root)
2018-08-07 16:09:41 +02:00
log.info("Processing single files")
# r128gain.process(list(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))
2018-08-07 16:09:41 +02:00
musicFiles = set()
2018-08-04 12:43:13 +02:00
for root, dirs, files in os.walk(album):
for f in files:
2018-08-07 16:09:41 +02:00
if isMusic(f):
fullPath = os.path.join(root, f)
musicFiles.add(fullPath)
2018-08-04 12:43:13 +02:00
2018-08-07 16:09:41 +02:00
# print(musicFiles)
if not len(musicFiles):
continue
r128gain.process(list(musicFiles), album_gain=True, skip_tagged=not FORCE, report=True)
print("==============================")
2018-08-04 12:43:13 +02:00