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.

221 lines
7.6 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. if DST_OVERWRITE:
  27. FFMPEG_OPTS += " -y"
  28. logging.basicConfig(level=logging.INFO)
  29. # ----------------------------------------------------------------- #
  30. # file and folder operations
  31. def copy(src: str, dst: str):
  32. logging.debug(f"copy {src} to {dst}")
  33. if not DRY_RUN:
  34. shutil.copyfile(src, dst, follow_symlinks=True)
  35. def mkdir(path: str):
  36. logging.debug(f"mkdir -r {path}")
  37. if not DRY_RUN:
  38. os.makedirs(path, exist_ok=True)
  39. def execute(command: str):
  40. logging.debug(f"execute: {command}")
  41. if not DRY_RUN:
  42. os.system(command)
  43. # ----------------------------------------------------------------- #
  44. # cue sheet processing
  45. def cue_sheet_processor(path: str):
  46. dst_folder, preexisting = prepare_destination(path)
  47. if preexisting and not DST_OVERWRITE:
  48. logging.info(f"Album {path} already exists in output location and overwrite is disabled - skipping")
  49. return
  50. sheet = get_files_with_ext(path, [CUE_SHEET_EXTENSION])
  51. args
  52. if (len(sheet) != 1):
  53. logging.error(f"Expected exactly one but {path} contains {len(sheet)} cue sheets - trying file processor")
  54. file_processor(path)
  55. return
  56. try:
  57. 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)
  58. data.open_cuefile()
  59. args = data.ffmpeg_arguments()["arguments"]
  60. except:
  61. logging.error(f"Error during parsing of cuesheet {path} - trying file processor")
  62. file_processor(path)
  63. return
  64. for arg in args:
  65. arg_end = arg.find("-y")
  66. filename = arg[arg_end + 5:-1]
  67. arg = arg[:arg_end]
  68. execute(f"{arg} \"{dst_folder}{filename}\"")
  69. # ----------------------------------------------------------------- #
  70. # basic "folder contains audio files" processing
  71. def metadata_from_folder(path: str) -> str:
  72. # this method has to be adapted to your individual folder structure
  73. # if there is none then do nothing as by default this is not used
  74. if not DEDUCE_METADATA:
  75. return ""
  76. path = os.path.normpath(path)
  77. folders = path.split(os.sep)
  78. logging.debug(f"deducing metadata from folders: {folders}")
  79. album_name = re.sub(r'[ ]*\(.*?\)', '', folders[-1])
  80. comment = folders[-1].replace(album_name, "").replace("(", "").replace(")", "").replace(" ","")
  81. logging.debug(f"got metadata: album={album_name} comment={comment} artist={folders[-2]} genre={folders[-3]}")
  82. return f"-metadata ALBUM=\"{album_name}\" -metadata COMMENT=\"{comment}\" -metadata ARTIST=\"{folders[-2]}\" -metadata GENRE=\"{folders[-3]}\""
  83. def metadata_from_file(filename: str) -> str:
  84. title=""
  85. number=""
  86. if re.match(r'[0-9]+[ ]*-[ ]*[.]*', filename):
  87. logging.debug(f"file {filename} matched metadata regex #1")
  88. number = re.search(r'[0-9]+[ ]*-[ ]*', filename).group()
  89. title = filename.replace(number, "")
  90. number = re.sub(r'[ ]*-[ ]*', '', number)
  91. elif re.match(r'[0-9]+\.[ ]*[.]*', filename):
  92. logging.info(f"file {filename} matched metadata regex #2")
  93. number = re.search(r'[0-9]+\.[ ]*', filename).group()
  94. title = filename.replace(number, "")
  95. number = re.sub(r'\.[ ]*', "", number)
  96. else:
  97. logging.debug(f"file {filename} matched no metadata regex")
  98. return f"-metadata TITLE=\"{title}\" -metadata TRACK=\"{number}\""
  99. def file_processor(path: str):
  100. dst_folder, preexisting = prepare_destination(path)
  101. if preexisting and not DST_OVERWRITE:
  102. logging.info(f"Album {path} already exists in output location and overwrite is disabled - skipping")
  103. return
  104. files = get_files_with_ext(path, SRC_FILE_EXTS)
  105. album_metadata = metadata_from_folder(path)
  106. for file in files:
  107. filename = get_filename(file, path)
  108. file_metadata = metadata_from_file(filename[1:])
  109. execute(f"\"{FFMPEG_LOCATION}\" -i \"{file}\" {FFMPEG_OPTS} {album_metadata} {file_metadata} \"{dst_folder + filename}.{DST_FILE_EXT}\"")
  110. # ----------------------------------------------------------------- #
  111. # Iteration over library folders and preparation
  112. def contains_extension(path: str, extensions: List[str]) -> bool:
  113. logging.debug(f"searching for extensions {extensions} in {path}")
  114. for root, dirs, files in os.walk(path):
  115. for file in files:
  116. for ext in extensions:
  117. if file.endswith(ext):
  118. logging.debug(f"file {file} matched extension '{ext}'")
  119. return True
  120. # needed to prevent recursive search
  121. break
  122. return False
  123. def get_files_with_ext(path: str, extensions: List[str]) -> List[str]:
  124. logging.debug(f"obtainint all files with extensions {extensions} from {path}")
  125. ret_files = []
  126. for root, dirs, files in os.walk(path):
  127. for file in files:
  128. for ext in extensions:
  129. if file.endswith(ext):
  130. logging.debug(f"file {file} matched extension '{ext}'")
  131. ret_files.append(os.path.join(root, file))
  132. # file is added now - no need to check the other extensions
  133. break
  134. # needed to prevent recursive search
  135. break
  136. return ret_files
  137. def prepare_destination(path: str) -> Tuple[str,bool]:
  138. images = get_files_with_ext(path, SRC_ICON_EXTS)
  139. sub_path = path.replace(SRC_FOLDER, "")
  140. new_path = DST_FOLDER + sub_path
  141. existing: bool = os.path.exists(new_path)
  142. mkdir(new_path)
  143. for img in images:
  144. img_name = img.replace(path, "")
  145. new_img_path = new_path + img_name
  146. copy(img, new_img_path)
  147. return (new_path,existing)
  148. def get_filename(file: str, path: str) -> str:
  149. fname = file.replace(path, "")
  150. for ext in SRC_FILE_EXTS:
  151. fname = fname.replace("." + ext, "")
  152. return fname
  153. def scan_folder(path: str):
  154. for root, dirs, files in os.walk(path):
  155. for directory in dirs:
  156. process_folder(os.path.join(root, directory))
  157. # needed to prevent recursive search
  158. break
  159. def process_folder(path: str):
  160. logging.info(f"Visiting folder '{path}'")
  161. if contains_extension(path, [CUE_SHEET_EXTENSION]):
  162. logging.info("found cuesheet")
  163. cue_sheet_processor(path)
  164. elif contains_extension(path, SRC_FILE_EXTS):
  165. logging.info("found audio files")
  166. file_processor(path)
  167. else:
  168. logging.info("scanning subfolders")
  169. scan_folder(path)
  170. if __name__ == "__main__":
  171. logging.info(f"Using settings from {CONFIG_FILE}")
  172. if DRY_RUN:
  173. logging.info("Executing as DRY RUN")
  174. if DST_OVERWRITE:
  175. logging.info("Overwrite of existing files active")
  176. process_folder(SRC_FOLDER)