import re, os, sys

def vtt_to_text(vtt_path, out_path):
    with open(vtt_path, "r", encoding="utf-8-sig") as f:
        content = f.read()
    lines = content.splitlines()
    clean = []
    for line in lines:
        stripped = line.strip()
        if not stripped:
            continue
        if stripped.startswith("WEBVTT"):
            continue
        if stripped.startswith("Kind:"):
            continue
        if stripped.startswith("Language:"):
            continue
        if "-->" in stripped:
            continue
        if re.match(r"^\d+$", stripped):
            continue
        cleaned = re.sub(r"<[^>]*>", "", stripped)
        cleaned = re.sub(r"<[0-9:.]+>", "", cleaned)
        cleaned = cleaned.replace("\u200b", " ")
        cleaned = re.sub(r"\s+", " ", cleaned).strip()
        if not cleaned:
            continue
        if clean and clean[-1] == cleaned:
            continue
        clean.append(cleaned)
    text = "\n".join(clean)
    with open(out_path, "w", encoding="utf-8") as f:
        f.write(text)

if __name__ == "__main__":
    raw_dir = sys.argv[1]
    out_dir = sys.argv[2]
    os.makedirs(out_dir, exist_ok=True)
    for fn in sorted(os.listdir(raw_dir)):
        if not fn.endswith(".vtt"):
            continue
        base = fn.split(".")[0]
        if "." in base:
            base = base.split(".")[0]
        vtt_to_text(os.path.join(raw_dir, fn), os.path.join(out_dir, base + ".txt"))
        print(f"cleaned {fn} -> {base}.txt")
