From 70d252bcc72764298f10a93a219729ed18cb04ac Mon Sep 17 00:00:00 2001 From: litagin02 Date: Tue, 19 Mar 2024 20:14:19 +0900 Subject: [PATCH] Feat: preserve relative path when resample --- resample.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/resample.py b/resample.py index 5be01a7..55f0f7f 100644 --- a/resample.py +++ b/resample.py @@ -33,9 +33,17 @@ def normalize_audio(data: NDArray[Any], sr: int): return data -def resample(file: Path, output_dir: Path, target_sr: int, normalize: bool, trim: bool): +def resample( + file: Path, + input_dir: Path, + output_dir: Path, + target_sr: int, + normalize: bool, + trim: bool, +): """ - fileを読み込んで、target_srなwavファイルに変換してoutput_dir直下に保存する + fileを読み込んで、target_srなwavファイルに変換して、 + output_dirの中に、input_dirからの相対パスを保つように保存する """ try: # librosaが読めるファイルかチェック @@ -53,7 +61,10 @@ def resample(file: Path, output_dir: Path, target_sr: int, normalize: bool, trim ) if trim: wav, _ = librosa.effects.trim(wav, top_db=30) - soundfile.write(output_dir / file.with_suffix(".wav").name, wav, sr) + relative_path = file.relative_to(input_dir) + output_path = output_dir / relative_path.with_suffix(".wav") + output_path.parent.mkdir(parents=True, exist_ok=True) + soundfile.write(output_path, wav, sr) except Exception as e: logger.warning(f"Cannot load file, so skipping: {file}, {e}") @@ -107,6 +118,7 @@ if __name__ == "__main__": input_dir = Path(args.input_dir) output_dir = Path(args.output_dir) + logger.info(f"Resampling {input_dir} to {output_dir}") sr = int(args.sr) normalize: bool = args.normalize trim: bool = args.trim @@ -122,7 +134,7 @@ if __name__ == "__main__": with ThreadPoolExecutor(max_workers=processes) as executor: futures = [ - executor.submit(resample, file, output_dir, sr, normalize, trim) + executor.submit(resample, file, input_dir, output_dir, sr, normalize, trim) for file in original_files ] for future in tqdm(