File size: 2,322 Bytes
4f81a3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""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()