Preserve tags on normalise; output time to stdout logs

This commit is contained in:
Keith Edmunds 2021-05-27 16:15:14 +01:00
parent 21c7b234fb
commit b7861ffcf9
2 changed files with 30 additions and 9 deletions

View File

@ -37,7 +37,8 @@ syslog.addFilter(filter)
stderr.addFilter(filter)
# create formatter and add it to the handlers
stderr_fmt = logging.Formatter('%(leveltag)s: %(message)s')
stderr_fmt = logging.Formatter('[%(asctime)s] %(leveltag)s: %(message)s',
datefmt='%H:%M:%S')
syslog_fmt = logging.Formatter('[%(name)s] %(leveltag)s: %(message)s')
stderr.setFormatter(stderr_fmt)
syslog.setFormatter(syslog_fmt)

View File

@ -1,4 +1,4 @@
#!/usr/bin/python3
#!/usr/bin/env python
import argparse
import os
@ -6,8 +6,9 @@ import shutil
import tempfile
from config import Config
from log import INFO
from log import DEBUG, INFO
from model import Tracks, session
from mutagen.flac import FLAC
from pydub import AudioSegment, effects
from tinytag import TinyTag
@ -54,14 +55,33 @@ def add_path_to_db(path):
if Config.NORMALISE_ON_IMPORT:
# Get current file gid, uid and permissions
stats = os.stat(path)
normalised = effects.normalize(audio)
try:
# Copy original file
fd, temp_path = tempfile.mkstemp()
normalised.export(temp_path, format=os.path.splitext(path)[1][1:])
shutil.copyfile(temp_path, path)
os.chown(path, stats.st_uid, stats.st_gid)
os.chmod(path, stats.st_mode)
finally:
shutil.copyfile(path, temp_path)
except Exception as err:
DEBUG(f"songdb.add_path_to_db({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.add_path_to_db({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