"""Rebuild audio for the sea-asr label datasets from the source videos. The datasets ship no audio. Pattern from PleIAs, YouTube-Commons (https://huggingface.co/datasets/PleIAs/YouTube-Commons): publish transcripts and source ids; users fetch the audio themselves from the original Creative Commons videos. pip install pyarrow soundfile numpy yt-dlp # and ffmpeg on PATH python fetch_audio.py --data data/ --out audio/ [--limit-videos 10] Writes /.flac (16 kHz mono) and /manifest.jsonl ({segment_id, audio, text, start, end}). Each video stays under its creator's CC-BY licence: keep the attribution (ATTRIBUTION.tsv) with any copy you make. Videos can be deleted or changed upstream; segments whose source is gone are skipped and counted. """ import argparse import glob import json import os import subprocess import tempfile import numpy as np import pyarrow.parquet as pq import soundfile as sf SR = 16000 def fetch_video(video_id: str, tmp: str) -> np.ndarray | None: """Best audio of one YouTube video as 16 kHz mono float32, or None if unavailable.""" path = os.path.join(tmp, video_id) r = subprocess.run(["yt-dlp", "-q", "--no-warnings", "-f", "bestaudio", "-o", path + ".%(ext)s", f"https://www.youtube.com/watch?v={video_id}"], capture_output=True) files = glob.glob(path + ".*") if r.returncode != 0 or not files: return None raw = subprocess.run(["ffmpeg", "-nostdin", "-loglevel", "error", "-i", files[0], "-ac", "1", "-ar", str(SR), "-f", "f32le", "-"], capture_output=True).stdout for f in files: os.remove(f) return np.frombuffer(raw, dtype=np.float32) if raw else None def main(): ap = argparse.ArgumentParser() ap.add_argument("--data", required=True, help="folder with the dataset's data-*.parquet files") ap.add_argument("--out", required=True) ap.add_argument("--limit-videos", type=int, default=0) a = ap.parse_args() os.makedirs(a.out, exist_ok=True) cols = ["source_id", "segment_id", "start", "end", "text_norm"] rows = [r for f in sorted(glob.glob(os.path.join(a.data, "*.parquet"))) for r in pq.read_table(f, columns=cols).to_pylist()] by_vid: dict[str, list[dict]] = {} for r in rows: by_vid.setdefault(r["source_id"], []).append(r) vids = sorted(by_vid)[: a.limit_videos or None] done = gone = 0 with tempfile.TemporaryDirectory() as tmp, open(os.path.join(a.out, "manifest.jsonl"), "a", encoding="utf-8") as man: for vid in vids: wav = fetch_video(vid, tmp) if wav is None: gone += len(by_vid[vid]) continue for r in by_vid[vid]: seg = wav[int(r["start"] * SR): int(r["end"] * SR)] if len(seg) < SR // 2: gone += 1 continue path = os.path.join(a.out, r["segment_id"] + ".flac") sf.write(path, seg, SR) man.write(json.dumps({"segment_id": r["segment_id"], "audio": path, "text": r["text_norm"], "start": r["start"], "end": r["end"]}, ensure_ascii=False) + "\n") done += 1 print(f"segments written {done}, skipped (video gone or too short) {gone}") if __name__ == "__main__": main()