This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_to_srt.py
53 lines (42 loc) · 1.53 KB
/
text_to_srt.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Convert Ultraschall chaptermarks to srt format
"""
class entries():
def __init__(self, total_length):
self.total_length = total_length
self.data = []
def add(self, entry):
self.data.append(entry)
def calculate(self):
for i in range(0, len(self.data) - 1):
self.data[i].end = self.data[i + 1].start.replace(".", ",")
self.data[-1].end = self.total_length.replace(".", ",")
def write_srt(self, open_file):
self.calculate()
for i, e in enumerate(self.data):
open_file.write("{}\n".format(i + 1))
open_file.write("{} --> {}\n".format(e.start, e.end))
open_file.write("{}\n".format(e.text))
open_file.write("\n")
class entry():
def __init__(self, text, start, end=None):
self.text = text
self.start = start
self.end = end
def convert_file(from_file, to_file, length_of_file):
e = entries(length_of_file)
with open(from_file, "r") as source_file:
for line in source_file:
time, text = line.split(" ", 1)
time = time.replace(".", ",")
en = entry(text.strip(), time.strip())
e.add(en)
with open(to_file, "w") as target_file:
e.write_srt(target_file)
# if __name__ == '__main__':
# # Calculate total length:
# # MP4Box -info good.m4a 2>&1| grep -i "track duration is"
# # We get the result: "02:22:52.731"
# convertfile(sys.argv[1], sys.argv[2], "02:22:52,731")