From 5550bc5b764fc5cc136a6427777cb7158c9233f6 Mon Sep 17 00:00:00 2001 From: Cuan Date: Wed, 20 Apr 2022 20:23:40 -0400 Subject: [PATCH] Added script for quickly adding ReplayGain data to a FLAC library --- bin/flac2pod | 8 +++--- bin/flac2pod-flacgain | 64 +++++++++++++++++++++++++++++++++++++++++++ bin/flac2pod.py | 1 + package.sh | 2 +- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100755 bin/flac2pod-flacgain create mode 120000 bin/flac2pod.py diff --git a/bin/flac2pod b/bin/flac2pod index 21f3f15..54f7ce2 100755 --- a/bin/flac2pod +++ b/bin/flac2pod @@ -13,11 +13,11 @@ from time import sleep # utility functions -def scan_source(): +def scan_source(_source): _artist_dirs, _album_dirs, _full_paths = [], [], [] - for _root, _directories, _files in walk(path.expanduser(source)): + for _root, _directories, _files in walk(path.expanduser(_source)): for _dir in sorted(_directories): - if not _root.endswith(path.expanduser(source)): + if not _root.endswith(path.expanduser(_source)): _artist_dirs.append(_root) _artist_dirs = list(set(_artist_dirs)) for _artist in sorted(_artist_dirs): @@ -137,6 +137,6 @@ if __name__ == "__main__": else: destination = args.destination_dir[0] - source_directories = scan_source() + source_directories = scan_source(source) create_destination() scan_destination_convert() diff --git a/bin/flac2pod-flacgain b/bin/flac2pod-flacgain new file mode 100755 index 0000000..161efd8 --- /dev/null +++ b/bin/flac2pod-flacgain @@ -0,0 +1,64 @@ +#!/bin/python3 + +from argparse import ArgumentParser +from flac2pod import scan_source +from mutagen.flac import FLAC +from os import listdir, system +from subprocess import PIPE, Popen, STDOUT +from sys import exit as s_exit + +# argument parsing +if __name__ == "__main__": + parser = ArgumentParser(description='Quickly tag your FLAC library with ReplayGain data using metaflac.') + parser.add_argument('source_dir', nargs='+', + help='directory tree containing music files - folder must contain library sorted into artists ' + 'and albums') + parser.add_argument('-f', '--force', action='store_true', + help='overwrite existing ReplayGain data') + args = parser.parse_args() + + if args.source_dir[0].endswith('/'): + source = args.source_dir[0][:-1] + else: + source = args.source_dir[0] + + source_directories = scan_source(source) + + gain = 1 + for album in sorted(source_directories[0]): + std_album = album.replace(' ', '\\ ').replace("'", "\\'").replace(')', '\\)') \ + .replace('(', '\\(').replace('&', '\\&').replace('`', '\\`').replace('$', '\\$') + for song in listdir(album): + audio = FLAC(f"{album}/{song}") + if audio.get('replaygain_track_gain') or audio.get('REPLAYGAIN_TRACK_GAIN'): + gain = 1 + else: + gain = 0 + break + if args.force or gain == 0: + print(f"[{album}/*] Adding ReplayGain data...") + system(f"metaflac --remove-replay-gain {std_album}/*") + command = f"metaflac --add-replay-gain {std_album}/*" + output = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) + block = output.communicate()[0].strip() # blocks the program from proceeding until stdout is given + if output.returncode == 1: + if block.decode('utf-8').__contains__('sample rate'): + print('Sample rate error, 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('$', '\\$') + command = f"metaflac --add-replay-gain {std_album}/{std_song}" + output = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) + block = output.communicate()[0].strip() + if output.returncode == 1: + print('There was an error processing your files.') + print(f"Return Code: [{str(output.returncode)}] {block.decode('utf-8')}") + s_exit(1) + else: + print('There was an error processing your files.') + print(f"Return Code: [{str(output.returncode)}] {block.decode('utf-8')}") + s_exit(1) + + else: + print(f"[{album}/*] ReplayGain data is already present.") diff --git a/bin/flac2pod.py b/bin/flac2pod.py new file mode 120000 index 0000000..f95c070 --- /dev/null +++ b/bin/flac2pod.py @@ -0,0 +1 @@ +/usr/bin/flac2pod \ No newline at end of file diff --git a/package.sh b/package.sh index 489ead6..786fbb0 100755 --- a/package.sh +++ b/package.sh @@ -39,7 +39,7 @@ pkgdesc='Converts your FLAC library to be iPod-ready' url='https://github.com/rwinkhart/flac2pod' arch=('any') license=('GPL2') -depends=(python ffmpeg screen python-mutagen) +depends=(ffmpeg flac python python-mutagen screen) source=(\""$source"\") sha512sums=('"$sha512"')