-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathsubtitle_synchronizer.py
65 lines (50 loc) · 2 KB
/
subtitle_synchronizer.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
54
55
56
57
58
59
60
61
62
63
64
65
def convertToMilliseconds(time):
milliseconds = 0
hh,mm,ms = time.split(":")
ms = ms.strip().replace(",","")
hh = int(hh)
mm = int(mm)
ms = int(ms)
milliseconds = hh*3600000+mm*60000+ms
return milliseconds
def synchronize(time,shift):
return time-shift
def convertToTime(milliseconds):
hh = milliseconds//3600000
milliseconds = milliseconds%3600000
hh = str(hh)
if len(hh) < 2: hh = "0"+hh
mm = milliseconds//60000
milliseconds = milliseconds%60000
mm = str(mm)
if len(mm) < 2: mm = "0"+mm
ss = milliseconds//1000
milliseconds = milliseconds%1000
ss = str(ss)
if len(ss) < 2: ss = "0"+ss
milliseconds = str(milliseconds)
while len(milliseconds) < 3:
milliseconds = "0"+milliseconds
return f"{hh}:{mm}:{ss},{milliseconds}"
def main():
SHIFT = int(input("Please enter the shift in milliseconds: "))
f = open("input.txt", "r", errors="ignore")
output = open("output.txt","a")
for x in f:
if "-->" in x:
start,end = x.split("-->")
start,end = convertToMilliseconds(start), convertToMilliseconds(end)
start,end = synchronize(start,SHIFT),synchronize(end,SHIFT)
start,end = convertToTime(start),convertToTime(end)
output.write(f"{start} --> {end}\n")
else:
output.write(x)
#Right click the subtitles file you want to synchronize and open it in a notepad
#Then copy all of its content and paste it into a new file and name it as "input.txt"
#You must have your "input.txt" in the same folder as this program
#Once you run this program you will be asked to enter a shift in milliseconds.
#You can find the shift when you are watching the video as it is impossible to
#determine that using any program since it is variable for every video.
#Once the program has finished running, you will have your subtitles synchronized
#in a file that says "output.txt". Copy its content and paste it into your original subtitles file.
main()