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()