File size: 1,659 Bytes
b165696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import unicodedata
import re


def normalize(text: str) -> str:
    """Normalize text for comparison: NFC, collapse whitespace, strip punctuation."""
    if not text:
        return ""

    # Unicode canonical composition (important for Indic scripts)
    text = unicodedata.normalize("NFC", text)

    # Remove HTML tags that sometimes appear in subtitle files
    text = re.sub(r"<[^>]+>", "", text)

    # Strip common subtitle formatting markers
    text = re.sub(r"\{[^}]*\}", "", text)  # {i}, {b}, etc.
    text = re.sub(r"\\[nN]", " ", text)    # \n, \N line breaks

    # Collapse whitespace
    text = re.sub(r"\s+", " ", text).strip()

    # Keep letters (L*), combining marks/diacritics (M*), digits (N*),
    # whitespace, and the Devanagari danda (।).
    # We must NOT use \w here — it misses Indic vowel signs (category Mc/Mn).
    kept = []
    for ch in text:
        cat = unicodedata.category(ch)
        if cat[0] in ("L", "M", "N") or ch in (" ", "।", "_"):
            kept.append(ch)
    text = "".join(kept)

    # Lower-case Latin characters (Indic scripts are case-insensitive by nature)
    text = text.lower()

    return text.strip()


def format_timestamp(seconds: float) -> str:
    """Convert float seconds → human-readable 'MM:SS.s' string."""
    mins = int(seconds // 60)
    secs = seconds % 60
    return f"{mins:02d}:{secs:05.2f}"


def seconds_to_srt_time(seconds: float) -> str:
    """Convert seconds → SRT timestamp HH:MM:SS,mmm."""
    h = int(seconds // 3600)
    m = int((seconds % 3600) // 60)
    s = int(seconds % 60)
    ms = int((seconds % 1) * 1000)
    return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"