From 91cea901be610f6364d9c8c1276bf3d5f608fada Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 2 Jul 2023 18:41:05 -0400 Subject: [PATCH] Improved non-FLAC detection --- lib/flac2pod-flacgain.py | 42 ++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/flac2pod-flacgain.py b/lib/flac2pod-flacgain.py index f5d181f..555049b 100755 --- a/lib/flac2pod-flacgain.py +++ b/lib/flac2pod-flacgain.py @@ -2,8 +2,9 @@ from argparse import ArgumentParser from flac2pod import scan_source +from glob import glob from mutagen.flac import FLAC, FLACNoHeaderError -from os import listdir, system +from os import listdir from subprocess import PIPE, run from sys import exit as s_exit @@ -25,33 +26,35 @@ if __name__ == "__main__": gain = 1 for album in sorted(source_directories[0]): - std_album = album.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)').replace('(', '\\(')\ - .replace(']', '\\]').replace('[', '\\[').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$') for song in listdir(album): - try: - audio = FLAC(f"{album}/{song}") - if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'): - gain = 1 - else: - gain = 0 - break - except FLACNoHeaderError: + gain = 1 # reset for each song + if not song.lower().endswith('.flac'): + gain = 2 # set non-flac signal + else: + try: + audio = FLAC(f"{album}/{song}") + if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'): + print(f"[{album}/*] ReplayGain data is already present.") + break + else: + gain = 0 + except FLACNoHeaderError: + gain = 2 # set non-flac signal + if gain == 2: print(f"[{album}/*] Contains non-FLAC files, skipping album...") - gain = 2 + break if gain != 2 and (args.force or gain == 0): print(f"[{album}/*] Adding ReplayGain data...") - system(f"metaflac --remove-replay-gain {std_album}/*") - output = run(f"metaflac --add-replay-gain {std_album}/*", shell=True, stderr=PIPE, text=True) + album_files = glob(album + '/*') + run(['metaflac', '--remove-replay-gain'] + album_files) + output = run(['metaflac', '--add-replay-gain'] + album_files, stderr=PIPE, text=True) if output.returncode == 1: if output.stderr.__contains__('sample') or output.stderr.__contains__('resolution of') or output.\ stderr.__contains__('does not match'): print('Resolution/Sample Rate/Channel mismatch, scanning tracks as individuals...') for song in listdir(album): print(f"[{album}/{song}] Adding ReplayGain data...") - std_song = song.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)')\ - .replace('(', '\\(').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$') - output = run(f"metaflac --add-replay-gain {std_album}/{std_song}", shell=True, stderr=PIPE, - text=True) + output = run(('metaflac', '--add-replay-gain', f"{album}/{song}"), stderr=PIPE, text=True) if output.returncode == 1: print('There was an error processing your files.') print(f"Return Code: [{str(output.returncode)}] {output.stderr}") @@ -60,6 +63,3 @@ if __name__ == "__main__": print('There was an error processing your files.') print(f"Return Code: [{str(output.returncode)}] {output.stderr}") s_exit(1) - - else: - print(f"[{album}/*] ReplayGain data is already present.")