mirror of
https://github.com/rwinkhart/flac2pod.git
synced 2026-08-28 21:06:36 -04:00
Improved non-FLAC detection
This commit is contained in:
+21
-21
@@ -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.")
|
||||
|
||||
Reference in New Issue
Block a user