mirror of
https://github.com/rwinkhart/flac2pod.git
synced 2026-09-05 08:37:16 -04:00
Improved non-FLAC detection
This commit is contained in:
+21
-21
@@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
from argparse import ArgumentParser
|
from argparse import ArgumentParser
|
||||||
from flac2pod import scan_source
|
from flac2pod import scan_source
|
||||||
|
from glob import glob
|
||||||
from mutagen.flac import FLAC, FLACNoHeaderError
|
from mutagen.flac import FLAC, FLACNoHeaderError
|
||||||
from os import listdir, system
|
from os import listdir
|
||||||
from subprocess import PIPE, run
|
from subprocess import PIPE, run
|
||||||
from sys import exit as s_exit
|
from sys import exit as s_exit
|
||||||
|
|
||||||
@@ -25,33 +26,35 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
gain = 1
|
gain = 1
|
||||||
for album in sorted(source_directories[0]):
|
for album in sorted(source_directories[0]):
|
||||||
std_album = album.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)').replace('(', '\\(')\
|
|
||||||
.replace(']', '\\]').replace('[', '\\[').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$')
|
|
||||||
for song in listdir(album):
|
for song in listdir(album):
|
||||||
try:
|
gain = 1 # reset for each song
|
||||||
audio = FLAC(f"{album}/{song}")
|
if not song.lower().endswith('.flac'):
|
||||||
if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'):
|
gain = 2 # set non-flac signal
|
||||||
gain = 1
|
else:
|
||||||
else:
|
try:
|
||||||
gain = 0
|
audio = FLAC(f"{album}/{song}")
|
||||||
break
|
if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'):
|
||||||
except FLACNoHeaderError:
|
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...")
|
print(f"[{album}/*] Contains non-FLAC files, skipping album...")
|
||||||
gain = 2
|
break
|
||||||
if gain != 2 and (args.force or gain == 0):
|
if gain != 2 and (args.force or gain == 0):
|
||||||
print(f"[{album}/*] Adding ReplayGain data...")
|
print(f"[{album}/*] Adding ReplayGain data...")
|
||||||
system(f"metaflac --remove-replay-gain {std_album}/*")
|
album_files = glob(album + '/*')
|
||||||
output = run(f"metaflac --add-replay-gain {std_album}/*", shell=True, stderr=PIPE, text=True)
|
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.returncode == 1:
|
||||||
if output.stderr.__contains__('sample') or output.stderr.__contains__('resolution of') or output.\
|
if output.stderr.__contains__('sample') or output.stderr.__contains__('resolution of') or output.\
|
||||||
stderr.__contains__('does not match'):
|
stderr.__contains__('does not match'):
|
||||||
print('Resolution/Sample Rate/Channel mismatch, scanning tracks as individuals...')
|
print('Resolution/Sample Rate/Channel mismatch, scanning tracks as individuals...')
|
||||||
for song in listdir(album):
|
for song in listdir(album):
|
||||||
print(f"[{album}/{song}] Adding ReplayGain data...")
|
print(f"[{album}/{song}] Adding ReplayGain data...")
|
||||||
std_song = song.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)')\
|
output = run(('metaflac', '--add-replay-gain', f"{album}/{song}"), stderr=PIPE, text=True)
|
||||||
.replace('(', '\\(').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$')
|
|
||||||
output = run(f"metaflac --add-replay-gain {std_album}/{std_song}", shell=True, stderr=PIPE,
|
|
||||||
text=True)
|
|
||||||
if output.returncode == 1:
|
if output.returncode == 1:
|
||||||
print('There was an error processing your files.')
|
print('There was an error processing your files.')
|
||||||
print(f"Return Code: [{str(output.returncode)}] {output.stderr}")
|
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('There was an error processing your files.')
|
||||||
print(f"Return Code: [{str(output.returncode)}] {output.stderr}")
|
print(f"Return Code: [{str(output.returncode)}] {output.stderr}")
|
||||||
s_exit(1)
|
s_exit(1)
|
||||||
|
|
||||||
else:
|
|
||||||
print(f"[{album}/*] ReplayGain data is already present.")
|
|
||||||
|
|||||||
Reference in New Issue
Block a user