import os
import re

def convert_srt_to_vtt(path):
    with open(path, "r", encoding="utf-8", errors="ignore") as f:
        lines = f.readlines()

    new_lines = []
    has_header = False

    for i, line in enumerate(lines):
        stripped = line.strip()

        # حذف شماره‌های خط (فقط یک عدد تنها)
        if re.match(r"^\d+$", stripped):
            continue

        # اصلاح تایم‌کد: 00:00:01,000 --> 00:00:05,000
        if "-->" in line:
            line = line.replace(",", ".")  # جایگزینی کاما با نقطه

        # اگر فایل قبلاً WEBVTT داشت دوباره نذار
        if i == 0 and stripped.upper().startswith("WEBVTT"):
            has_header = True

        new_lines.append(line)

    # اضافه کردن هدر WEBVTT اگر نبود
    if not has_header:
        new_lines.insert(0, "WEBVTT\n\n")

    with open(path, "w", encoding="utf-8") as f:
        f.writelines(new_lines)

    print(f"✅ تبدیل شد: {path}")

def scan_folder(root):
    for dirpath, _, filenames in os.walk(root):
        for filename in filenames:
            if filename.lower().endswith(".vtt"):
                convert_srt_to_vtt(os.path.join(dirpath, filename))

if __name__ == "__main__":
    scan_folder(os.getcwd())

