From 4f10ed7bad85a44b43224535ce4bc8affe8c3a67 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Thu, 15 Jul 2021 17:54:34 +0100 Subject: [PATCH] Normalise mp3's on import --- app/songdb.py | 55 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/app/songdb.py b/app/songdb.py index d6ce04b..f611629 100755 --- a/app/songdb.py +++ b/app/songdb.py @@ -9,6 +9,7 @@ from config import Config from log import DEBUG, INFO from model import Tracks, Playdates, PlaylistTracks, Session from mutagen.flac import FLAC +from mutagen.mp3 import MP3 from pydub import AudioSegment, effects from tinytag import TinyTag @@ -68,6 +69,12 @@ def create_track_from_file(session, path): session.commit() if Config.NORMALISE_ON_IMPORT: + # Check type + ftype = os.path.splitext(path)[1][1:] + if ftype not in ['mp3', 'flac']: + INFO(f"File type {ftype} not implemented") + return track + # Get current file gid, uid and permissions stats = os.stat(path) try: @@ -78,26 +85,32 @@ def create_track_from_file(session, path): DEBUG(f"songdb.create_track_from_file({path}): err1: {str(err)}") return - # Overwrite original file with normalised output - normalised = effects.normalize(audio) - try: - normalised.export(path, format=os.path.splitext(path)[1][1:]) - # Fix up permssions and ownership - os.chown(path, stats.st_uid, stats.st_gid) - os.chmod(path, stats.st_mode) - # Copy tags - src = FLAC(temp_path) - dst = FLAC(path) - for tag in src: - dst[tag] = src[tag] - dst.save() - except Exception as err: - DEBUG(f"songdb.create_track_from_file({path}): err2: {str(err)}") - # Restore original file - shutil.copyfile(path, temp_path) - finally: - if os.path.exists(temp_path): - os.remove(temp_path) + # Overwrite original file with normalised output + normalised = effects.normalize(audio) + try: + normalised.export(path, format=os.path.splitext(path)[1][1:]) + # Fix up permssions and ownership + os.chown(path, stats.st_uid, stats.st_gid) + os.chmod(path, stats.st_mode) + # Copy tags + if ftype == 'flac': + tag_handler = FLAC + elif ftype == 'mp3': + tag_handler = MP3 + else: + return track + src = tag_handler(temp_path) + dst = tag_handler(path) + for tag in src: + dst[tag] = src[tag] + dst.save() + except Exception as err: + DEBUG(f"songdb.create_track_from_file({path}): err2: {str(err)}") + # Restore original file + shutil.copyfile(path, temp_path) + finally: + if os.path.exists(temp_path): + os.remove(temp_path) return track @@ -105,7 +118,7 @@ def create_track_from_file(session, path): def full_update_db(): "Rescan all entries in database" - pass + print("Full scan not yet implemented") def get_audio_segment(path):