From 00325b36eb82f9939aca645a51cc9755728b3b86 Mon Sep 17 00:00:00 2001 From: jgrogan Date: Sun, 29 Sep 2024 20:28:44 +0100 Subject: [PATCH] Start setting up conversion workflow --- src/jgutils/converters.py | 25 ++++++------------- src/jgutils/filesystem/filesystem.py | 15 +++++++++++ src/jgutils/music/music.py | 37 +++++++++++++++++----------- src/jgutils/tasks/tasks.py | 5 +--- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/jgutils/converters.py b/src/jgutils/converters.py index 6faf711..34324ca 100644 --- a/src/jgutils/converters.py +++ b/src/jgutils/converters.py @@ -9,7 +9,6 @@ logger = logging.getLogger(__name__) class ConversionConfig(NamedTuple): input_dir: Path - output_dir: Path output_ext: str input_ext: str @@ -22,29 +21,19 @@ def _get_converted_path(input_path: Path, config: ConversionConfig): relative_path = input_path.relative_to(config.input_dir) output_filename = str(input_path.stem) + f".{config.output_ext}" - output_filename.replace("'", "") - output_path = config.output_dir / relative_path.parent / output_filename - return output_path - - -def _build_conversion_tasks( - input_files: list[Path], - output_files: list[Path], - config: ConversionConfig, - conversion_func: Callable, -) -> list[Tasks]: - tasks = [] - for input_path, output_path in zip(input_files, output_files): - cmd = conversion_func(input_path, config.output_dir, config.output_ext) - tasks.append(Task(cmd, output_tmp, output_path)) - return tasks + return relative_path.parent / output_filename def get_unconverted_files( input_files: list[Path], config: ConversionConfig ) -> list[Path]: output_files = [_get_converted_path(f, config) for f in input_files] - return [f for f in output_files if not f.exists()] + + conversion_pairs = [] + for input_file, output_file in zip(input_files, output_files): + if not output_file.exists(): + conversion_pairs.append((input_file, output_file)) + return conversion_pairs def convert(config: ConversionConfig, conversion_func: Callable): diff --git a/src/jgutils/filesystem/filesystem.py b/src/jgutils/filesystem/filesystem.py index 538c6a4..0cd0199 100644 --- a/src/jgutils/filesystem/filesystem.py +++ b/src/jgutils/filesystem/filesystem.py @@ -1,5 +1,20 @@ from pathlib import Path +import os +import sys def get_files_recursive(search_path: Path, extension: str) -> list[Path]: return list(search_path.rglob(f"*.{extension}")) + +def delete_empty_dirs_recursive(search_path: Path): + for curdir, subdirs, files in os.walk(search_path): + if len(subdirs) == 0 and len(files) == 0: + #print(curdir) + os.rmdir(curdir) + + +if __name__ == "__main__": + + input_path = sys.argv[1] + print(input_path) + delete_empty_dirs_recursive(Path(input_path).resolve()) diff --git a/src/jgutils/music/music.py b/src/jgutils/music/music.py index d1077dd..c9c1498 100644 --- a/src/jgutils/music/music.py +++ b/src/jgutils/music/music.py @@ -3,6 +3,7 @@ import logging import shutil from pathlib import Path import uuid +import subprocess from tinytag import TinyTag from pydantic import BaseModel @@ -46,23 +47,25 @@ class MusicCollection(BaseModel): return None -def ffmpeg_convert(input_path: Path, output_dir: Path, output_ext: str) -> str: - output_tmp = output_dir / (str(uuid.uuid4()) + f".{output_ext}") - cmd = f"ffmpeg -i '{input_path}' -ab 320k -map_metadata 0 -id3v2_version 3 '{output_tmp}'" +def ffmpeg_convert(input_path: Path, output_path: Path) -> int: + identifier = str(uuid.uuid4()) + input_tmp = input_path.parent / (identifier + input_path.suffix) + shutil.copy(input_path, input_tmp) + + output_tmp = output_path.parent / (identifier + output_path.suffix) + cmd = f"ffmpeg -i '{input_tmp}' -ab 320k -map_metadata 0 -id3v2_version 3 '{output_tmp}'" -def move(input_dir: Path, output_dir: Path): + os.makedirs(output_path.parent, exist_ok=True) + status = subprocess.run(cmd, shell=True) - logger.info("Moving files in %s to %s", input_dir, output_dir) - os.makedirs(output_dir, exist_ok=True) + input_tmp.unlink() + if status.returncode == 0: + shutil.move(output_tmp, output_path) + else: + logger.error("Error converting: %s", input_path) + return status.returncode - mp3_files = list(input_dir.resolve().rglob("*.mp3")) - for idx, path in enumerate(mp3_files): - logger.info("Moving file %d of %d", idx, len(mp3_files)) - relative_path = path.relative_to(input_dir) - output_path = output_dir / relative_path - os.makedirs(output_path.parent, exist_ok=True) - shutil.move(path, output_path) def get_metadata(input_dir: Path, extension: str) -> MusicCollection: @@ -106,6 +109,12 @@ def refresh(input_dir: Path): logger.warn("Found tag with no title, skipping: %s", tag) continue - os.make_dirs(input_dir / tag.artist, exist_ok=True) + os.makedirs(input_dir / tag.artist, exist_ok=True) + + if tag.album: + os.makedirs(input_dir / tag.artist / tag.album, exist_ok=True) + shutil.move(eachFile, input_dir / tag.artist / tag.album / f"{tag.title}.flac") + else: + shutil.move(eachFile, input_dir / tag.artist / f"{tag.title}.flac") diff --git a/src/jgutils/tasks/tasks.py b/src/jgutils/tasks/tasks.py index 66350cb..a392744 100644 --- a/src/jgutils/tasks/tasks.py +++ b/src/jgutils/tasks/tasks.py @@ -18,10 +18,7 @@ class Task(NamedTuple): def _run_task(args): task = args[0] - subprocess.run(cmd, shell=True) - os.makedirs(task.output_path.parent, exist_ok=True) - shutil.move(task.output_tmp, task.output_path) - + subprocess.run(task.cmd, shell=True) def run_tasks(tasks, pool_size: 10): with Pool(10) as p: