import csv import shutil from pathlib import Path folder_path = Path("darija20") folder_path.mkdir(exist_ok=True) audio_folders_path: Path = folder_path / "data" audio_folders_path.mkdir(exist_ok=True) all_rows = [] for file in Path("darija/texts").iterdir(): with file.open("r") as f: reader = csv.DictReader(f, delimiter="\t") split = file.stem split_audios_path = audio_folders_path / split split_audios_path.mkdir(exist_ok=True) columns = reader.fieldnames for row in reader: wav = row["wav"] shutil.copy(Path("darija/wavs") / wav, split_audios_path) row["wav"] = Path("data") / split / row["wav"] all_rows.append(row) columns = ["file_name", "words", "duration", "audio_id"] with (folder_path / "metadata.csv").open("w", newline='', encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=columns) writer.writeheader() for row in all_rows: row["file_name"] = row["wav"] row["audio_id"] = Path(row["wav"]).stem del row["wav"] writer.writerow(row)