"""Prepare HuggingFace-compatible metadata CSV for the 'raw' subset. Reads the canonical metadata.csv, parses each SRT file to extract plain text, and writes raw/metadata.csv with a `file_name` column for the audiofolder builder. """ import csv import re from pathlib import Path from rich.progress import Progress REPO_ROOT = Path(__file__).resolve().parent.parent def parse_srt_text(srt_path: Path) -> str: """Extract plain text from an SRT file, stripping sequence numbers and timecodes.""" try: content = srt_path.read_text(encoding="utf-8") except FileNotFoundError: return "" text_lines = [] for line in content.strip().split("\n"): line = line.strip() if not line or line.isdigit(): continue if re.match(r"\d{2}:\d{2}:\d{2},\d{3}\s*-->", line): continue text_lines.append(line) return " ".join(text_lines) def main() -> None: src = REPO_ROOT / "metadata.csv" out_dir = REPO_ROOT / "raw" out_dir.mkdir(exist_ok=True) dst = out_dir / "metadata.csv" with open(src, encoding="utf-8", newline="") as f: reader = csv.DictReader(f) rows = list(reader) out_fields = ["file_name", "transcription", "id", "title", "description", "publish_date", "duration", "duration_seconds"] with Progress() as progress: task = progress.add_task("Parsing SRT files", total=len(rows)) out_rows = [] for row in rows: srt_path = REPO_ROOT / row["subtitles"] transcription = parse_srt_text(srt_path) out_rows.append({ "file_name": row["audio"], "transcription": transcription, "id": row["id"], "title": row["title"], "description": row["description"], "publish_date": row["publish_date"], "duration": row["duration"], "duration_seconds": row["duration_seconds"], }) progress.advance(task) with open(dst, "w", encoding="utf-8", newline="") as f: writer = csv.DictWriter(f, fieldnames=out_fields) writer.writeheader() writer.writerows(out_rows) print(f"Wrote {len(out_rows)} rows to {dst}") if __name__ == "__main__": main()