Automated music library format conversion with cuesheet detection, tagging support and configurable regex to obtain tags from filenames. Configuration with ini-files to support multiple locations with multiple quality requirements.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
6.9 KiB

  1. CUE_SHEET_EXTENSION: str = ".cue"
  2. import os
  3. import shutil
  4. import logging
  5. import subprocess
  6. import sys
  7. import re
  8. from configparser import ConfigParser
  9. from copy import deepcopy
  10. from pathlib import Path
  11. from typing import List, Optional, Tuple
  12. from ffcuesplitter.cuesplitter import FFCueSplitter
  13. CONFIG_FILE: str = sys.argv[1]
  14. config = ConfigParser()
  15. config.read(CONFIG_FILE)
  16. SRC_FOLDER: str = config.get("source", "folder")
  17. SRC_FILE_EXTS: List[str] = config.get("source", "file_exts").split(",")
  18. SRC_ICON_EXTS: List[str] = config.get("source", "icon_exts").split(",")
  19. DST_FOLDER: str = config.get("destination", "folder")
  20. DST_FILE_EXT: str = config.get("destination", "file_ext")
  21. DST_OVERWRITE: bool = config.get("libconv", "overwrite") == "true"
  22. DRY_RUN: bool = config.get("libconv", "dry_run") != "false"
  23. DEDUCE_METADATA: bool = config.get("libconv", "deduce_metadata") == "true"
  24. FFMPEG_LOCATION: str = config.get("ffmpeg", "location")
  25. FFMPEG_OPTS: str = config.get("ffmpeg", "options")
  26. logging.basicConfig(level=logging.INFO)
  27. # ----------------------------------------------------------------- #
  28. # file and folder operations
  29. def copy(src: str, dst: str):
  30. if (DRY_RUN):
  31. logging.info(f"copy {src} to {dst}")
  32. else:
  33. shutil.copyfile(src, dst, follow_symlinks=True)
  34. def mkdir(path: str):
  35. if (DRY_RUN):
  36. logging.info(f"mkdir -r {path}")
  37. else:
  38. os.makedirs(path, exist_ok=True)
  39. def execute(command: str):
  40. if (DRY_RUN):
  41. logging.info(f"execute: {command}")
  42. else:
  43. subprocess.run(command)
  44. # ----------------------------------------------------------------- #
  45. # cue sheet processing
  46. def cue_sheet_processor(path: str):
  47. dst_folder = prepare_destination(path)
  48. sheet = get_files_with_ext(path, [CUE_SHEET_EXTENSION])
  49. if (len(sheet) != 1):
  50. logging.error(f"Expected exactly one but {path} contains {len(sheet)} cue sheets")
  51. return
  52. data = FFCueSplitter(sheet[0], dry=DRY_RUN, outputdir=dst_folder, suffix=DST_FILE_EXT, overwrite=DST_OVERWRITE, ffmpeg_cmd=FFMPEG_LOCATION, ffmpeg_add_params=FFMPEG_OPTS)
  53. data.open_cuefile()
  54. args = data.ffmpeg_arguments()["arguments"]
  55. dst_folder = prepare_destination(path)
  56. for arg in args:
  57. arg_end = arg.find("-y")
  58. filename = arg[arg_end + 5:-1]
  59. arg = arg[:arg_end]
  60. execute(f"{arg} \"{dst_folder}{filename}\"")
  61. # ----------------------------------------------------------------- #
  62. # basic "folder contains audio files" processing
  63. def metadata_from_folder(path: str) -> str:
  64. # this method has to be adapted to your individual folder structure
  65. # if there is none then do nothing as by default this is not used
  66. if not DEDUCE_METADATA:
  67. return ""
  68. path = os.path.normpath(path)
  69. folders = path.split(os.sep)
  70. logging.debug(f"deducing metadata from folders: {folders}")
  71. album_name = re.sub(r'[ ]*\(.*?\)', '', folders[-1])
  72. comment = folders[-1].replace(album_name, "").replace("(", "").replace(")", "").replace(" ","")
  73. logging.debug(f"got metadata: album={album_name} comment={comment} artist={folders[-2]} genre={folders[-3]}")
  74. return f"-metadata ALBUM=\"{album_name}\" -metadata COMMENT=\"{comment}\" -metadata ARTIST=\"{folders[-2]}\" -metadata GENRE=\"{folders[-3]}\""
  75. def metadata_from_file(filename: str) -> str:
  76. title=""
  77. number=""
  78. if re.match(r'[0-9]+[ ]*-[ ]*[.]*', filename):
  79. logging.debug(f"file {filename} matched metadata regex #1")
  80. number = re.search(r'[0-9]+[ ]*-[ ]*', filename).group()
  81. title = filename.replace(number, "")
  82. number = re.sub(r'[ ]*-[ ]*', '', number)
  83. elif re.match(r'[0-9]+\.[ ]*[.]*', filename):
  84. logging.info(f"file {filename} matched metadata regex #2")
  85. number = re.search(r'[0-9]+\.[ ]*', filename).group()
  86. title = filename.replace(number, "")
  87. number = re.sub(r'\.[ ]*', "", number)
  88. else:
  89. logging.debug(f"file {filename} matched no metadata regex")
  90. return f"-metadata TITLE=\"{title}\" -metadata TRACK=\"{number}\""
  91. def file_processor(path: str):
  92. dst_folder = prepare_destination(path)
  93. files = get_files_with_ext(path, SRC_FILE_EXTS)
  94. album_metadata = metadata_from_folder(path)
  95. for file in files:
  96. filename = get_filename(file, path)
  97. file_metadata = metadata_from_file(filename[1:])
  98. execute(f"\"{FFMPEG_LOCATION}\" -i \"{file}\" {FFMPEG_OPTS} {album_metadata} {file_metadata} \"{dst_folder + filename}.{DST_FILE_EXT}\"")
  99. # ----------------------------------------------------------------- #
  100. # Iteration over library folders and preparation
  101. def contains_extension(path: str, extensions: List[str]) -> bool:
  102. logging.debug(f"searching for extensions {extensions} in {path}")
  103. for root, dirs, files in os.walk(path):
  104. for file in files:
  105. for ext in extensions:
  106. if file.endswith(ext):
  107. logging.debug(f"file {file} matched extension '{ext}'")
  108. return True
  109. # needed to prevent recursive search
  110. break
  111. return False
  112. def get_files_with_ext(path: str, extensions: List[str]) -> List[str]:
  113. logging.debug(f"obtainint all files with extensions {extensions} from {path}")
  114. ret_files = []
  115. for root, dirs, files in os.walk(path):
  116. for file in files:
  117. for ext in extensions:
  118. if file.endswith(ext):
  119. logging.debug(f"file {file} matched extension '{ext}'")
  120. ret_files.append(os.path.join(root, file))
  121. # file is added now - no need to check the other extensions
  122. break
  123. # needed to prevent recursive search
  124. break
  125. return ret_files
  126. def prepare_destination(path: str) -> str:
  127. images = get_files_with_ext(path, SRC_ICON_EXTS)
  128. sub_path = path.replace(SRC_FOLDER, "")
  129. new_path = DST_FOLDER + sub_path
  130. mkdir(new_path)
  131. for img in images:
  132. img_name = img.replace(path, "")
  133. new_img_path = new_path + img_name
  134. copy(img, new_img_path)
  135. return new_path
  136. def get_filename(file: str, path: str) -> str:
  137. fname = file.replace(path, "")
  138. for ext in SRC_FILE_EXTS:
  139. fname = fname.replace("." + ext, "")
  140. return fname
  141. def scan_folder(path: str):
  142. for root, dirs, files in os.walk(path):
  143. for directory in dirs:
  144. process_folder(os.path.join(root, directory))
  145. # needed to prevent recursive search
  146. break
  147. def process_folder(path: str):
  148. logging.info(f"Visiting folder '{path}'")
  149. if contains_extension(path, [CUE_SHEET_EXTENSION]):
  150. logging.info("found cuesheet")
  151. cue_sheet_processor(path)
  152. elif contains_extension(path, SRC_FILE_EXTS):
  153. logging.info("found audio files")
  154. file_processor(path)
  155. else:
  156. logging.info("scanning subfolders")
  157. scan_folder(path)
  158. if __name__ == "__main__":
  159. logging.info(f"Using settings from {CONFIG_FILE}")
  160. if DRY_RUN:
  161. logging.info("Executing as DRY RUN")
  162. process_folder(SRC_FOLDER)