-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtxt2srt.py
187 lines (148 loc) · 5.36 KB
/
txt2srt.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# written by deciMae, minorly edited by glumbaron
import sys
import tkinter as tk
import tkinter.filedialog as fd
numlines = 0
importf = ""
timedelta = 0
total = 0
updating = False
def openfile():
global importf
global timedelta
global total
importf = fd.askopenfilename()
if(importf == ""):
return 0
getnumlines()
importfVar.set(importf)
if(timedelta == 5 and total != 0):
updatefromtotal()
else:
updatefromdelta()
return importf
def getnumlines():
global numlines
if(importf == ""):
numlines = 0
else:
numlines = sum(1 if line != "\n" else 0 for line in open(importf, "r"))
numlinesVar.set(numlines)
def updatefromdelta(*args):
global total
global timedelta
global updating
if(updating):
print("hi")
return
updating = True
if((not timedeltaVar.get().isnumeric()) or int(timedeltaVar.get()) <= 0):
timedeltaVar.set(str(timedelta))
else :
timedelta = int(timedeltaVar.get())
if(importf != ""):
total = timedelta * numlines
seconds = total % 60
totalsVar.set("%02d"% (seconds) )
totalmVar.set(str(int(total/60)))
updating = False
def updatefromtotal(*args):
global updating
global timedelta
global total
if(updating):
return
updating = True
if ( (not totalsVar.get().isnumeric()) or (not totalmVar.get().isnumeric()) or int(totalsVar.get()) > 59 or int(totalsVar.get()) < 0 or int(totalmVar.get()) < 0 or int(totalmVar.get()) + int(totalsVar.get()) == 0):
seconds = total % 60
totalsVar.set("%02d"% (seconds))
totalmVar.set(str(int(total/60)))
else:
total = int(totalsVar.get()) + int(totalmVar.get())*60
if(importf != ""):
timedelta = int(total/numlines)
timedeltaVar.set(str(timedelta))
updating = False
def run():
exportf = fd.asksaveasfilename(defaultextension = ".srt")
file = open(importf, "r")
outfile = open(exportf, "w")
i = 0
for x in file:
if x == "\n":
continue
outfile.write(str(i) + "\n")
time = i * timedelta
nexttime = (i + 1) * timedelta
hour = int(time / 3600)
min = int(time / 60) % 60
sec = int(time) % 60
nexthour = int(nexttime / 3600)
nextmin = int(nexttime / 60) % 60
nextsec = int(nexttime) % 60
outfile.write("%02d:%02d:%02d,000 --> %02d:%02d:%02d,000\n" %
(hour, min, sec, nexthour, nextmin, nextsec))
outfile.write(x + "\n")
i += 1
exit()
window = tk.Tk()
window.title("txt2srt")
window.resizable(False,False)
tk.Label(window, text = "Import from:").grid(row = 0,column = 0)
importfVar = tk.StringVar()
tk.Entry(window, width = 60, textvariable = importfVar, state = "disabled").grid(row = 0, column = 1,columnspan = 5)
tk.Button(window, text = "Select", command = openfile).grid(row = 0, column = 6, columnspan = 3)
tk.Label(window, text = "Number of lines:").grid(row = 1, column = 0, columnspan = 2)
numlinesVar = tk.IntVar()
tk.Entry(window, width = 5, textvariable = numlinesVar, state = "disabled", justify = "right").grid(row = 1, column = 2)
tk.Label(window, text = "Time per line (seconds):").grid(row = 1, column = 3)
timedeltaVar = tk.StringVar()
timedeltaVar.set("5")
timedeltaVar.trace("w",updatefromdelta)
tk.Entry(window, width = 5, validate = "focusout", textvariable = timedeltaVar, justify = "right").grid(row = 1, column = 4)
tk.Label(window, text = "Total time (min:seconds)").grid(row = 1, column = 5)
totalmVar = tk.StringVar()
totalmVar.set("0")
totalmVar.trace("w",updatefromtotal)
tk.Entry(window, width = 5, validate = "focusout", textvariable = totalmVar, justify = "right").grid(row = 1, column = 6)
tk.Label(window, text = ":").grid(row = 1, column = 7)
totalsVar = tk.StringVar()
totalsVar.set("00")
totalsVar.trace("w",updatefromtotal)
tk.Entry(window, width = 2, validate = "focusout", textvariable = totalsVar, justify = "right").grid(row = 1, column = 8)
tk.Button(window, text = "Convert", command = run).grid(row = 2, column = 6, columnspan = 3)
window.mainloop()
# exportf = sys.argv[2]
# importf = sys.argv[1]
# if len(sys.argv) - 1 == 3:
# maxmins, maxsecs = sys.argv[3].split(":")
# maxtime = int(maxmins) * 60 + int(maxsecs)
# else:
# maxtime = 0
# try:
# file = open(importf, "r")
# except:
# print("Error: file not found")
# numlines = sum(1 if line != "\n" else 0 for line in open(importf, "r"))
# if maxtime != 0:
# timedelta = maxtime / numlines
# else:
# timedelta = 2
# outfile = open(exportf, "w")
# i = 0
# for x in file:
# if x == "\n":
# continue
# outfile.write(str(i) + "\n")
# time = i * timedelta
# nexttime = (i + 1) * timedelta
# hour = int(time / 3600)
# min = int(time / 60) % 60
# sec = int(time) % 60
# nexthour = int(nexttime / 3600)
# nextmin = int(nexttime / 60) % 60
# nextsec = int(nexttime) % 60
# outfile.write("%02d:%02d:%02d,000 --> %02d:%02d:%02d,000\n" %
# (hour, min, sec, nexthour, nextmin, nextsec))
# outfile.write(x + "\n")
# i += 1