From 3bffe595e50ab3a58050ae95c0a002beb042a382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Constantin=20F=C3=BCrst?= Date: Mon, 29 Aug 2022 23:16:30 +0200 Subject: [PATCH] catch errors from ffcuesplitter and handle by falling back to file processor, test for output folder for albums existing and skip if DST_OVERWRITE is not set, small optimizations as well --- libconv.py | 60 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/libconv.py b/libconv.py index 334cf8d..23fcdc9 100644 --- a/libconv.py +++ b/libconv.py @@ -28,44 +28,55 @@ DEDUCE_METADATA: bool = config.get("libconv", "deduce_metadata") == "true" FFMPEG_LOCATION: str = config.get("ffmpeg", "location") FFMPEG_OPTS: str = config.get("ffmpeg", "options") +if DST_OVERWRITE: + FFMPEG_OPTS += " -y" + logging.basicConfig(level=logging.INFO) # ----------------------------------------------------------------- # # file and folder operations def copy(src: str, dst: str): - if (DRY_RUN): - logging.info(f"copy {src} to {dst}") - else: + logging.debug(f"copy {src} to {dst}") + if not DRY_RUN: shutil.copyfile(src, dst, follow_symlinks=True) def mkdir(path: str): - if (DRY_RUN): - logging.info(f"mkdir -r {path}") - else: + logging.debug(f"mkdir -r {path}") + if not DRY_RUN: os.makedirs(path, exist_ok=True) def execute(command: str): - if (DRY_RUN): - logging.info(f"execute: {command}") - else: - subprocess.run(command) + logging.debug(f"execute: {command}") + if not DRY_RUN: + os.system(command) # ----------------------------------------------------------------- # # cue sheet processing def cue_sheet_processor(path: str): - dst_folder = prepare_destination(path) + dst_folder, preexisting = prepare_destination(path) + + if preexisting and not DST_OVERWRITE: + logging.info(f"Album {path} already exists in output location and overwrite is disabled - skipping") + return + sheet = get_files_with_ext(path, [CUE_SHEET_EXTENSION]) + args if (len(sheet) != 1): - logging.error(f"Expected exactly one but {path} contains {len(sheet)} cue sheets") + logging.error(f"Expected exactly one but {path} contains {len(sheet)} cue sheets - trying file processor") + file_processor(path) return - 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) - data.open_cuefile() - args = data.ffmpeg_arguments()["arguments"] - dst_folder = prepare_destination(path) + try: + 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) + data.open_cuefile() + args = data.ffmpeg_arguments()["arguments"] + except: + logging.error(f"Error during parsing of cuesheet {path} - trying file processor") + file_processor(path) + return for arg in args: arg_end = arg.find("-y") @@ -114,7 +125,12 @@ def metadata_from_file(filename: str) -> str: return f"-metadata TITLE=\"{title}\" -metadata TRACK=\"{number}\"" def file_processor(path: str): - dst_folder = prepare_destination(path) + dst_folder, preexisting = prepare_destination(path) + + if preexisting and not DST_OVERWRITE: + logging.info(f"Album {path} already exists in output location and overwrite is disabled - skipping") + return + files = get_files_with_ext(path, SRC_FILE_EXTS) album_metadata = metadata_from_folder(path) @@ -153,11 +169,13 @@ def get_files_with_ext(path: str, extensions: List[str]) -> List[str]: break return ret_files -def prepare_destination(path: str) -> str: +def prepare_destination(path: str) -> Tuple[str,bool]: images = get_files_with_ext(path, SRC_ICON_EXTS) sub_path = path.replace(SRC_FOLDER, "") new_path = DST_FOLDER + sub_path + existing: bool = os.path.exists(new_path) + mkdir(new_path) for img in images: @@ -165,7 +183,7 @@ def prepare_destination(path: str) -> str: new_img_path = new_path + img_name copy(img, new_img_path) - return new_path + return (new_path,existing) def get_filename(file: str, path: str) -> str: fname = file.replace(path, "") @@ -194,6 +212,10 @@ def process_folder(path: str): if __name__ == "__main__": logging.info(f"Using settings from {CONFIG_FILE}") + if DRY_RUN: logging.info("Executing as DRY RUN") + if DST_OVERWRITE: + logging.info("Overwrite of existing files active") + process_folder(SRC_FOLDER)