9 Commits
2 changed files with 53 additions and 43 deletions
+26 -20
View File
@@ -3,9 +3,10 @@
# external modules # external modules
from argparse import ArgumentParser from argparse import ArgumentParser
from mutagen.flac import FLAC from mutagen.flac import FLAC, FLACNoHeaderError
from os import cpu_count, path, walk from os import cpu_count, path, walk
from pathlib import Path from pathlib import Path
from shutil import copy
from subprocess import check_output, Popen from subprocess import check_output, Popen
from sys import exit as s_exit from sys import exit as s_exit
from time import sleep from time import sleep
@@ -29,7 +30,7 @@ def scan_source(_source):
for _root, _directories, _files in walk(path.expanduser(_album)): for _root, _directories, _files in walk(path.expanduser(_album)):
for _filename in _files: for _filename in _files:
if _filename.endswith('.flac'): if _filename.endswith('.flac'):
_full_paths.append(f"{_album}/{_filename[:-5]}") _full_paths.append(f"{_album}/{_filename}")
return _album_dirs, _full_paths return _album_dirs, _full_paths
@@ -44,13 +45,13 @@ def scan_destination_convert():
for _file in source_directories[1]: for _file in source_directories[1]:
_i_total += 1 _i_total += 1
_progress = round((_i_total / _total_files) * 100, 2) _progress = round((_i_total / _total_files) * 100, 2)
if not Path(f"{_file.replace(path.expanduser(source), path.expanduser(destination))}.mp3").is_file() and not \ if not Path(f"{(_file.replace(path.expanduser(source), path.expanduser(destination)))[:-5]}.m4a").is_file():
Path(f"{_file.replace(path.expanduser(source), path.expanduser(destination))}.m4a").is_file():
_file_s = _file.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)').replace('(', '\\(')\ _file_s = _file.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)').replace('(', '\\(')\
.replace('&', '\\&').replace('`', '\\`').replace('$', '\\$') + '.flac' .replace(']', '\\]').replace('[', '\\[').replace('&', '\\&').replace('`', '\\`')\
.replace('$', '\\$')
_file_d = _file.replace(path.expanduser(source), path.expanduser(destination)).replace(' ', '\\ ')\ _file_d = _file.replace(path.expanduser(source), path.expanduser(destination)).replace(' ', '\\ ')\
.replace("'", "\\'").replace(')', '\\)').replace('(', '\\(').replace('&', '\\&')\ .replace("'", "\\'").replace(')', '\\)').replace('(', '\\(').replace(']', '\\]')\
.replace('`', '\\`').replace('$', '\\$') + '.m4a' .replace('[', '\\[').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$')[:-5] + '.m4a'
_active_processes = int(check_output('ps -C ffmpeg | wc -l', shell=True)) - 1 _active_processes = int(check_output('ps -C ffmpeg | wc -l', shell=True)) - 1
if _active_processes >= cpu_count(): if _active_processes >= cpu_count():
while _active_processes >= cpu_count(): while _active_processes >= cpu_count():
@@ -60,33 +61,38 @@ def scan_destination_convert():
_i += 1 _i += 1
print(f"\n{_progress}% | \u001b[38;5;0;48;5;15mConverting {_file_s} to {_file_d}...\u001b[0m\n") print(f"\n{_progress}% | \u001b[38;5;0;48;5;15mConverting {_file_s} to {_file_d}...\u001b[0m\n")
# get relevant ReplayGain info # get relevant ReplayGain info
_audio = FLAC(f"{_file}.flac") try:
_audio = FLAC(f"{_file}")
if args.albumgain: if args.albumgain:
if _audio.get('replaygain_album_gain'): if _audio.get('REPLAYGAIN_ALBUM_GAIN'):
_rggain = float(_audio.get('replaygain_album_gain')[0][:-3])
elif _audio.get('REPLAYGAIN_ALBUM_GAIN'):
_rggain = float(_audio.get('REPLAYGAIN_ALBUM_GAIN')[0][:-3]) _rggain = float(_audio.get('REPLAYGAIN_ALBUM_GAIN')[0][:-3])
elif _audio.get('replaygain_album_gain'):
_rggain = float(_audio.get('replaygain_album_gain')[0][:-3])
else: else:
_rggain = None _rggain = None
if _audio.get('replaygain_album_peak'): if _audio.get('REPLAYGAIN_ALBUM_PEAK'):
_rgpeak = float(_audio.get('replaygain_album_peak')[0])
elif _audio.get('REPLAYGAIN_ALBUM_PEAK'):
_rgpeak = float(_audio.get('REPLAYGAIN_ALBUM_PEAK')[0]) _rgpeak = float(_audio.get('REPLAYGAIN_ALBUM_PEAK')[0])
elif _audio.get('replaygain_album_peak'):
_rgpeak = float(_audio.get('replaygain_album_peak')[0])
else: else:
_rgpeak = None _rgpeak = None
else: else:
if _audio.get('replaygain_track_gain'): if _audio.get('REPLAYGAIN_TRACK_GAIN'):
_rggain = float(_audio.get('replaygain_track_gain')[0][:-3])
elif _audio.get('REPLAYGAIN_TRACK_GAIN'):
_rggain = float(_audio.get('REPLAYGAIN_TRACK_GAIN')[0][:-3]) _rggain = float(_audio.get('REPLAYGAIN_TRACK_GAIN')[0][:-3])
elif _audio.get('replaygain_track_gain'):
_rggain = float(_audio.get('replaygain_track_gain')[0][:-3])
else: else:
_rggain = None _rggain = None
if _audio.get('replaygain_track_peak'): if _audio.get('REPLAYGAIN_TRACK_PEAK'):
_rgpeak = float(_audio.get('replaygain_track_peak')[0])
elif _audio.get('REPLAYGAIN_TRACK_PEAK'):
_rgpeak = float(_audio.get('REPLAYGAIN_TRACK_PEAK')[0]) _rgpeak = float(_audio.get('REPLAYGAIN_TRACK_PEAK')[0])
elif _audio.get('replaygain_track_peak'):
_rgpeak = float(_audio.get('replaygain_track_peak')[0])
else: else:
_rgpeak = None _rgpeak = None
except FLACNoHeaderError:
print(f"[{_file}] Not a FLAC file, copying...")
copy(f"{_file}", f"{_file.replace(path.expanduser(source), path.expanduser(destination))}")
_rggain, _rgpeak = None, None
# determine ffmpeg arguments # determine ffmpeg arguments
if args.bake and _rggain is not None: if args.bake and _rggain is not None:
_bake_args, _flac2pod_rg_tags = '-filter:a "volume=' + str(_rggain) + 'dB"', '' _bake_args, _flac2pod_rg_tags = '-filter:a "volume=' + str(_rggain) + 'dB"', ''
+8 -4
View File
@@ -2,7 +2,7 @@
from argparse import ArgumentParser from argparse import ArgumentParser
from flac2pod import scan_source from flac2pod import scan_source
from mutagen.flac import FLAC from mutagen.flac import FLAC, FLACNoHeaderError
from os import listdir, system from os import listdir, system
from subprocess import PIPE, Popen, STDOUT from subprocess import PIPE, Popen, STDOUT
from sys import exit as s_exit from sys import exit as s_exit
@@ -26,16 +26,20 @@ 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(')', '\\)') \ std_album = album.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)').replace('(', '\\(')\
.replace('(', '\\(').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$') .replace(']', '\\]').replace('[', '\\[').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$')
for song in listdir(album): for song in listdir(album):
try:
audio = FLAC(f"{album}/{song}") audio = FLAC(f"{album}/{song}")
if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'): if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'):
gain = 1 gain = 1
else: else:
gain = 0 gain = 0
break break
if args.force or gain == 0: except FLACNoHeaderError:
print(f"[{album}/*] Contains non-FLAC files, skipping album...")
gain = 2
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}/*") system(f"metaflac --remove-replay-gain {std_album}/*")
command = f"metaflac --add-replay-gain {std_album}/*" command = f"metaflac --add-replay-gain {std_album}/*"