adeeb-khoja commited on
Commit
3c6aaf5
·
verified ·
1 Parent(s): 87e8f23

Upload srt_generator.py

Browse files
Files changed (1) hide show
  1. helpers/srt_generator.py +40 -0
helpers/srt_generator.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import timedelta
2
+ import json
3
+
4
+ # Sample list of subtitle objects
5
+ subtitles = [
6
+ {
7
+ "text": " The entire world has been suffering due to the climate change dilemma.",
8
+ "start": 0.0,
9
+ "end": 9.88,
10
+ "id": 0
11
+ },
12
+ # Add more subtitle objects here
13
+ ]
14
+
15
+ class SRTGenerator(object):
16
+
17
+ @classmethod
18
+ def format_time(cls,seconds):
19
+ """Convert seconds to SRT time format (hh:mm:ss,ms)"""
20
+ ms = int((seconds - int(seconds)) * 1000)
21
+ td = str(timedelta(seconds=int(seconds)))
22
+ return f"{td},{ms:03d}"
23
+ @classmethod
24
+ def generate_srt(cls,subtitles, output_file):
25
+ with open(output_file, 'w') as f:
26
+ for sub in subtitles:
27
+ start_time = cls.format_time(sub['start'])
28
+ end_time = cls.format_time(sub['end'])
29
+ text = sub['text'].strip()
30
+ srt_entry = f"{sub['id'] + 1}\n{start_time} --> {end_time}\n{text}\n\n"
31
+ f.write(srt_entry)
32
+
33
+
34
+
35
+ if __name__ == "__main__":
36
+ segments_file = "segments.json"
37
+ with open(segments_file, 'r') as f:
38
+ segments = json.load(f)
39
+ output_srt_file = "subtitles.srt"
40
+ SRTGenerator.generate_srt(segments, output_srt_file)