File size: 2,227 Bytes
add8e69
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
from pathlib import Path
import sys

BASE_DIR = Path("/home/zaibi/code/recitation-all")
sys.path.append(str(BASE_DIR))

import recitation_pb2

def main():
    reciters = []
    for p in BASE_DIR.iterdir():
        if p.is_dir() and not p.name.startswith(".") and p.name not in ["audio", "wbw_tester"]:
            if (p / "001").exists():
                reciters.append(p)

    print(f"Found {len(reciters)} reciters to process.")

    count = 0
    for reciter_dir in reciters:
        print(f"Converting JSON to Protobuf for {reciter_dir.name}...")
        for i in range(1, 115):
            surah_id = f"{i:03d}"
            surah_dir = reciter_dir / surah_id
            json_path = surah_dir / f"{surah_id}.json"
            pb_path = surah_dir / f"{surah_id}.pb"
            
            if not json_path.exists():
                continue
                
            try:
                with open(json_path, 'r') as f:
                    data = json.load(f)
                
                # Create protobuf message
                surah_ts = recitation_pb2.SurahTimestamps()
                
                for verse_key, verse_data in data.items():
                    verse_seg = recitation_pb2.VerseSegments()
                    for segment in verse_data.get("segments", []):
                        # Format: [0-based, 1-based, start_ms, end_ms]
                        word_seg = verse_seg.segments.add()
                        word_seg.word_index_0_based = int(segment[0])
                        word_seg.word_index_1_based = int(segment[1])
                        word_seg.timestamp_from = int(segment[2])
                        word_seg.timestamp_to = int(segment[3])
                    
                    surah_ts.verses[verse_key].CopyFrom(verse_seg)
                    
                # Serialize to binary protobuf
                with open(pb_path, 'wb') as f:
                    f.write(surah_ts.SerializeToString())
                
                count += 1
            except Exception as e:
                print(f"Error converting {json_path}: {e}")

    print(f"Successfully converted {count} JSON files to Protobuf!")

if __name__ == "__main__":
    main()