scripts/regain
2023-10-02 22:13:12 +01:00

111 lines
4.1 KiB
Python
Executable File

#!/usr/bin/env python3
# regain - search folders below working directory and call regainer on each album found
# Copyright Frederick Boniface 2023 - fredboniface.co.uk
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import subprocess
import logging
import argparse
VERSION = "0.0.1"
# Updated 2023/10/02
FORMATS = ["flac", "m4a", "aac", "alac", "mp3", "m3a", "ogg", "opus", "oga"]
PATH_TO_REGAINER = "./regainer.py"
DESCRIPTION = """
regain - Calls regainer.py on any albums found in subdirectories.
\n\n
Run the regain command in a directory that contains albums and ReplayGain tags will be
added to the files, including album tags.
\n\n
regain will walk through all subdirectories so can be run in your 'Music' folder, or
individual artist/album folders and tag as many albums as neccessary.
"""
def parse_arguments():
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('--debug', action='store_true', help='Enable debug logging (including regainer output)')
return parser.parse_args()
def configureLogging(debug_mode = False):
log_level = logging.DEBUG if debug_mode else logging.INFO
logging.basicConfig(level=log_level, format='%(asctime)s - %(levelname)s - %(message)s')
def getWorkingDirectory():
return os.getcwd()
def find_files_by_extension(workdir):
file_lists = []
for root, _, files in os.walk(workdir):
matching_files = [os.path.join(root, file) for file in files if file.endswith(tuple(FORMATS))]
if matching_files:
file_lists.append(matching_files)
return file_lists
def countFiles(file_list_of_lists):
count = 0
for item in file_list_of_lists:
count = count + len(item)
return count
if __name__ == "__main__":
args = parse_arguments()
configureLogging(args.debug)
workdir = getWorkingDirectory()
print(f"regain version {VERSION}")
logging.info(f"WorkDir: {workdir}")
logging.debug("Debug logs enabled")
file_lists = find_files_by_extension(workdir)
logging.debug(f"File lists: {file_lists}")
file_count = countFiles(file_lists)
if file_count == 0:
logging.error("No files to process")
print("You should run this command from the parent directory of any album folders")
sys.exit(1)
logging.info(f"Files for processing: {file_count}")
cmd = ["python", PATH_TO_REGAINER, "-f"]
for sublist in file_lists:
cmd.append("-a")
for item in sublist:
cmd.append(item)
logging.debug(f"Running command: {cmd}")
logging.info("Running regainer.py")
if args.debug:
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
output = result.stdout
error = result.stderr
# Handle the captured output as needed
print("Output:", output)
print("Error:", error)
else:
result = subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
logging.debug(f"regainer.py has exited with code {result.returncode}")
if result.returncode != 0:
print("You can re-run with --debug to view the output from regainer")
else:
print("Files successfully tagged")