6130220421 (74.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 23.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
3
00:01:55,500 --> 00:02:10,600
BBB
4
00:59:40,100 --> 01:00:08,100
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
DDD
DD |
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
AAA
4
00:01:55,001 --> 00:01:56,000
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
6
00:01:56,998 --> 00:01:56,999
7
00:01:56,999 --> 00:04:22,100
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID : 6130220421
003: def inmili(t):
004: h,m,s = t.strip().split(":")
005: ss,ms = s.strip().split(",")
006: tt = int(ms)
007: if ss[0] == 0:
008: tt += int(ss[1])*1000
009: else:
010: tt += int(ss)*1000
011: tt += int(m)*60*1000
012: tt += int(h)*60*60*1000
013: return tt
014: def outmili(t):
015: h = t//(60*60*1000)
016: m = (t-h*(60*60*1000))//(60*1000)
017: s = (t-h*(60*60*1000)-m*(60*1000))
018: ss = s//1000
019: ms = s%1000
020: if len(str(h)) != 2:
021: h = "0"+str(h)
022: if len(str(m)) != 2:
023: m = "0"+str(m)
024: if len(str(ss)) != 2:
025: ss = "0"+str(ss)
026: if len(str(ms)) != 3:
027: if len(str(ms)) == 2:
028: ms = "0"+str(ms)
029: elif len(str(ms)) == 1:
030: ms = "00"+str(ms)
031: h = str(h)
032: m = str(m)
033: ss= str(ss)
034: ms= str(ms)
035: tt = [h+":"+m+":"+ss+","+ms]
036: return tt
037:
038: def sortdata(data):
039: din = []
040: dout = []
041: for no,start,stop,sub in data:
042: din.append([start,stop,no,sub])
043: din.sort()
044: for start,stop,no,sub in din:
045: dout.append([no,start,stop,sub])
046: for i in range(len(dout)):
047: dout[i][0] = str(i+1)+"\n"
048: return dout
049:
050: def read2list(file_in):
051: fin = open(file_in,"r",encoding="utf-8")
052: data = []
053: subdata = [""]*4
054: rl = fin.readline()
055: while len(rl) != 0:
056: if rl.strip().isnumeric():
057: subdata[0] = rl
058: elif "-->" in rl:
059: start,stop = rl.split("-->")
060: subdata[1] = inmili(start.strip())
061: subdata[2] = inmili(stop.strip())
062: elif rl == "\n":
063: subdata[3] += "\n"
064: data.append(subdata)
065: subdata = [""]*4
066: else:
067: subdata[3] += rl
068: rl = fin.readline()
069: fin.close()
070: return data
071:
072: def list2write(data,file_out):
073: fout = open(file_out, "w",encoding="utf-8")
074: for no,start,stop,sub in data:
075: fout.write(no)
076: fout.write(outmili(start)[0]+" --> "+outmili(stop)[0]+"\n")
077: fout.write(sub)
078: fout.close()
079: # code for clean
080: def cbtw(sub):
081: subout =""
082: pos = []
083: for i in range(len(sub)):
084: if sub[i] in "()[]{}<>":
085: pos.append(i)
086: if pos != []:
087: subout += sub[:pos[0]]
088: if len(pos) == 2:
089: subout += sub[pos[-1]+1:]
090: return subout
091: else:
092: for i in range(1,len(pos)-1,2):
093: subout += sub[pos[i]+1:pos[i+1]]
094: subout += sub[pos[-1]+1:]
095: return subout
096: else:
097: return sub
098:
099: def cn(sub):
100: resub = sub[::-1]
101: subout = ""
102: c = 1
103: for i in range(1,len(resub)):
104: if resub[i-1] == "\n" and resub[i] == "\n":
105: c+=1
106: else:
107: subout = "\n\n" + resub[c:]
108: return subout[::-1]
109: subout = "\n\n"
110: return subout
111: def chalnm(sub):
112: sb = ""
113: c = []
114: for e in sub:
115: if e.isalnum():
116: c.append("t")
117: else:
118: c.append("f")
119: if "t" not in c:
120: sb = ""
121: return sb
122: else:
123: sb += sub +"\n"
124: return sb
125: def chckalnm(sub):
126: ssb = ""
127: c =[]
128: cc = ''
129: for e in sub:
130: cc+=e
131: if e == "\n":
132: s = chalnm(cc[:-1])
133: ssb += s
134: cc = ""
135: return ssb
136: def estrip(sub): #***
137: subout = ""
138: for e in sub:
139: if e != "\n":
140: subout += e
141: elif e == "\n":
142: subout = subout.strip(" ") + "\n"
143: return subout
144: # code for merge
145: def find(dinmerge,base,threshold):
146: bsout = list(base)
147: tp = []
148: c = []
149: for e in base:
150: no,start,stop,sub = e
151: if abs(start-dinmerge[1]) <= threshold:
152: c.append([abs(start-dinmerge[1]),e])
153: tp.append(e)
154: c.sort()
155: if c != []:
156: bsout[base.index(c[0][1])][3] = base[base.index(c[0][1])][3][:-1] + dinmerge[3]
157: dinmerge = []
158: return bsout,dinmerge
159: # ------------------------------------------
160: def shift(file_in, time_shift, file_out):
161: data = read2list(file_in)
162: mdata = []
163: for no,start,stop,sub in data:
164: start = int(start) + int(time_shift)
165: stop = int(stop) + int(time_shift)
166: for i in range(len(data)):
167: data[i][1] = int(data[i][1]) + int(time_shift)
168: data[i][2] = int(data[i][2]) + int(time_shift)
169: if data[i][1] <= 0:
170: data[i][1] = 0
171: if data[i][2] <= 0:
172: data[i][2] = 0
173: for e in data:
174: if e[1] != e[2]:
175: mdata.append(e)
176: dout = sortdata(mdata)
177: list2write(dout,file_out)
178: return
179:
180: # ------------------------------------------
181: def merge(base_file, merge_file, threshold, file_out):
182: basein = read2list(base_file)
183: mergein = read2list(merge_file)
184: baseout = list(basein)
185: out = []
186: for e in mergein:
187: bsout,dinmerge = find(e,basein,threshold)
188: basein = bsout
189: if dinmerge == e:
190: out.append(e)
191: out += basein
192: dout = sortdata(out)
193: list2write(dout,file_out)
194: return
195:
196: # ------------------------------------------
197: def clean(file_in, file_out):
198: data = read2list(file_in)
199: din = []
200: dout = []
201: for no,start,stop,sub in data:
202: din.append([no,start,stop,cn(chckalnm(cbtw(sub)))])
203: for no,start,stop,sub in din:
204: if sub != "\n\n":
205: dout.append([no,start,stop,estrip(sub)])
206: dout = sortdata(dout)
207: list2write(dout,file_out)
208: return
209:
210: # ------------------------------------------
6430001121 (100.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430001121
003:
004: def readTime(time):
005: HH, MM, SS = time.split(':')
006: s, ms = SS.split(',')
007: time = [int (e) for e in [HH, MM, s, ms]]
008:
009: return time[3] + time[2]*1e3 + time[1]*60*1e3 + time[0]*60*60*1e3 # convert HH, MM, s, ms to ms
010:
011: def timeShift(time, shift):
012: time = readTime(time) + shift
013: if time < 0: time = 0
014: return time
015:
016: def time2Format(time):
017: t0 = time
018: # convert time(ms) to HH, MM, s, ms
019: HH = time//(60*60*1e3)
020: time -= HH*(60*60*1e3)
021: MM = time//(60*1e3)
022: time -= MM*(60*1e3)
023: s = time//1e3
024: time -= s*1e3
025: ms = time
026: # make into format
027: time = [str(int(e)) for e in [HH, MM, s, ms]]
028: for i in range(len(time)):
029: while len(time[i]) < 2: time[i] = '0' + time[i]
030: while len(time[3]) < 3: time[3] = '0' + time[3]
031: # check the code work
032: ret = time[0]+':'+time[1]+':'+time[2]+','+time[3]
033: if readTime(ret) != t0: print('Something went wrong with readTime or time2Format')
034:
035: return ret
036:
037: def getStartTime(time):
038: st, ed = time.strip().split(' --> ')
039: return readTime(st)
040:
041: def writeSub(subFile, fout):
042: sub = subFile.readline()
043: while sub != '\n':
044: fout.write(sub)
045: sub = subFile.readline()
046: return sub
047:
048: def keepSub(subFile):
049: ret = ''
050: sub = subFile.readline()
051: while sub != '\n':
052: ret += sub
053: sub = subFile.readline()
054: return ret
055:
056: def keepSubClean(subFile):
057: ret = ''
058: sub = subFile.readline()
059: while sub != '\n' and len(sub) != 0:
060: ret += sub
061: sub = subFile.readline()
062: return ret
063:
064: def writeKeepSub(sub, fout):
065: fout.write(sub)
066: return
067:
068: def writeSubNL(subFile, fout):
069: sub = subFile.readline()
070: while sub != '\n':
071: fout.write(sub)
072: sub = subFile.readline()
073: fout.write('\n')
074: return sub
075:
076: def writeSubNum(subNum, fout):
077: fout.write(str(subNum) + '\n')
078: subNum += 1
079: return subNum
080:
081: def goNextSub(subFile):
082: if len(subFile.readline()) == 0: return False, 0
083: ret = subFile.readline()
084: return getStartTime(ret), ret # return the start time of sub and time
085:
086: def cleanSub(sub):
087: ret = ''
088: j = 0
089: while j != len(sub):
090: if sub[j] in '([{<':
091: while sub[j] not in ')]}>':
092: j += 1
093: else:
094: if sub[j] not in ')]}>': ret += sub[j]
095: j += 1
096: sub = ret.split('\n')
097: lastRet = ''
098: for line in sub:
099: chk = False
100: for c in line:
101: if c.isalnum(): chk = True
102: if chk: lastRet += line.strip() + '\n'
103: return lastRet
104:
105: def checkSub(sub):
106: if len(sub) == 0: return False
107: for c in sub:
108: if c.isalnum(): return True
109: return False
110: # ------------------------------------------
111: def shift(file_in, time_shift, file_out):
112: fin = open(file_in, 'r', encoding='utf-8')
113: fout = open(file_out, 'w', encoding='utf-8')
114:
115: subNum = 1
116: while True:
117: lin = fin.readline()
118: if len(lin) == 0: break
119: st, ed = fin.readline().strip().split(' --> ') # get time (in format)
120: st = time2Format(timeShift(st, time_shift)) # shift and format
121: ed = time2Format(timeShift(ed, time_shift))
122: if st == ed == '00:00:00,000':
123: while fin.readline() != '\n': pass # read through unwanted line
124: continue # go to new loop
125: fout.write(str(subNum) + '\n'); subNum += 1
126: fout.write(st + ' --> ' + ed + '\n') # write new time format
127: while lin != '\n':
128: lin = fin.readline()
129: fout.write(lin) # write sub
130:
131: fin.close()
132: fout.close()
133: return
134:
135: # shift('D:\Python\\2110101_COMP_PROG\HW\hw08\SRT_Files\\' + 'test.srt', 4500, 'test1_shifted.srt')
136: # shift('D:\Python\\2110101_COMP_PROG\HW\hw08\SRT_Files\\' + 'test.srt', -5500, 'test2_shifted.srt')
137: # ------------------------------------------
138: def merge(base_file, merge_file, threshold, file_out):
139: baseSub = open(base_file, 'r', encoding='utf-8')
140: mergeSub = open(merge_file, 'r', encoding='utf-8')
141: fout = open(file_out, 'w', encoding='utf-8')
142:
143: subNum = 1
144: stBase, timeB = goNextSub(baseSub)
145: stMerge, timeM = goNextSub(mergeSub)
146: while stBase != False and stMerge != False:
147: if abs(stBase - stMerge) <= threshold: # compare d with multiple sub ****
148: stime = stBase
149: stimeB = timeB
150: dtime = abs(stBase - stMerge)
151: sub = keepSub(baseSub)
152: stBase, timeB = goNextSub(baseSub) # competible with next baseSub
153: stNew = stBase
154: dnew = abs(stBase - stMerge)
155: if dtime < dnew:
156: subNum = writeSubNum(subNum, fout)
157: fout.write(stimeB)
158: writeKeepSub(sub, fout) # merge
159: writeSub(mergeSub, fout)
160: stMerge, timeM = goNextSub(mergeSub)
161: while abs(stMerge - stime) < abs(stMerge - stNew) and abs(stMerge - stime) <= threshold:
162: writeSub(mergeSub, fout) # more merge
163: stMerge, timeM = goNextSub(mergeSub)
164: fout.write('\n')
165: elif dtime > dnew:
166: subNum = writeSubNum(subNum, fout)
167: fout.write(stimeB)
168: writeKeepSub(sub, fout)
169: fout.write('\n')
170: elif stBase < stMerge:
171: subNum = writeSubNum(subNum, fout)
172: fout.write(timeB)
173: writeSubNL(baseSub, fout)
174: stBase, timeB = goNextSub(baseSub)
175: elif stBase > stMerge:
176: subNum = writeSubNum(subNum, fout)
177: fout.write(timeM)
178: writeSubNL(mergeSub, fout)
179: stMerge, timeM = goNextSub(mergeSub)
180:
181: while stMerge != False:
182: subNum = writeSubNum(subNum, fout)
183: fout.write(timeM)
184: writeSubNL(mergeSub, fout)
185: stMerge, timeM = goNextSub(mergeSub)
186:
187: while stBase != False:
188: subNum = writeSubNum(subNum, fout)
189: fout.write(timeB)
190: writeSubNL(baseSub, fout)
191: stBase, timeB = goNextSub(baseSub)
192:
193: #if stBase == False and stMerge == False: print('Merge Complete')
194:
195: baseSub.close()
196: mergeSub.close()
197: fout.close()
198: return
199:
200: # dir = 'D:\Python\\2110101_COMP_PROG\HW\hw08\SRT_Files\\'
201: # # test2
202: # merge('test2_en.srt', 'test2_th.srt', 1000, 'test2_en_th_mymerge.srt')
203: # merge('test2_th.srt', 'test2_en.srt', 1000, 'test2_th_en_mymerge.srt')
204:
205: # # test3
206: # merge(dir + 'test3_en.srt', dir + 'test3_th.srt', 1000, 'test3_en_th_mymerge.srt')
207: # merge(dir + 'test3_th.srt', dir + 'test3_en.srt', 1000, 'test3_th_en_mymerge.srt')
208:
209: # # test4
210: # merge(dir + 'test4_en.srt', dir + 'test4_th.srt', 1000, 'test4_en_th_mymerge.srt')
211: # merge(dir + 'test4_th.srt', dir + 'test4_en.srt', 1000, 'test4_th_en_mymerge.srt')
212: # ------------------------------------------
213: def clean(file_in, file_out):
214: fin = open(file_in, 'r', encoding='utf-8')
215: fout = open(file_out, 'w', encoding='utf-8')
216:
217: subNum = 1
218: line = fin.readline() # subNum 1
219: while len(line) != 0:
220: line = fin.readline() # time
221: time = line
222: sub = keepSubClean(fin)
223: sub = cleanSub(sub)
224: sub = sub.strip() # should strip from every line
225: if checkSub(sub): # delete line in cleanSub that no num&alphabet maybe split() from \n in sub ***
226: subNum = writeSubNum(subNum, fout)
227: fout.write(time)
228: writeKeepSub(sub, fout)
229: fout.write('\n\n')
230: fin.readline()
231:
232: #print('Clean Complete')
233: fin.close()
234: fout.close()
235: return
236:
237: # dir = 'D:\Python\\2110101_COMP_PROG\HW\hw08\SRT_Files\\'
238: # # test
239: # clean('test.srt', 'test_cleaned.srt')
240:
241: # # test2
242: # clean('test2_en.srt', 'test2_en_mycleaned.srt')
243: # clean('test2_th.srt', 'test2_th_mycleaned.srt')
244:
245: # # test3
246: # clean(dir + 'test3_en.srt', 'test3_en_mycleaned.srt')
247: # clean(dir + 'test3_th.srt', 'test3_th_mycleaned.srt')
248:
249: # # test4
250: # clean(dir + 'test4_en.srt', 'test4_en_mycleaned.srt')
251: # clean(dir + 'test4_th.srt', 'test4_th_mycleaned.srt')
252:
253: # check clean func
254: #clean('testCleanSub.srt', 'testCleanSubOP.srt')
255: # ------------------------------------------
6430003421 (0.0%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 275, in shift
organ=read_and_organ_and_sort(file_in)
File "z:\HW08\_student.py", line 115, in read_and_organ_and_sort
organ=organ_v1(data)
File "z:\HW08\_student.py", line 88, in organ_v1
index[0]=int(index[0])
, stdout = , close_file = |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 275, in shift
organ=read_and_organ_and_sort(file_in)
File "z:\HW08\_student.py", line 115, in read_and_organ_and_sort
organ=organ_v1(data)
File "z:\HW08\_student.py", line 88, in organ_v1
index[0]=int(index[0])
, stdout = , close_file = |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 275, in shift
organ=read_and_organ_and_sort(file_in)
File "z:\HW08\_student.py", line 115, in read_and_organ_and_sort
organ=organ_v1(data)
File "z:\HW08\_student.py", line 88, in organ_v1
index[0]=int(index[0])
, stdout = , close_file = |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 0.0%
[30, 'clean_1.srt', 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 418, in clean
if "<" in e[2] or ">" in e[2]:
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 422, in clean
change_v1(forgan,"[","]",e)
File "z:\HW08\_student.py", line 254, in change_v1
listin[i][2+k]+=" "
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 422, in clean
change_v1(forgan,"[","]",e)
File "z:\HW08\_student.py", line 254, in change_v1
listin[i][2+k]+=" "
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 419, in clean
change_v1(forgan,"<",">",e)
File "z:\HW08\_student.py", line 254, in change_v1
listin[i][2+k]+=" "
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 303, in merge
blist=organ_v3(basedata)
File "z:\HW08\_student.py", line 222, in organ_v3
time_calculate(res)
File "z:\HW08\_student.py", line 201, in time_calculate
lists[i].append(time_to_mmsec(lists[i][0][0]))
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 484, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 303, in merge
blist=organ_v3(basedata)
File "z:\HW08\_student.py", line 222, in organ_v3
time_calculate(res)
File "z:\HW08\_student.py", line 201, in time_calculate
lists[i].append(time_to_mmsec(lists[i][0][0]))
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: def change(inputs,what,to_what):
002: re=""
003: for e in inputs:
004: if e == what:
005: re+=to_what
006: else:
007: re+=e
008: return re
009: def read_time(time):#['00:00:11:000', '00:00:12:500']
010: re=[]
011: res1={}
012: res2={}
013: #-------------------------------
014: t1=time[0].split(":")
015: #print("t1",t1)
016: res1["hour"]=int(t1[0])
017: res1["minute"]=int(t1[1])
018: res1["sec"]=int(t1[2])
019: res1["mmsec"]=int(t1[3])
020: re.append(res1)
021: #-------------------------------
022: t2=time[1].split(":")
023: res2["hour"]=int(t2[0])
024: res2["minute"]=int(t2[1])
025: res2["sec"]=int(t2[2])
026: res2["mmsec"]=int(t2[3])
027: re.append(res2)
028: return re
029:
030: def read_time_single(time):#['00:00:11:000', '00:00:12:500']
031: re=[]
032: res1={}
033: #-------------------------------
034: t1=time.split(":")
035: #print("t1",t1)
036: res1["hour"]=int(t1[0])
037: res1["minute"]=int(t1[1])
038: res1["sec"]=int(t1[2])
039: res1["mmsec"]=int(t1[3])
040: re.append(res1)
041: return re
042:
043: def time_to_mmsec(e):#{'hour': 0, 'minute': 0, 'sec': 8, 'mmsec': 0}
044: total=e['hour']*3600000 + e['minute']*60000 + e['sec']*1000 + e["mmsec"]
045: return total
046:
047: def mmsec_to_time(t):
048: mmsec=t%1000
049: normal=t//1000
050: hour=normal//3600
051: mi=normal%3600
052: minute=mi//60
053: sec=mi%60
054: return {'hour': hour, 'minute': minute, 'sec': sec, 'mmsec': mmsec}
055:
056: def read_v2(file_in):
057: f=open(file_in,encoding='utf-8')
058: data=f.readlines()
059: return data
060: def read_v1(file_in):
061: f=open(file_in,encoding='utf-8')
062: data=[]
063: for line in f:
064: if line=="\n":
065: data.append("\n")
066: else:
067: data.append(str(line[:-1:]))
068: f.close()
069: return data
070: def organ_v1(data):
071: organ=[]
072: k=[]
073: for d in range(len(data)):
074: if data[d]=="\n":
075: if k==[]:
076: pass
077: else:
078: organ.append(k)
079: k=[]
080: organ.append("\n")
081: else:
082: k.append(data[d])
083: organ.append(k)
084: for index in organ:
085: if index=="\n":
086: pass
087: else:
088: index[0]=int(index[0])
089: index[1]=change(index[1],",",":")
090: index[1]=index[1].split(" --> ")
091: for ind in organ:
092: if ind == "\n":
093: pass
094: else:
095: time=ind[1]
096: #print(time)
097: ind[1]=read_time(time)
098:
099: return organ
100: def make_printable_v1(ei1):
101: #[{'hour': 0, 'minute': 0, 'sec': 4, 'mmsec': 0}, {'hour': 0, 'minute': 0, 'sec': 5, 'mmsec': 0}]
102: t1=ei1[0]
103: t2=ei1[1]
104: re=str("%02d"%t1['hour'])+":"+str("%02d"%t1["minute"])+":"+str("%02d"%t1["sec"])+","+str("%03d"%t1["mmsec"])+\
105: " --> "+str("%02d"%t2['hour'])+":"+str("%02d"%t2["minute"])+":"+str("%02d"%t2["sec"])+","+str("%03d"%t2["mmsec"])
106: return re
107:
108: def read_and_organ(file_in):
109: data=read_v2(file_in)
110: organ=organ_v1(data)
111: return organ
112:
113: def read_and_organ_and_sort(file_in):
114: data=read_v2(file_in)
115: organ=organ_v1(data)
116: organ1=[]
117: c=0
118: for l in organ:
119: if l =="\n":
120: c+=1
121: else:
122: organ1.append(l)
123: time=[]
124: re=[]
125:
126: for e in organ1:
127: if e !="\n":
128: t1=e[1][0]
129: time1=time_to_mmsec(t1)
130: time.append(time1)
131: elif e=="\n":
132: pass
133: timesort=sorted(time)
134: for r in range(len(time)):
135: re.append(organ1[time.index(timesort[r])])
136: re1=re
137: for i in range(len(re)):
138: re1.insert(2*i+1,"\n")
139: return re1
140: def organ_v2(base):
141: basef=base+["l"]
142: basef=basef[0:-1:]
143: print("baskf",basef)
144: for e in range(len(basef)):
145: basef[e]=basef[e][1::]
146: basef1=[]
147: for f in basef:
148: if f =="":
149: pass
150: else:
151: basef1.append(f)
152: print("baskf1=",basef1)
153: for e in range(len(basef1)):
154: basef1[e].append(time_to_mmsec(basef1[e][0][0]))
155: print("baskf12=",basef1)
156: return basef1
157: def fileout_writting_v1(organ,file_out):
158: fout = open(file_out, 'w', encoding='utf-8')
159: resault=[]
160: c=1
161: for l in range(len(organ)):
162: if (organ[l] !="\n") and ('not use' not in organ[l]):
163: for r in range(len(organ[l])):
164: if r==0:
165: resault.append(str(c))
166: elif r==1:
167: resault.append(make_printable_v1(organ[l][1]))
168: elif r==l:
169: resault.append(organ[l][r])#+"\n")
170: else:
171: resault.append(organ[l][r])
172: c+=1
173: elif organ[l] =="\n":
174: resault+=["\n"]
175: else:
176: pass
177: #print("resault = ",resault)
178: for eachstr in range(len(resault)):
179: if "\n" not in resault[eachstr]:
180: resault[eachstr]+="\n"
181: if resault[eachstr]=="\n":
182: pass
183: #print("resault = ",resault)
184: for obj in resault:
185: fout.write(obj)
186: #fout.write("\n")
187: fout.close()
188:
189: def time_in_list(lists):
190: for e in range(len(lists)):
191: if "-->" in lists[e] and ":" in lists[e] and "," in lists[e]:
192: lists[e]=lists[e][:-2:]
193: else:
194: pass
195: def is_time(string):
196: if "-->" in string and ":" in string and "," in string:
197: return True
198: return False
199: def time_calculate(lists):
200: for i in range(len(lists)):
201: lists[i].append(time_to_mmsec(lists[i][0][0]))
202: def organ_v3(lists):
203: time_in_list(lists)
204: res=[]
205: k=[]
206: for ee in lists:
207: if ee=="\n":
208: if k==[]:
209: res.append("/n")
210: else:
211: res.append(k[1::])
212: k=[]
213: else:
214: if is_time(ee):
215: t=ee.split(" --> ")
216: for e in range(2):
217: t[e]=change(t[e],",",":")
218: k.append(read_time(t))
219: else:
220: k.append(ee)
221: res.append(k[1::])
222: time_calculate(res)
223: return res
224: def organ_v4(lists):
225: res=[]
226: k=[]
227: for ee in lists:
228: #print(" ee =",ee)
229: if ee =="\n":
230: res.append(k)
231: res.append("\n")
232: k=[]
233: else:
234: k.append(ee)
235: res.append(k)
236: return res
237:
238: def make_printable_v2(listresault,list_of_what_to_add,count):
239: listresault+=[str(count)+"\n",make_printable_v1(list_of_what_to_add[0])+"\n"]+\
240: [sub for sub in list_of_what_to_add[1:-1:]]
241:
242: def change_v1(listin,firstsym,lastsym,e):
243: i=listin.index(e)
244: k=len(listin[i][2::])
245: #print("k=",k)
246: if k==1:
247: listin[i][2]+=" "
248: first=listin[i][2].index(firstsym)
249: last=listin[i][2].index(lastsym)
250: listin[i][2]=listin[i][2][:first:]+listin[i][2][last+1::]
251: listin[i][2].strip()
252: else:
253: for m in range(k):
254: listin[i][2+k]+=" "
255: first=listin[i][2+k].index(firstsym)
256: last=listin[i][2+k].index(lastsym)
257: listin[i][2+k]=listin[i][k+2][:first:]+listin[i][k+2][last+1::]
258: listin[i][2+k].strip()
259: def isalnum(string):
260: f=False
261: for e in string:
262: if e.isalnum():
263: f=True
264: else:
265: pass
266: break
267: return f
268:
269: def find_not_used(lists,used,not_used):
270: for a in range(len(lists)):
271: if a not in used:
272: not_used.append(a)
273: #------------------------------------------------------------------------------------
274: def shift(file_in,time_shift,file_out):
275: organ=read_and_organ_and_sort(file_in)
276: #print("organ1",organ)
277: for e in organ:
278: if e !="\n":
279: t1=e[1][0]
280: t2=e[1][1]
281: time1=time_to_mmsec(t1)+time_shift
282: time2=time_to_mmsec(t2)+time_shift
283: if time1<0 and time2<0:
284: e.append("not use")
285: elif time1<0 and time2>0:
286: e[0][0]={'hour': 0, 'minute': 0, 'sec': 0, 'mmsec': 0}
287: e[0][1]=mmsec_to_time(time2)
288: else:
289: e[1][0]=mmsec_to_time(time1)
290: e[1][1]=mmsec_to_time(time2)
291: #print("before sorted organ=",organ)
292: #organ.sort()
293: #print("organ",organ)
294: fileout_writting_v1(organ,file_out)
295: #------------------------------------------------------------------------------------
296: def merge(base_file, merge_file, threshold, file_out):
297: finbase=open(base_file,encoding='utf-8')
298: finmerge=open(merge_file,encoding='utf-8')
299: basedata=finbase.readlines()
300: mergedata=finmerge.readlines()
301: finbase.close()
302: finmerge.close()
303: blist=organ_v3(basedata)
304: mlist=organ_v3(mergedata)
305: #print("blist = ",blist)
306: #print("mlist = ",mlist)
307: #return
308: re1=[]
309: c=1
310: b_used=[]
311: m_used=[]
312: #print(blist[7])
313: for e in range(len(blist)):
314: absdif=[]
315: loca=[]#[[e,f],[e,f],[e,f],[e,f],[e,f]]
316: for f in range(len(mlist)):
317: #print("f = ",f)
318: d=mlist[f][-1]-blist[e][-1]
319: #print("d = ",d)
320: if abs(d)<=1000:
321: absdif.append(abs(d))
322: loca.append([e,f])
323: b_used.append(e)
324: m_used.append(f)
325:
326: else:
327: pass
328: #make_printable_v2(re1,mlist[f],c)
329: #re1+="\n"
330: #c+=1
331: #print("e and dif = ",e,dif)
332: #return
333: #print("---------------------------------------------------------------")
334: if absdif==[]:
335: pass
336: else:
337: difsort=sorted(absdif)
338: g=absdif.index(difsort[0])
339: locag=loca[g]
340: match=locag[1]
341: #blist[e]
342: #mlist[match]
343: re1+=[[make_printable_v1(blist[e][0])+"\n"]+\
344: [sub for sub in blist[e][1:-1:]]+\
345: [subthai for subthai in mlist[match][1:-1:]]]+["\n"]
346: c+=1
347: #print(re1)
348: #print("b_used =",b_used)
349: #print("m_used =",m_used)
350: b_notused=[]
351: m_notused=[]
352: find_not_used(blist,b_used,b_notused)
353: find_not_used(mlist,m_used,m_notused)
354: #print("b_notused =",b_notused)
355: #print("m_notused =",m_notused)
356: for e in b_notused:
357: #print("e = ",e)
358: #print("blist[e] = ",blist[e])
359: fe=[make_printable_v1(blist[e][0])+"\n"]
360: fe.append(blist[e][1:-1:])
361: re1.append(fe)
362: re1.append("\n")
363: for f in m_notused:
364: ff=[make_printable_v1(mlist[f][0])+"\n"]
365: ff.append(mlist[f][1:-1:])
366: re1.append(ff)
367: re1.append("\n")
368: #print("re1 = ",re1)
369: #return
370: re2=[]
371: for text in re1:
372: if text=="\n":
373: pass
374: else:
375: re2.append(text)
376: #print("re2 = ",re2)
377: time=[]
378: #print("re2[7] = ",re2[7])
379: #return
380: for t in re2:
381: ti=t[0][:-2:]
382: tia=ti.split(" --> ")[0]
383: tim=change(tia,",",":")
384: timedic=read_time_single(tim)
385: timevalue=time_to_mmsec(timedic[0])
386: time.append(timevalue)
387: #print("time =",time)
388: timesort=sorted(time)
389: #re2
390: re3=[]
391: for r in range(len(re2)):
392: re3.append(re2[time.index(timesort[r])])
393: re3.append("\n")
394: #print("re3=",re3)
395: #return
396: fout = open(file_out, 'w', encoding='utf-8')
397: for obj in re3:
398: fout.write(obj)
399: fout.close()
400: #merge("test2_en.srt","test2_th.srt", 1000, "merge output.srt")
401: #merge("merge base.txt","merge merge.txt", 1000, "merge output.txt")
402: def clean(file_in, file_out):
403: fin=open(file_in,encoding='utf-8')
404: f=fin.readlines()
405: fin.close()
406: #print("f = ",f)
407: #return
408: forgan=organ_v4(f)
409: #c.isalnum()
410: #print("forgan = ",forgan)
411: #return
412: for e in forgan:
413: if e =="\n":
414: pass
415: elif is_time(e):
416: pass
417: else:
418: if "<" in e[2] or ">" in e[2]:
419: change_v1(forgan,"<",">",e)
420:
421: if "[" in e[2] or "]" in e[2]:
422: change_v1(forgan,"[","]",e)
423:
424: if "(" in e[2] or ")" in e[2]:
425: change_v1(forgan,"(",")",e)
426:
427: if "{" in e[2] or "}" in e[2]:
428: change_v1(forgan,"{","}",e)
429: #print("forgan(1) = ",forgan)
430: #return
431: re=[]
432: for i in forgan:
433: if i=="\n":
434: re.append("\n")
435: else:
436: k=i[2::]
437: if len(k)==1:
438: if isalnum(i[2]):
439: for j in i:
440: re.append(j)
441: else:
442: for m in range(len(k)):
443: if isalnum(i[2+m]):
444: for j in i:
445: re.append(j)
446: fout = open(file_out, 'w', encoding='utf-8')
447: for obj in re:
448: fout.write(obj)
449: fout.close()
450: #clean("clean in.txt","clean out.txt")
6430009221 (52.67%)
shift -> 88.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 203, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 98, in merge
t1 = change_1(t_b[0])
File "z:\HW08\_student.py", line 27, in change_1
y = x[2].split(',')
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 203, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 98, in merge
t1 = change_1(t_b[0])
File "z:\HW08\_student.py", line 27, in change_1
y = x[2].split(',')
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID 6430009221 Name krittapas
003:
004: # ------------------------------------------
005: def fin_list_manage(file_in):
006: fin = open(file_in, 'r', encoding='utf-8')
007: galaxy_list = []
008: line = fin.readline().strip()
009: while True:
010: if line.isnumeric():
011: inside_list = []
012: inside_list.append(line)
013: elif line == '':
014: if len(inside_list) != 0:
015: inside_list.append(line)
016: galaxy_list.append(inside_list)
017: inside_list = []
018: else:
019: break
020: else:
021: inside_list.append(line)
022: line = fin.readline().strip()
023: fin.close()
024: return galaxy_list
025: def change_1(x):# 00:00:04,000 --> 4000msint# str --> int
026: x = x.strip().split(':')
027: y = x[2].split(',')
028: x[2] = y[0]+y[1]#04000
029: ms = (int(x[0])*(60**2)*1000) + int(x[1])*60*1000 + int(x[2])
030: return ms
031: def change_2(x):# 4000ms --> 00:00:04,000# str --> str
032: x = int(x.strip())
033: hr = str(x//3600000)
034: if len(hr)== 1:
035: hr = '0'+hr
036: min = str((x%3600000)//60000)
037: if len(min) == 1:
038: min = '0'+min
039: ms = str((x%3600000)%60000)
040: if len(ms) != 5:
041: ms = '0'+ms
042: if ms == '00':
043: ms = ms[0:2] + ',' +'000'
044: else:
045: ms = ms[0:2] + ',' + ms[2:]
046: return hr+':'+min+':'+ms
047: def change_time(time,time_shift):#'00:00:05,000 --> 00:00:08,000\n' --> '00:00:00,000 --> 00:00:02,500\n'
048: z = time.strip().split(' --> ')
049: start = change_1(z[0]) + time_shift
050: stop = change_1(z[1]) + time_shift
051: if start < 0:
052: start = 0
053: if stop < 0:
054: stop = 0
055: if start == 0 and stop == 0:
056: return 'delete'
057: else:
058: start = change_2(str(start))
059: stop = change_2(str(stop))
060: return start + ' --> ' + stop
061: def packing_shift(list_in,time_shift):
062: list_out = []
063: alunum = []
064: for i in list_in:
065: i[1] = change_time(i[1],time_shift)
066: if i[1] != 'delete':
067: list_out.append(i[1:])
068: alunum.append(i[0])
069: alunum = alunum[0:len(list_out)]
070: for i in range(len(alunum)):
071: list_out[i] = [alunum[i]] + list_out[i]
072: return list_out
073: def shift(file_in, time_shift, file_out):
074: fout = open(file_out, 'w', encoding='utf-8')
075: fin_list = fin_list_manage(file_in)
076: packing_fin_list = packing_shift(fin_list,time_shift)
077: for i in packing_fin_list:
078: for j in i:
079: fout.write(j + '\n')
080: fout.close()
081: # ------------------------------------------
082: def b_merge(t1,t2,threshold):
083: if abs(t1-t2) <= threshold:
084: return True
085: else:
086: return False
087: def merge(base_file, merge_file, threshold, file_out):
088: fot = open(file_out,'w', encoding='utf-8')
089: x = fin_list_manage(base_file)
090: y = fin_list_manage(merge_file)
091: ans = []
092: mirr = []
093: for i in x:
094: t = 0
095: for j in y:
096: t_b = i[1].split(' --> ')
097: t_m = j[1].split(' --> ')
098: t1 = change_1(t_b[0])
099: t2 = change_1(t_m[0])
100: if b_merge(t1,t2,threshold):
101: mystr = i[2] + '\n' + j[2]
102: i_s = [i[1]] + [mystr] + [i[-1]]
103: ans.append(i_s)
104: mirr.append(j)
105: t = 1
106: if t == 0:
107: ans.append(i[1:])
108: for k in range(len(ans)):
109: ans[k] = [str(k+1)] +ans[k]
110: for z in ans:
111: for i in z:
112: fot.write(i + '\n')
113: fot.close()
114: #print(mirr_2)
115: # ------------------------------------------
116: def determind_str_from_inside_list(list_in):
117: str_gather_list = list_in[2:-1]
118: str_gather = ''
119: for i in range(len(str_gather_list)):
120: if i != len(str_gather_list)-1:
121: str_gather += str_gather_list[i] + '\n'
122: else:
123: str_gather += str_gather_list[i] #edit
124: return str_gather #
125: def delete_str(x,d):#d = (),[]...
126: s = ''
127: check = 0
128: for i in x:
129: if i == d[0]:
130: check =1
131: elif i == d[1]:
132: check = 0
133: elif check == 0:
134: s += i
135: return s
136: def super_delete_str(x):
137: x = delete_str(x,'[]')
138: x = delete_str(x, '()')
139: x = delete_str(x, '{}')
140: x = delete_str(x, '<>')
141: return x
142: def have_num_alphabet(x):
143: s = ''
144: for i in x:
145: if 'a' <= i <= 'z' or '0' <= i <= '9' or 'ก' <= i <= 'ฮ' or 'A' <= i <= 'Z':
146: s += i
147: return s
148: def clean(file_in, file_out):
149: fin_list = fin_list_manage(file_in)
150: fot = open(file_out, 'w', encoding='utf-8')
151: alunum = []
152: list_out = []
153: for i in range(len(fin_list)):
154: str_together = determind_str_from_inside_list(fin_list[i])
155: str_together = super_delete_str(str_together)
156: alunum.append(fin_list[i][0])
157: if have_num_alphabet(str_together).isalnum():
158: my_list = [fin_list[i][1]] + [str_together] + [fin_list[i][-1]]
159: list_out.append(my_list)
160: alunum = alunum[0:len(list_out)]
161: for i in range(len(alunum)):
162: list_out[i] = [alunum[i]] + list_out[i]
163: for i in list_out:
164: for j in i:
165: fot.write(j + '\n')
166: fot.close()
167:
168:
169: # ------------------------------------------
6430012021 (90.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = ['00:03:20,600 --> 00:03:23,600', 'AAAA AAAA AAA.', '']
['00:03:31,000 --> 00:03:46,100', 'AAAA AAAA AAA.', '']
['01:01:25,600 --> 01:01:43,600', 'AAAA AAAA AAA.', '']
['01:02:40,600 --> 01:02:53,600', 'AAAA AAAA AAA.', '']
, close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = ['00:00:09,600 --> 00:00:12,600', 'AAAA AAAA AAA.', '']
['00:00:20,000 --> 00:00:35,100', 'AAAA AAAA AAA.', '']
['00:58:14,600 --> 00:58:32,600', 'AAAA AAAA AAA.', '']
['00:59:29,600 --> 00:59:42,600', 'AAAA AAAA AAA.', '']
, close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = ['00:00:00,000 --> 00:00:00,100', 'AAAA AAAA AAA.', '']
['00:57:39,600 --> 00:57:57,600', 'AAAA AAAA AAA.', '']
['00:58:54,600 --> 00:59:07,600', 'AAAA AAAA AAA.', '']
, close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 70.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = ['00:01:35,100 --> 00:01:48,100\n', 'AAA\n']
['00:01:45,100 --> 00:01:48,100\n', '111\n']
['00:01:55,500 --> 00:02:10,600\n', '222\n', 'BBB\n']
['00:59:40,100 --> 01:00:08,100\n', '333\n', '33\n']
['00:59:50,100 --> 01:00:08,100\n', 'CCC\n', 'CC\n']
['01:01:05,100 --> 01:01:18,100\n', '444\n44\n', 'DDD\nDD\n']
, close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = [115001, 116000, '222\n', 'BBB\n', 498]
['00:01:20,499 --> 00:01:22,000\n', 'XXX\n']
['00:01:49,499 --> 00:01:49,500\n', 'YYY\n']
['00:01:50,000 --> 00:01:56,000\n', '111\n', 'AAA\n']
['00:01:55,001 --> 00:01:56,000\n', '222\n', 'BBB\n']
['00:01:56,997 --> 00:01:56,998\n', '333\n', 'DDD\n']
['00:01:56,998 --> 00:01:56,999\n', '444\n', 'DDD\n']
['00:01:56,999 --> 00:04:22,100\n', '555\n', 'DDD\n']
, close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
DDD
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID 6430012021
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: fin = open(file_in, encoding='utf-8')
007: fout = open(file_out, "w" , encoding='utf-8')
008: list1 = []
009: k=[]
010: for x in fin:
011: if(x=="\n"):
012: k.append(x)
013: k.pop(0)
014: list1.append(k)
015: k = []
016: else:
017: k.append(x)
018: if(len(k)>0):
019: k.pop(0)
020: list1.append(k)
021:
022: result = []
023: for i in list1:
024: k = []
025: for j in i:
026: m = j.replace("\n",'')
027: k.append(m)
028: result.append(k)
029:
030: for x in result:
031: f = x[0].split("-->")
032: result[result.index(x)][0] = f[0]
033: result[result.index(x)].insert(1,f[1])
034:
035: resultwithtime = []
036: for x in result:
037: time1 = x[0].split(":")
038: time2 = x[1].split(":")
039: time1_all = int(time1[0])*3600000 + int(time1[1])*60000 + int(time1[2].replace(",",''))
040: time2_all = int(time2[0])*3600000 + int(time2[1])*60000 + int(time2[2].replace(",",''))
041: y = time1_all + time_shift
042: z = time2_all + time_shift
043: if(y<0 and z <0):
044: continue
045: else:
046: if(y<0):
047: y = 0
048: resultwithtime.append(x)
049: resultwithtime[resultwithtime.index(x)][0] = y
050: resultwithtime[resultwithtime.index(x)][1] = z
051:
052: resultwithtime.sort()
053: for x in resultwithtime:
054: x[0] = timechange(x[0],x[1])
055: x.pop(1)
056: print(x)
057: for i in resultwithtime:
058: fout.write(str(resultwithtime.index(i)+1)+"\n")
059: for j in i:
060: fout.write(j +"\n")
061: fin.close()
062: fout.close()
063: return
064:
065: def timechange(y,z):
066: time1f = []
067: time2f = []
068: k=[]
069: time1f.append(y//3600000)
070: y = y%3600000
071: time1f.append(y//60000)
072: y = y%60000
073: time1f.append(y)
074: time2f.append(z//3600000)
075: z = z%3600000
076: time2f.append(z//60000)
077: z = z%60000
078: time2f.append(z)
079: ystr = ""
080: zstr = ""
081: ystr = str(time1f[0]).zfill(2) + ":" + str(time1f[1]).zfill(2) + ":"
082: zstr = str(time2f[0]).zfill(2) + ":" + str(time2f[1]).zfill(2) + ":"
083: ystr += str(y//1000).zfill(2) + "," + str(y%1000).zfill(3)
084: zstr += str(z//1000).zfill(2) + "," + str(z%1000).zfill(3)
085: omgstr = ystr + " --> " + zstr
086: return omgstr
087:
088:
089: # ------------------------------------------
090: def timetotal(entirelist):
091: for x in entirelist:
092: x[0].replace("\n",'')
093: y = x[0].split("-->")
094: time1 = y[0].split(":")
095: time2 = y[1].split(":")
096: time1_all = int(time1[0])*3600000 + int(time1[1])*60000 + int(time1[2].replace(",",''))
097: time2_all = int(time2[0])*3600000 + int(time2[1])*60000 + int(time2[2].replace(",",''))
098: x[0] = time1_all
099: x.insert(1,time2_all)
100: return entirelist
101:
102: def merge(base_file, merge_file, threshold, file_out):
103: base = open(base_file, encoding = 'utf-8')
104: merge = open(merge_file, encoding = 'utf-8')
105: fout = open(file_out,"w", encoding = 'utf-8')
106: base_lis = []
107: merge_lis = []
108: temp = []
109: for x in base:
110: if(x=="\n"):
111: temp.append(x)
112: temp.pop(0)
113: base_lis.append(temp)
114: temp=[]
115: else:
116: temp.append(x)
117: if(len(temp)>0):
118: temp.pop(0)
119: base_lis.append(temp)
120: temp=[]
121: for y in merge:
122: if(y=="\n"):
123: temp.append(y)
124: temp.pop(0)
125: merge_lis.append(temp)
126: temp=[]
127: else:
128: temp.append(y)
129: if(len(temp)>0):
130: temp.pop(0)
131: merge_lis.append(temp)
132: temp=[]
133: base_lis = timetotal(base_lis)
134: merge_lis = timetotal(merge_lis)
135: for i in base_lis:
136: i.pop(len(i)-1)
137: for i in merge_lis:
138: i.pop(len(i)-1)
139: sulatan = []
140: merge_fuu = []
141: opp = []
142: oss = []
143: for i in merge_lis:
144: for j in base_lis:
145: k = []
146: s = ""
147: y = ""
148: if(abs(i[0]-j[0])<= threshold):
149: k.append(j[0])
150: k.append(j[1])
151: for x in range(2,len(j)):
152: y += j[x]
153: for x in range(2,len(i)):
154: s += i[x]
155: k.append(y)
156: k.append(s)
157: k.append(abs(i[0]-j[0]))
158: merge_fuu.append(k)
159: opp.append(i)
160: oss.append(j)
161:
162: for i in oss:
163: if i in base_lis:
164: base_lis.remove(i)
165: for i in opp:
166: if i in merge_lis:
167: merge_lis.remove(i)
168: merge_fuu.sort()
169: inde = []
170: for i in range(len(merge_fuu)-1):
171: if(merge_fuu[i+1][0]==merge_fuu[i][0]):
172: if(merge_fuu[i][4]<merge_fuu[i+1][4]):
173: print(merge_fuu[i])
174: inde.append(i+1)
175: else:
176: print(merge_fuu[i])
177: inde.append(i)
178: zad = []
179: for i in merge_fuu:
180: if merge_fuu.index(i) in inde:
181: pass
182: else:
183: zad.append(i)
184:
185:
186: tum = []
187: for i in zad:
188: if(len(i)==0):
189: continue
190: i = i[:-1]
191: tum.append(i)
192: for i in base_lis:
193: tum.append(i)
194: for i in merge_lis:
195: tum.append(i)
196: tum.sort()
197: for i in tum:
198: i[0] = timechange(i[0],i[1])+"\n"
199: i.pop(1)
200: print(i)
201: for i in tum:
202: fout.write(str(tum.index(i)+1)+"\n")
203: for j in i:
204: fout.write(j)
205: fout.write("\n")
206: base.close()
207: merge.close()
208: fout.close()
209: return
210:
211:
212: # ------------------------------------------
213: def clean(file_in, file_out):
214: xmark = ["(","<","[","{"]
215: ymark = ["}","]",">",")"]
216: bub = ["@"]
217: store = []
218: temp = []
219: fin = open(file_in, encoding='utf-8')
220: fout = open(file_out,'w', encoding='utf-8')
221: for x in fin:
222: if(x=="\n"):
223: temp.append(x)
224: temp.pop(0)
225: temp = temp[:-1]
226: store.append(temp)
227: temp=[]
228: else:
229: temp.append(x)
230: # เก็บแยกช่อง
231: if(len(temp)>0):
232: temp.pop(0)
233: store.append(temp)
234: result = []
235: for i in store:
236: k = []
237: for j in i:
238: m = j.replace("\n",'')
239: k.append(m)
240: result.append(k)
241: # เอา เอ็นเตอร์ออก
242: result_str=[]
243: for i in result:
244: sup = []
245: sup.append(i[0])
246: temp_str = ""
247: for j in range(1,len(i)):
248: temp_str += i[j]
249: temp_str += "@"
250: sup.append(temp_str)
251: result_str.append(sup)
252: # ใส่ @ เพื่อแยก string
253: clean_re = []
254: for x in result_str:
255: temp_x = []
256: temp_list = []
257: temp_str = ""
258: bypass = False
259: cant = False
260: for i in range(len(x[1])):
261: if x[1][i] in bub:
262: bypass = True
263: if(bypass and cant==False):
264: temp_str = temp_str
265: if(len(temp_str)>=1):
266: if(temp_str[len(temp_str)-1] ==" "):
267: temp_str = temp_str[:-1]
268: temp_list.append(temp_str)
269: temp_str = ""
270: bypass = False
271: cant = False
272: continue
273: if x[1][i] in xmark:
274: cant = True
275: continue
276: if x[1][i] in ymark:
277: cant = False
278: continue
279: if(cant):
280: continue
281: elif(len(temp_str)==0 and x[1][i]==" "):
282: continue
283: else:
284: temp_str += x[1][i]
285:
286: flag = []
287:
288: for i in range(len(temp_list)):
289: for j in range(len(temp_list[i])):
290: if(temp_list[i][j].isalnum()):
291: if i not in flag:
292: flag.append(i)
293: if(len(flag)==0):
294: continue
295: else:
296: temp_x.append(x[0])
297: for i in range(len(flag)):
298: temp_x.append(temp_list[flag[i]])
299: temp_x.append('')
300: clean_re.append(temp_x)
301: for i in clean_re:
302: fout.write(str(clean_re.index(i)+1)+"\n")
303: for j in i:
304: fout.write(j+"\n")
305: fin.close()
306: fout.close()
307: return
6430014321 (22.67%)
shift -> 8.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:02:80,600 --> 00:02:83,600
AAAA AAAA AAA.
2
00:02:90,1000 --> 00:03:45,1100
AAAA AAAA AAA.
3
00:60:85,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
1
59:59:69,600 --> 59:59:72,600
AAAA AAAA AAA.
2
59:59:79,1000 --> 59:60:34,1100
AAAA AAAA AAA.
3
59:117:74,600 --> 60:58:32,600
AAAA AAAA AAA.
4
60:59:29,600 --> 60:59:42,600
AAAA AAAA AAA. |
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
1
59:58:94,600 --> 59:58:97,600
AAAA AAAA AAA.
2
59:58:104,1000 --> 59:59:59,1100
AAAA AAAA AAA.
3
59:116:99,600 --> 60:57:57,600
AAAA AAAA AAA.
4
60:58:54,600 --> 60:58:67,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 0.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
2
00:01:55,500 --> 00:02:10,600
3
00:59:50,100 --> 01:00:08,100
4
01:01:05,100 --> 01:01:18,100 |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
2
00:01:55,500 --> 00:02:10,600
BBB
3
00:59:50,100 --> 01:00:08,100
4
01:01:05,100 --> 01:01:18,100 |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
BBB
2
00:01:55,500 --> 00:02:10,600
BBB
3
00:59:50,100 --> 01:00:08,100
ZZZZ
4
01:01:05,100 --> 01:01:18,100
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
2
00:01:55,500 --> 00:02:10,600
3
00:59:50,100 --> 01:00:08,100
4
01:01:05,100 --> 01:01:18,100
5
01:03:45,100 --> 01:03:48,100 |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 60.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
5
00:01:55,500 --> 00:01:55,502
CCC
6
00:01:56,997 --> 00:01:56,998
333
DDD
7
00:01:56,998 --> 00:01:56,999
444
8
00:01:56,999 --> 00:04:22,100
555 |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # import os
002: # Subtitle Tools
003: # 6430014321
004:
005: class subtitle:
006: def __init__(self, data):
007: self.id = data[0]
008: start, stop = [i.strip() for i in data[1].split("-->")]
009: start, start_ms = start.split(",")
010: stop, stop_ms = stop.split(",")
011: self.start_ms = int(start_ms)
012: self.stop_ms = int(stop_ms)
013: self.start_hr, self.start_min, self.start_sec = [int(i.strip()) for i in start.split(":")]
014: self.stop_hr, self.stop_min, self.stop_sec = [int(i.strip()) for i in stop.split(":")]
015: self.texts = data[2:]
016:
017: def __IsoToUnix(self, timeList):
018: time = timeList[3]
019: time += timeList[2] * 1000
020: time += timeList[1] * 1000 * 60
021: time += timeList[0] * 1000 * 60 *60
022: return time
023:
024: def __UnixToIso(self, offset):
025: ms = offset % 1000
026: secs = int((offset / 1000) % 60)
027: mins = int((offset / ( 1000*60 ) ) % 60)
028: hours = int((offset / ( 1000*60*60 ) ) % 60)
029: return hours, mins, secs, ms
030:
031: def getStartTime(self):
032: return self.__IsoToUnix([self.start_hr, self.start_min, self.start_sec, self.start_ms])
033:
034: def getEndTime(self):
035: return self.__IsoToUnix([self.stop_hr, self.stop_min, self.stop_sec, self.stop_ms])
036:
037:
038: def addStartTime(self, offset):
039: hours, mins, secs, ms = self.__UnixToIso(offset)
040: self.start_hr += hours
041: self.start_min += mins
042: self.start_sec += secs
043: self.start_ms += ms
044:
045: def addEndTime(self, offset):
046: hours, mins, secs, ms = self.__UnixToIso(offset)
047: self.stop_hr += hours
048: self.stop_min += mins
049: self.stop_sec += secs
050: self.stop_ms += ms
051:
052: def getLines(self):
053: text = self.id + '\n'
054: text += "{}:{}:{},{} --> {}:{}:{},{}\n".format(str(self.start_hr).zfill(2), str(self.start_min).zfill(2), str(self.start_sec).zfill(2), str(self.start_ms).zfill(3), str(self.stop_hr).zfill(2), str(self.stop_min).zfill(2), str(self.stop_sec).zfill(2), str(self.stop_ms).zfill(3))
055: text += "\n".join(self.texts) + "\n\n"
056: return text
057:
058: def mergeData(self, sub):
059: self.texts += sub.texts
060:
061: # def cleanData(self,start_bracket, stop_bracket):
062: # alltexts = "\n".join(self.texts).split(stop_bracket)
063: # cleanedText = ""
064: # for text in alltexts:
065: # cleanedText += text[:text.find(start_bracket)]
066: # if text.find(start_bracket): cleanedText+=stop_bracket
067: # self.texts = cleanedText.split("\n")
068:
069: def cleanData(self, start_bracket, stop_bracket):
070: alltexts = "\n".join(self.texts)
071: cleanedText = alltexts
072: for i in range(len(alltexts)):
073: if alltexts[i] == start_bracket:
074: clindex = alltexts.find(stop_bracket,i)
075: cleanedText = cleanedText.replace(alltexts[i:clindex+1],"")
076: cleanedText.replace("\n\n", "\n")
077: self.texts = [i.strip() for i in cleanedText.split("\n") if i.isalnum()]
078:
079:
080: def formatData(rawData):
081: subs = [sub.split('\n') for sub in rawData.read().split("\n\n") if len(sub.split("\n"))>= 3]
082: data = []
083: for sub in subs:
084: data+=[subtitle(sub)]
085: return data
086:
087:
088:
089:
090:
091: # ------------------------------------------
092: def shift(file_in, time_shift, file_out):
093: # fin_path = os.path.abspath("./SRT_Files/{}".format(file_in))
094: # fout_path = os.path.abspath("./SRT_Files/{}".format(file_out))
095: fin = open(file_in, "r", encoding='utf-8')
096: fout = open(file_out, "w+", encoding='utf-8')
097: time_shift = int(time_shift)
098: data = formatData(fin)
099: for sub in data:
100: sub.addStartTime(time_shift)
101: sub.addEndTime(time_shift)
102: fout.write(sub.getLines())
103: fin.close()
104: fout.close()
105: return
106:
107: # ------------------------------------------
108: def merge(base_file, merge_file, threshold, file_out):
109: # bf_path = os.path.abspath("./SRT_Files/{}".format(base_file))
110: # mf_path = os.path.abspath("./SRT_Files/{}".format(merge_file))
111: # fout_path = os.path.abspath("./SRT_Files/{}".format(file_out))
112: bfin = open(base_file, "r", encoding='utf-8')
113: mfin = open(merge_file, "r", encoding='utf-8')
114: fout = open(file_out, "w+", encoding='utf-8')
115: threshold = int(threshold)
116: bfData = formatData(bfin)
117: mfData = formatData(mfin)
118: ofData = []
119: for base_sub in bfData:
120: for merge_sub in mfData:
121: if abs(base_sub.getStartTime() - merge_sub.getStartTime()) <= threshold:
122: base_sub.mergeData(merge_sub)
123: mfData.remove(merge_sub)
124: ofData += [base_sub]
125: ofData += mfData
126: ofData.sort(key=lambda x: x.getStartTime())
127: for subID in range(len(ofData)):
128: ofData[subID].id = str(subID + 1)
129: fout.write(ofData[subID].getLines())
130: bfin.close()
131: mfin.close()
132: fout.close()
133: return
134:
135: # ------------------------------------------
136: def clean(file_in, file_out):
137: # fin_path = os.path.abspath("./SRT_Files/{}".format(file_in))
138: # fout_path = os.path.abspath("./SRT_Files/{}".format(file_out))
139: fin = open(file_in, "r", encoding='utf-8')
140: fout = open(file_out, "w+", encoding='utf-8')
141: data = formatData(fin)
142: for sub in data:
143: sub.cleanData("(",")")
144: sub.cleanData("[","]")
145: sub.cleanData("<",">")
146: sub.cleanData("{","}")
147: fout.write(sub.getLines())
148: return
149:
150: # ------------------------------------------
151: # shift('test1_en.srt', 4500, 'test1_shifted.srt')
152: # merge('test_en.srt','test_th.srt', 1000, "test_merged.srt")
153: # clean('./SRT_Files/test1_en.srt', './SRT_Files/test_cleaned.srt')
6430015021 (53.33%)
shift -> 90.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
1
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
2
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = UnboundLocalError("local variable 'timebase' referenced before assignment")
File "z:\HW08\_student.py", line 192, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 88, in merge
out.write(str(count) + "\n" + timebase + base.readline() + mer.readline())
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = UnboundLocalError("local variable 'timebase' referenced before assignment")
File "z:\HW08\_student.py", line 192, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 88, in merge
out.write(str(count) + "\n" + timebase + base.readline() + mer.readline())
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430015021
003:
004: def timel2s(time) :
005: time = time.strip()
006: h,m,s = [e for e in time.split(":")]
007: s = s.split(",")
008: mstime = int(h)*60*60*1000 + int(m)*60*1000 + int(s[0])*1000 + int(s[1])
009: return mstime
010: def times2l(time) :
011: h = time//(60*60*1000)
012: time = time - h*60*60*1000
013: m = time//(60*1000)
014: time = time - m*60*1000
015: s = time//1000
016: time = time - s*1000
017: ms = time
018: h = "0"*(2-len(str(h))) + str(h)
019: m = "0"*(2-len(str(m))) + str(m)
020: s = "0"*(2-len(str(s))) + str(s)
021: ms = "0"*(3-len(str(ms))) + str(ms)
022: timeo = h+":"+m+":"+s+","+ms
023: return timeo
024: # ------------------------------------------
025: def shift(file_in, time_shift, file_out):
026: i = 0
027: time_start = []
028: time_stop = []
029: dialog = []
030: tnl = []
031: file = open(file_in,"r",encoding='utf-8')
032: while True:
033: line = file.readline()
034: if line.find("-->") != -1 :
035: time = line.split(" --> ")
036: time_start.append( timel2s(time[0]) )
037: time_stop.append( timel2s(time[1]) )
038:
039: elif line.strip() != "" and line.strip().isdigit() == False:
040: te = ""
041: te += line.strip() + "\n"
042: a = file.readline()
043: if a.strip() != "" :
044: te += a
045: dialog.append(te)
046: if not line:
047: break
048: file.close()
049: # time shifting
050: for e in range(len(time_start)) :
051: if time_start[e] + time_shift > 0 :
052: time_start[e] = time_start[e] + time_shift
053: else : time_start[e] = 0
054:
055: if time_stop[e] + time_shift > 0 :
056: time_stop[e] = time_stop[e] + time_shift
057: else : time_stop[e] = 0
058: # convert ms time to readable to time and remove dialog if it not in range
059: for e in range(len(time_start)) :
060: a = time_start[e]
061: b = time_stop[e]
062: if a != 0 and b != 0 :
063: tnl.append(times2l(a)+" --> "+times2l(b))
064: else:
065: dialog.pop(e)
066: with open(file_out,"w+",encoding='utf-8') as wfile:
067: for count in range(len(dialog)):
068: wfile.write(str(count+1)+"\n" + tnl[count] + "\n" + dialog[count]+"\n")
069: count += 1
070: wfile.close()
071: # ------------------------------------------
072: def merge(base_file, merge_file, threshold, file_out):
073: base = open(base_file,"r",encoding="utf-8")
074: mer = open(merge_file,"r",encoding="utf-8")
075: out = open(file_out,"w+",encoding="utf-8")
076: count = 1
077: while True :
078: bf = base.readline()
079: mf = mer.readline()
080: htm = 0 ; htb = 0
081: if bf.find("-->") != -1 :
082: timebase = bf
083: htb = timel2s(bf.split("-->")[0])
084: if mf.find("-->") != -1 :
085: timemer = mf
086: htm = timel2s(mf.split("-->")[0])
087: if abs(htb-htm) <= threshold :
088: out.write(str(count) + "\n" + timebase + base.readline() + mer.readline())
089: count += 1
090: else :
091: out.write(str(count) + "\n" + timebase + base.readline()) if htb < htm else out.write(str(count) + "\n" + timemer + mer.readline())
092: count += 1
093: if not bf and not mf :
094: break
095: # ------------------------------------------
096: def clean(file_in, file_out):
097: rfile = open(file_in , "r" , encoding='utf-8')
098: wfile = open(file_out , "w+" , encoding='utf-8')
099: set = ""
100: line = rfile.readline()
101: for line in rfile :
102: if line.strip().isdigit() == False :
103: set += line.strip() + "\n"
104: e = 0
105: while e < len(set) :
106: if e >= len(set) :
107: break
108: if set[e] in ["(",")","[","]","<","{","}"] :
109: a = 0 ; i = 1
110: while a == 0 :
111: if set[e+i] in ["(",")","[","]","<",">","{","}"] :
112: set = set[:e] + set[e+i+1:]
113: e = 0
114: break
115: else :
116: i += 1
117: continue
118: e += 1
119: wfile.write(set)
120: wfile.close()
121: ans = ""
122: wfile = open(file_out , "r" , encoding='utf-8')
123: count = 1
124: while True :
125: re = wfile.readline()
126: lock = 0
127: cor = 0
128: if re.find("-->") != -1 :
129: re2 = wfile.readline()
130: if re2.find("-->") == -1 and re2.strip() != "":
131: for e in re2 :
132: if e.isalnum() == True :
133: ans += str(count) + "\n" + re.strip() + "\n" + re2.strip() + "\n"
134: count += 1
135: lock = 1
136: cor = 1
137: break
138: re3 = wfile.readline()
139: if re3.strip().find("-->") == -1 and re3.strip() != "" :
140: for e in re3 :
141: if e.isalnum() == True :
142: if lock == 1 :
143: ans += re3.strip() + "\n"
144: else :
145: ans += str(count) + "\n" + re.strip() + "\n" + re3.strip() + "\n"
146: count += 1
147: cor = 1
148: break
149: if cor == 1 :
150: ans += "\n"
151:
152: if not re :
153: break
154: wfile.close()
155: wfile = open(file_out , "w+" , encoding='utf-8')
156: wfile.write(ans)
157: wfile.close()
158: rfile.close()
159: # ------------------------------------------
6430028121 (23.33%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:02:80,600 --> 00:02:83,600
AAAA AAAA AAA.
2
00:02:91,000 --> 00:03:46,099999999999994
AAAA AAAA AAA.
3
01:00:85,600 --> 01:01:43,599999999999994
AAAA AAAA AAA.
4
01:02:40,599999999999994 --> 01:02:53,599999999999994
AAAA AAAA AAA. |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 292, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 164, in merge
while abs(time_base_start[base] - time_merge_start[merge]) <= float(threshold)/1000 :
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 292, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 164, in merge
while abs(time_base_start[base] - time_merge_start[merge]) <= float(threshold)/1000 :
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID
003:
004: # ------------------------------------------
005: def time_change(time):
006: time1 = ''
007: for ch in time :
008: if ch == ',' :
009: time1 += '.'
010: else :
011: time1 += ch
012: a = time1.split(':')
013: return a
014:
015: def time_changeback(time):
016: for i in range(len(time)):
017: time1 = ''
018: for e in time[i] :
019: if e == ' ':
020: time1 += ''
021: else :
022: time1 += e
023: time[i] = time1
024: ans = ':'.join(time)
025: return ans
026:
027: def time_manage(t1):
028: if float(t1[2]) < 0 :
029: if float(t1[1]) >= 1 :
030: t1[1] = str(int(float(t1[1])) - 1)
031: t1[2] = str(float(t1[2]) + 60)
032: if float(t1[1]) < 1 :
033: if float(t1[0]) >= 1 :
034: t1[0] = str(int(float(t1[0])) - 1)
035: t1[1] = str(int(float(t1[1])) + 60)
036: t1[1] = str(int(float(t1[1])) - 1)
037: t1[2] = str(float(t1[2]) + 60)
038: else :
039: t1 = ['00', '00', '00.000']
040: if float(t1[2]) >= 60 :
041: t1[2] = str(float(t1[2]) - 60)
042: t1[1] = str(int(float(t1[1])) + 1)
043: if float(t1[1]) >= 60 :
044: t1[1] = str(int(float(t1[1])) - 60)
045: t1[0] = str(int(float(t1[0])) +1)
046: if len(t1[0]) == 1 :
047: t1[0] = '0' + t1[0]
048: if len(t1[1]) == 1 :
049: t1[1] = '0' + t1[1]
050: a, b = t1[2].split('.')
051: if len(a) == 1 :
052: a = '0' + a
053: if len(b) == 1 :
054: b = b + '00'
055: if len(b) == 2 :
056: b = b + '0'
057: t1[2] = a+ '.' + b
058: c = ''
059: for ch in t1[2] :
060: if ch == '.' :
061: c += ','
062: else :
063: c += ch
064: t1[2] = c
065: return t1
066:
067: def file_to_list(filename) :
068: fin = open(filename, encoding='utf-8')
069: list_fin = []
070: list_line = []
071: for line in fin:
072: list_line.append(line[:-1])
073: if line == '\n' :
074: list_fin.append(list_line)
075: list_line = []
076: fin.close()
077: return list_fin
078:
079:
080: def time_cal(l_time):
081: time = float(l_time[0])*3600 + float(l_time[1])*60 + float(l_time[2])
082: return time
083:
084: #------------------------------------------------------
085:
086: def shift(file_in, time_shift, file_out) :
087: fin = open(file_in, encoding='utf-8')
088: fout = open(file_out, 'w', encoding='utf-8')
089: list_fin = []
090: list_line = []
091: for line in fin:
092: list_line.append(line[:-1])
093: if line == '\n' :
094: list_fin.append(list_line)
095: list_line = []
096: for i in range(len(list_fin)) :
097: time1, time2 = list_fin[i][1].split('-->')
098: t1 = time_change(time1)
099: t2 = time_change(time2)
100: t1[2] = str(float(t1[2]) + float(time_shift)/1000)
101: t2[2] = str(float(t2[2]) + float(time_shift)/1000)
102: t1 = time_manage(t1)
103: t2 = time_manage(t2)
104: t1 = time_changeback(t1)
105: t2 = time_changeback(t2)
106: list_fin[i][1] = t1 + ' --> ' +t2
107: count = 1
108: for i in range(len(list_fin)) :
109: if list_fin[i][1] == '00:00:00,000 --> 00:00:00,000' :
110: pass
111: else :
112: x = [str(count), list_fin[i][1]] + list_fin[i][2:]
113: fout.write('\n'.join(x) + '\n')
114: count += 1
115: fin.close()
116: fout.close()
117: return
118:
119: # ------------------------------------------
120: def merge(base_file, merge_file, threshold, file_out) :
121: list_fbase = file_to_list(base_file) # อ่านไฟล์ base
122: list_fmerge = file_to_list(merge_file) # อ่านไฟล์ที่เอามารวม
123: fout = open(file_out, 'w', encoding='utf-8')
124:
125: list_base_tstart = []
126: list_base_tend = []
127:
128: for i in range(len(list_fbase)) :
129: tstart, tend = list_fbase[i][1].split('-->')
130: tstart = time_change(tstart)
131: tend = time_change(tend)
132: list_base_tstart.append(tstart)
133: list_base_tend.append(tend)
134:
135: list_merge_tstart = []
136: list_merge_tend = []
137:
138: for i in range(len(list_fmerge)) :
139: tstart, tend = list_fmerge[i][1].split('-->')
140: tstart = time_change(tstart)
141: tend = time_change(tend)
142: list_merge_tstart.append(tstart)
143: list_merge_tend.append(tend)
144:
145: base = 0
146: merge = 0
147: count = 1
148: merged_list = []
149:
150: time_base_start = []
151: for i in range(len(list_base_tstart)):
152: time_base_start.append(time_cal(list_base_tstart[i]))
153:
154: time_merge_start = []
155: for i in range(len(list_merge_tstart)):
156: time_merge_start.append(time_cal(list_merge_tstart[i]))
157:
158: #***** แก้ใหม่ ****** เพราะ ถ้ามีหลายอันเวลาใกล้กันมันจะเอาแค่คู่เดียว ไม่รวม
159: while base < len(time_base_start) and merge < len(time_merge_start) :
160:
161: #-------------------------------------------------------------------------------------------
162: if abs(time_base_start[base] - time_merge_start[merge]) <= float(threshold)/1000 :
163: y = []
164: while abs(time_base_start[base] - time_merge_start[merge]) <= float(threshold)/1000 :
165: y += list_fmerge[merge][2:-1]
166: merge += 1
167: x = [str(count), list_fbase[base][1]] + list_fbase[base][2:-1] + y + ['']
168: merged_list.append(x)
169: count += 1
170: base += 1
171: elif time_base_start[base] < time_merge_start[merge] :
172: x = [str(count), list_fbase[base][1]] + list_fbase[base][2:]
173: merged_list.append(x)
174: count += 1
175: base += 1
176: elif time_base_start[base] > time_merge_start[merge] :
177: x = [str(count), list_fmerge[merge][1]] + list_fmerge[merge][2:]
178: merged_list.append(x)
179: count += 1
180: merge += 1
181: #----------------------------------------------------------------------------------------------
182: while base == len(time_base_start) and merge < len(time_merge_start) :
183: x = [str(count), list_fmerge[merge][1]] + list_fmerge[merge][2:]
184: merged_list.append(x)
185: count += 1
186: merge += 1
187: while base < len(time_base_start) and merge == len(time_merge_start) :
188: x = [str(count), list_fbase[base][1]] + list_fbase[base][2:]
189: merged_list.append(x)
190: count += 1
191: base += 1
192:
193:
194: for e in merged_list:
195: fout.write('\n'.join(e) + '\n')
196: fout.close()
197: return
198:
199: # ------------------------------------------
200: def clean(file_in, file_out) :
201: fin = open(file_in, encoding='utf-8')
202: fout = open(file_out, 'w', encoding='utf-8')
203: list_fin = []
204: list_line = []
205: for line in fin:
206: list_line.append(line[:-1])
207: if line == '\n' :
208: list_fin.append(list_line)
209: list_line = []
210:
211: l_fin = []
212: for i in range(len(list_fin)):
213: l_fin.append(list_fin[i][:2] + ['\n'.join(list_fin[i][2:-1])])
214:
215: #------------------------------------------
216: for i in range(len(l_fin)) :
217: while '[' in l_fin[i][2] :
218: g1 = l_fin[i][2].find('[')
219: g2 = l_fin[i][2].find(']',g1)
220: l_fin[i][2] = l_fin[i][2][0:g1] + l_fin[i][2][g2+1:]
221:
222: while '(' in l_fin[i][2] :
223: g1 = l_fin[i][2].find('(')
224: g2 = l_fin[i][2].find(')',g1)
225: l_fin[i][2] = l_fin[i][2][0:g1] + l_fin[i][2][g2+1:]
226:
227: while '{' in l_fin[i][2] :
228: g1 = l_fin[i][2].find('{')
229: g2 = l_fin[i][2].find('}',g1)
230: l_fin[i][2] = l_fin[i][2][0:g1] + l_fin[i][2][g2+1:]
231:
232: while '<' in l_fin[i][2] :
233: g1 = l_fin[i][2].find('<')
234: g2 = l_fin[i][2].find('>',g1)
235: l_fin[i][2] = l_fin[i][2][0:g1] + l_fin[i][2][g2+1:]
236: #---------------------------------------------
237: list_clean = []
238: count = 1
239: for i in range(len(l_fin)) :
240: a = False
241: for k in range(len(l_fin[i][2])) :
242: a = l_fin[i][2][k].isalnum()
243: if a == True :
244: break
245: if a == True :
246: x = [str(count), l_fin[i][1], l_fin[i][2], '']
247: list_clean.append(x)
248: count += 1
249:
250: for i in range(len(list_clean)) :
251: x = list_clean[i]
252: fout.write('\n'.join(x) + '\n')
253:
254: fin.close()
255: fout.close()
256: return
257:
258: # ------------------------------------------
6430032621 (56.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 48.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = False |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = False |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
123 AA♪
A♪
2
00:01:55,500 --> 00:02:10,600
$$AA
3
01:01:05,100 --> 01:01:18,100
♪♪
4
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 20.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
111
2
00:01:55,500 --> 00:02:10,600
222
3
00:59:40,100 --> 01:00:08,100
333
4
5
01:01:05,100 --> 01:01:18,100
444
6 |
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = False |
1
00:01:50,000 --> 00:01:56,000
00:01:49,499 --> 00:01:49,500
00:01:49,500 --> 00:01:50,000
111
2
00:01:55,001 --> 00:01:56,000
222
3
00:01:56,997 --> 00:01:56,998
333
4
00:01:56,998 --> 00:01:56,999
444
5
00:01:56,999 --> 00:04:22,100
555 |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430032621
003:
004: # ------------------------------------------
005: def time(x):
006: #x='00:00:00,000'
007: x=x.split(':')
008: h=x[0]
009: m=x[1]
010: s=x[2].split(',')
011: t=(int(h)*3600+int(m)*60+int(s[0]))*1000+int(s[1])
012: return t
013:
014: def turn(x):
015: #x=เวลาในหน่วยms
016: h=x//3600//1000
017: m=(x%3600000)//60//1000
018: s0=(x%60000)//1000
019: s1=((x%60000)%1000)%1000
020: hour=str(h//10)+str(h%10)
021: minute=str(m//10)+str(m%10)
022: sec=str(s0//10)+str(s0%10)
023: millisec='0'*(3-len(str(s1)))+str(s1)
024: y=hour+':'+minute+':'+sec+','+millisec
025: return y
026:
027: def shift(file_in, time_shift, file_out):
028: fn=open(file_in,"r",encoding='utf-8')
029: fo=open(file_out,"w",encoding='utf-8')
030: state=0
031: take=1
032: for line in fn:
033: if state!='delete':
034: if (str(line))[0] not in '0123456789':
035: state=2
036: if state==0:
037: state=1
038: elif state==1:
039: l=line.split(' --> ')
040: start=time(l[0])
041: stop=time(l[1])
042: newstart=start+time_shift
043: newstop=stop+time_shift
044: if newstart>0 and newstop>0:
045: fo.write(str(take)+'\n')
046: fo.write(turn(newstart)+' --> '+turn(newstop)+'\n')
047: take+=1
048: state=2
049: elif newstart<=0 and newstop<=0:
050: state='delete'
051: elif newstart<=0 and newstop>0:
052: newstart=0
053: fo.write(str(take)+'\n')
054: fo.write(turn(newstart)+' --> '+turn(newstop)+'\n')
055: take+=1
056: state=2
057: elif state==2:
058: fo.write(line)
059: state=0
060: else:
061: if line.strip()=='':
062: state=0
063: fn.close()
064: fo.close()
065: return
066:
067: # ------------------------------------------
068: def merge(base_file, merge_file, threshold, file_out):
069: b=open(base_file,"r",encoding='utf-8')
070: m=open(merge_file,"r",encoding='utf-8')
071: fo=open(file_out,"w",encoding='utf-8')
072: limit=''
073: state=0
074: take=1
075: for line in b:
076: if (str(line))[0] not in '0123456789':
077: state=2
078: if state==0:
079: fo.write(str(take)+'\n')
080: take+=1
081: state=1
082: elif state==1:
083: startstop=str(line)
084: limit=startstop
085: fo.write(startstop)
086: for check in m:
087: if '-->' in str(check):
088: s=check.split(' --> ')
089: start=time(s[0])
090: stop=time(s[1])
091: l=limit.split(' --> ')
092: ls=time(l[0])
093: lp=time(l[1])
094: add=''
095: comment=''
096: if start<ls:
097: if state!='add':
098: if abs(ls-start)>threshold:
099: add=str(check)
100: state='add'
101: elif state=='add':
102: comment=str(check)
103: if add.strip()!='':
104: fo.write(take)
105: fo.write(add)
106: add=''
107: if comment.strip()=='':
108: fo.write('\n')
109: state=0
110: break
111: else:
112: fo.write(comment)
113: state=2
114: elif state==2:
115: fo.write(str(line))
116: for check in m:
117: merge=''
118: if '-->' in str(check):
119: s=check.split(' --> ')
120: start=time(s[0])
121: stop=time(s[1])
122: l=limit.split(' --> ')
123: ls=time(l[0])
124: lp=time(l[1])
125: if abs(ls-start)<=threshold:
126: state=4
127: elif state==4:
128: merge=str(check)
129: if merge.strip()!='':
130: fo.write(str(merge))
131: else:
132: state=0
133: state=0
134: return
135:
136: # ------------------------------------------
137: def cut(x):
138: d=''
139: state=''
140: for i in range(len(x)):
141: if state!='die':
142: if x[i] not in '()[]{}<>':
143: d+=x[i]
144: elif x[i] in '()[]{}<>':
145: state='die'
146: elif state=='die':
147: if x[i] not in '()[]{}<>':
148: d+=''
149: elif x[i] in '()[]{}<>':
150: state=''
151: return d
152: def clean(file_in, file_out):
153: fn=open(file_in,"r",encoding='utf-8')
154: fo=open(file_out,"w",encoding='utf-8')
155: state=0
156: take=1
157: startstop=''
158: limit='None'
159: for line in fn:
160: if state!='delete':
161: if (str(line))[0] not in '0123456789':
162: if startstop!=limit:
163: state=2
164: else:
165: state=3
166: if state==0:
167: state=1
168: elif state==1:
169: if startstop!=str(line):
170: startstop=str(line)
171: limit=''
172: state=2
173: else:
174: state=3
175: elif state==2:
176: comment=cut(line)
177: if comment.strip()!='':
178: fo.write(str(take)+'\n')
179: fo.write(str(startstop))
180: fo.write(str(comment))
181: take+=1
182: state=0
183: limit=startstop
184: elif comment.strip()=='':
185: state='delete'
186: elif state==3:
187: comment=cut(line)
188: fo.write(str(comment))
189: state=0
190: else:
191: if line.strip()=='':
192: state=0
193:
194: return
195:
196: # ------------------------------------------
6430033221 (69.33%)
shift -> 80.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = False |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = False |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = False |
clean -> 64.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = False |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = False |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = False |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
00:01:55,500 --> 00:02:10,600
3
00:59:50,100 --> 01:00:08,100
♫♫ ♫
4
01:01:05,100 --> 01:01:18,100
5
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 64.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = False |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = False |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
AAA
3
00:01:50,000 --> 00:01:56,000
111
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430033221
003: def timetoms(t) :
004: a = t[0:12].split(':')
005: b = t[17:29].split(':')
006: a[2:3] = a[2].split(',')
007: b[2:3] = b[2].split(',')
008: for i in range(4) :
009: a[i] = int(a[i])
010: b[i] = int(b[i])
011: msa = a[3]
012: msa += a[2]*1000
013: msa += a[1]*1000*60
014: msa += a[0]*1000*60*60
015: msb = b[3]
016: msb += b[2]*1000
017: msb += b[1]*1000*60
018: msb += b[0]*1000*60*60
019: return msa,msb
020:
021: def mstotime(a,b) :
022: msa = a%1000
023: ssa = a//1000%60
024: mma = a//60000%60
025: hha = a//3600000
026: msb = b%1000
027: ssb = b//1000%60
028: mmb = b//60000%60
029: hhb = b//3600000
030: ans = str(hha).zfill(2) + ':' + str(mma).zfill(2) + ':'
031: ans+= str(ssa).zfill(2) + ',' + str(msa).zfill(3) + ' --> '
032: ans+= str(hhb).zfill(2) + ':' + str(mmb).zfill(2) + ':'
033: ans+= str(ssb).zfill(2) + ',' + str(msb).zfill(3)
034: return ans
035:
036: # ------------------------------------------
037: def shift(file_in, time_shift, file_out):
038: fi = open(file_in,'r', encoding='utf-8')
039: fo = open(file_out,'w', encoding='utf-8')
040: ans = ""
041: s = fi.readlines()
042: cnt = 1
043: i = 0
044: while i < (len(s)) :
045: i+=1 #for the first line, number
046:
047: a,b = timetoms(s[i]) #seconds
048: a += time_shift
049: b += time_shift
050: write = 0
051: if a > 0 and b > 0 :
052: write = 1
053: elif a < 0 and b > 0 :
054: write = 1
055: a = 0
056: else :
057: write = 0
058:
059: if write == 1 :
060: fo.write(str(cnt) + '\n')
061: fo.write(mstotime(a,b) + '\n')
062: i+=1
063:
064: while s[i] != '\n' : #words
065: if write == 1 :
066: fo.write(s[i])
067: i+=1
068: if write == 1 :
069: fo.write('\n')
070: cnt += 1
071: i+=1
072: return
073:
074: # ------------------------------------------
075: def convert(file) :
076: i = 0
077: output = []
078: while i < (len(file)) :
079: i+=1 #for the first line, number
080:
081: t1,t2 = timetoms(file[i]) #milliseconds
082: i+=1
083: subs = []
084: while file[i] != '\n' : #words
085: subs.append(file[i])
086: i+=1
087: i+=1
088: output.append([[t1,t2],subs])
089: return output #returns [[t1,t2],[sub1,sub2,sub3]]
090:
091: def fuckgoback(output,file) :
092: cnt = 1
093: fo = open(file,'w', encoding='utf-8')
094: for i in output :
095: fo.write(str(cnt) + '\n') #writes number
096: cnt += 1
097:
098: fo.write(mstotime(i[0][0],i[0][1]) + '\n') #writes time
099:
100: for j in i[1] :
101: fo.write(j)
102: fo.write('\n')
103:
104: def merge(base_file, merge_file, threshold, file_out):
105: bf = open(base_file,'r', encoding='utf-8')
106: mf = open(merge_file,'r', encoding='utf-8')
107: fo = open(file_out,'w', encoding='utf-8')
108: basefile = convert(bf.readlines())
109: mergefile = convert(mf.readlines())
110: basefile.sort() #now in the converted form
111: mergefile.sort()
112: for i in range(len(mergefile)) :
113: min_dist = 2099999999 #yes this is mine
114: for j in range(len(basefile)) :
115: #grabs mergefile, then check with every base file
116: if abs(mergefile[i][0][0]-basefile[j][0][0]) < min_dist :
117: min_dist = abs(mergefile[i][0][0]-basefile[j][0][0])
118: j_index = j
119: if min_dist <= threshold : #if it's within the threshold
120: for k in range(len(mergefile[i][1])) : #goes through all of that line's
121: basefile[j_index][1].append(mergefile[i][1][k])
122: else : #if not, just add the entire thing to the end
123: basefile.append(mergefile[i])
124: basefile.sort()
125: fuckgoback(basefile,file_out)
126: return
127:
128: # ------------------------------------------
129: def clean(file_in, file_out):
130: fi = open(file_in,'r', encoding='utf-8')
131: filein = convert(fi.readlines()) #returns [[t1,t2],[sub1,sub2,sub3]]
132: for i in filein : #in all text
133: subs = ""
134: for sub1 in i[1] : #adds all subs of that line to 1 long string
135: subs += sub1
136:
137: removed = ""
138: s1,s2,s3,s4 = 0,0,0,0
139: for e in subs :
140: if e == '[' :
141: s1 += 1
142: elif e == '(' :
143: s2 += 1
144: elif e == '<' :
145: s3 += 1
146: elif e == '{' :
147: s4 += 1
148: elif e == ']' and s1 > 0 :
149: s1 -= 1
150: elif e == ')'and s2 > 0 :
151: s2 -= 1
152: elif e == '>'and s3 > 0 :
153: s3 -= 1
154: elif e == '}'and s4 > 0 :
155: s4 -= 1
156: elif s1 == 0 and s2 == 0 and s3 == 0 and s4 == 0 or e == "\n" :
157: removed += e #removes brackets
158:
159: seperated = []
160: line = ""
161: for character in removed :
162: if character != '\n' :
163: line += character
164: else :
165: line += '\n'
166: seperated.append(line)
167: line = "" #seperate them back in to lines
168:
169: for line in seperated :
170: deleteline = True
171: for character in line :
172: if character.isalnum() : #removes non character lines
173: deleteline = False
174: break
175: if deleteline :
176: seperated.remove(line)
177:
178: i[1] = seperated
179: fuckgoback(filein,file_out)
180: return
181:
182: # ------------------------------------------
6430037821 (93.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:49,500 --> 00:01:50,000
AAA
4
00:01:50,000 --> 00:01:56,000
111
5
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
6
00:01:56,997 --> 00:01:56,998
333
7
00:01:56,998 --> 00:01:56,999
444
8
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430037821
003: def to_ms(time) :
004: ms = 0
005: time = time.split(":")
006: ms += int(time[0])*60*60*1000
007: ms += int(time[1])*60*1000
008: sec = time[2].split(",")
009: ms += int(sec[0])*1000
010: ms += int(sec[1])
011: return ms
012:
013: def to_time(ms) :
014: H = ms // (60*60*1000)
015: min = ms % (60*60*1000)
016: Min = min // (60*1000)
017: sec = min % (60*1000)
018: Sec = sec // 1000
019: msec = sec % 1000
020: time = "0"*(2-len(str(H))) + str(H) + ":" + "0"*(2-len(str(Min))) + str(Min) + ":" + "0"*(2-len(str(Sec))) + str(Sec) + "," + "0"*(3-len(str(msec))) + str(msec)
021: return time
022:
023: def to_list(file) :
024: lis = [[]]
025: d = 0
026: n = 0
027: for line in file :
028: if d == 0 :
029: d += 1
030: elif d == 1 :
031: lis[n].append(to_ms(line[:12]))
032: lis[n].append(to_ms(line[18:30]))
033: d += 1
034: elif line.strip(" ") == "\n" :
035: n += 1
036: d = 0
037: lis.append([])
038: elif d != 0 and d != 1 :
039: lis[n].append(line)
040: d += 1
041: return lis
042:
043: # ------------------------------------------
044: def shift(file_in, time_shift, file_out):
045: fin = open(file_in,"r",encoding = "utf-8")
046: fout = open(file_out,"w",encoding = "utf-8")
047: lfin = to_list(fin)
048: w = []
049: for i in range(len(lfin)) :
050: if len(lfin[i]) > 1 :
051: lfin[i][0] += time_shift
052: lfin[i][1] += time_shift
053: if lfin[i][0] < 0 :
054: lfin[i][0] = 0
055: if lfin[i][1] < 0 :
056: lfin[i][1] = 0
057: if lfin[i][1] != 0 :
058: w.append(lfin[i])
059: n = 1
060: for j in w :
061: fout.write(str(n)+"\n")
062: for q in range(len(j)) :
063: if q == 0 :
064: pass
065: elif q == 1 :
066: fout.write(to_time(j[0]) + " --> " + to_time(j[1]) + "\n")
067: else :
068: fout.write(j[q])
069: fout.write("\n")
070: n += 1
071: fin.close()
072: fout.close()
073:
074: # ------------------------------------------
075: def merge(base_file, merge_file, threshold, file_out):
076: bf = open(base_file,"r",encoding = "utf-8")
077: mf = open(merge_file,"r",encoding = "utf-8")
078: fo = open(file_out,"w",encoding = "utf-8")
079: lbf = to_list(bf)
080: lmf = to_list(mf)
081: lbf2 = []
082: for l in lbf :
083: lbf2.append(l)
084: for i in lmf :
085: c = threshold
086: x = 0
087: for j in lbf :
088: if len(i) > 0 and len(j) > 0 :
089: n = abs(i[0]-j[0])
090: if n < c :
091: c = n
092: dex = lbf.index(j)
093: x = 1
094: if x == 1 :
095: for g in range(len(i)) :
096: if g > 1 :
097: lbf2[dex].append(i[g])
098: if x == 0 :
099: lbf2.append(i)
100: lbf2.sort()
101: n = 1
102: for i in lbf2 :
103: if len(i) > 0 :
104: fo.write(str(n) + "\n")
105: i[0] = to_time(i[0])
106: i[1] = to_time(i[1])
107: for j in range(len(i)) :
108: if j == 0 :
109: pass
110: elif j == 1 :
111: fo.write(i[0] + " --> " + i[1] + "\n")
112: else :
113: fo.write(i[j])
114: fo.write("\n")
115: n += 1
116: bf.close()
117: mf.close()
118: fo.close()
119:
120: # ------------------------------------------
121: def findcha(w) :
122: if "(" in w :
123: a = w.find("(")
124: b = w.find(")")
125: return a,b
126: if "[" in w :
127: a = w.find("[")
128: b = w.find("]")
129: return a,b
130: if "{" in w :
131: a = w.find("{")
132: b = w.find("}")
133: return a,b
134: if "<" in w :
135: a = w.find("<")
136: b = w.find(">")
137: return a,b
138: else :
139: return -1,-1
140:
141:
142:
143: def clean(file_in, file_out):
144: fin = open(file_in,"r",encoding = "utf-8")
145: fout = open(file_out,"w",encoding = "utf-8")
146: lfin = to_list(fin)
147: n = 1
148: for i in lfin :
149: sub = ""
150: for j in range(len(i)) :
151: if j > 1 :
152: sub += i[j]
153: x,y = findcha(sub)
154: while x != -1 :
155: m = sub[x:y+1].find("\n")
156: if m != -1 :
157: sub = sub[:x]+"\n"+ sub[y+1:]
158: else :
159: sub = sub[:x] + sub[y+1:]
160: x,y = findcha(sub)
161: d = []
162: s = sub.split("\n")
163: for p in s :
164: c = False
165: for z in p :
166: if z.isalnum() == True :
167: c = True
168: break
169: if c == True :
170: d.append(p)
171: if d != [] :
172: fout.write(str(n)+"\n")
173: i[0] = to_time(i[0])
174: i[1] = to_time(i[1])
175: fout.write(i[0] + " --> " + i[1] + "\n")
176: for r in d :
177: fout.write(r.strip() + "\n")
178: fout.write("\n")
179: n += 1
180: fin.close()
181: fout.close()
6430043521 (66.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 0%
001: # Subtitle Tools
002: # 6430043521
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: fin = read_file(file_in)
007: lst_of_time = [] ; pair_time_start = [] ; ind_for_pop = [] ; final_time = []
008: #Creat data
009: for e in fin:
010: lst_of_time.append(e[0][0:len(e[0])])
011: for f in lst_of_time:
012: f = f.split()
013: pair_time_start.append([f[0],f[2]])
014: #Begin
015: for i in range(len(pair_time_start)):
016: millisec_ts = time_to_millisec(pair_time_start[i][0]) + time_shift
017: millisec_tf = time_to_millisec(pair_time_start[i][1]) + time_shift
018: hhmmss1 = milli_to_time(millisec_ts)
019: hhmmss2 = milli_to_time(millisec_tf)
020: if hhmmss1 == '00:00:00,000' and hhmmss2 == '00:00:00,000':
021: ind_for_pop.append(i)
022: else:
023: final_time.append(hhmmss1+' --> '+hhmmss2+'\n')
024: ind_for_pop = ind_for_pop[::-1]
025: write_time_file(file_in, final_time, file_out, ind_for_pop)
026: return
027: # ------------------------------------------
028: def merge(base_file, merge_file, threshold, file_out):
029: lst_base = read_file(base_file) ; lst_merge = read_file(merge_file)
030: i = 0 ; j = 0
031: time1, scr1 = read_list(lst_base[i], lst_base) ; time1_f, scr1_f = read_list(lst_base[i+1], lst_base) ; time2, scr2 = read_list(lst_merge[j], lst_merge)
032: while time1 != '' and time2 != '':
033: bgt_1 = time_to_millisec(time1[0:12]) ; bgt_2 = time_to_millisec(time2[0:12])
034: if time1_f != '':
035: bgt_1f = time_to_millisec(time1_f[0:12])
036: #jebpuad
037: if bgt_2 > bgt_1 and bgt_2 < bgt_1f:
038: if bgt_2 - bgt_1 < threshold:
039: lst_base[i].append(scr2) ; j += 1
040: time2, scr2 = read_list(lst_merge[j], lst_merge)
041: elif abs(bgt_1f - bgt_2) < threshold:
042: lst_base[i+1].append(scr2) ; j += 1
043: time2, scr2 = read_list(lst_merge[j], lst_merge)
044: i = lst_base.index(lst_base[i+1])
045: time1, scr1 = read_list(lst_base[i], lst_base)
046: else:
047: lst_base.insert(i+1,lst_merge[j]); j += 1
048: time2, scr2 = read_list(lst_merge[j], lst_merge)
049: i = lst_base.index(lst_base[i])
050: time1_f, scr1_f = read_list(lst_base[i+1], lst_base)
051: elif bgt_2 > bgt_1 and bgt_2 > bgt_1f:
052: i = lst_base.index(lst_base[i+1])
053: time1, scr1 = read_list(lst_base[i], lst_base)
054: if time1 != '':
055: time1_f, scr1_f = read_list(lst_base[i+1], lst_base)
056: elif bgt_2 < bgt_1:
057: if abs(bgt_2 - bgt_1) < threshold:
058: lst_base[i].append(scr2) ; j += 1
059: time2, scr2 = read_list(lst_merge[j], lst_merge)
060: else:
061: lst_base.insert(i,lst_merge[j]) ; j += 1
062: time2, scr2 = read_list(lst_merge[j], lst_merge)
063: i = lst_base.index(lst_base[i])
064: time1, scr1 = read_list(lst_base[i], lst_base)
065: time1_f, scr1_f = read_list(lst_base[i+1], lst_base)
066: while time1 == '' and time2 != '':
067: lst_base.append(lst_merge[j]) ; j += 1
068: time2, scr2 = read_list(lst_merge[j], lst_merge)
069: write_merge_file(lst_base, file_out)
070: return
071: # ------------------------------------------
072: def clean(file_in, file_out):
073: lst_fin = read_file(file_in)
074: lst_script = [] ; scrp_ath = ''
075: #Create script
076: for e in lst_fin:
077: e.pop(0)
078: for a in lst_fin:
079: for b in a:
080: scrp_ath += b
081: lst_script.append([scrp_ath])
082: scrp_ath = ''
083: #Begin อยากได้ลิสต์ ที่cleanเสร็จ แล้ว ind ที่ต้อง pop ออก
084: lst_clean_scrp, ind_for_pop2 = filter_punc_sym(lst_script)
085: write_clean_file(file_in, lst_clean_scrp, file_out, ind_for_pop2)
086: return
087: # ------------------------------------------
088: def read_file(file_in):
089: lst = [] ; file_list = []
090: fin = open(file_in, 'r', encoding='utf-8')
091: for line in fin:
092: if line != '\n':
093: lst.append(line)
094: else:
095: lst.pop(0)
096: file_list.append(lst)
097: lst = []
098: file_list.append(lst)
099: file_list.pop(len(file_list)-1)
100: fin.close()
101: return file_list
102: # ------------------------------------------
103: def write_time_file(file_in, time, file_out, pop):
104: fout = open(file_out, 'w', encoding='utf-8')
105: lst_data = read_file(file_in) ; n = 1
106: for a in pop:
107: lst_data.pop(a)
108: for e in lst_data:
109: e.pop(0)
110: for i in range(len(lst_data)):
111: lst_data[i].insert(0,time[i])
112: for j in lst_data:
113: fout.write(str(n)+'\n') ; n += 1
114: for k in j:
115: fout.write(k)
116: fout.write('\n')
117: fout.close()
118: return
119: # ------------------------------------------
120: def filter_punc_sym(lst_script):
121: ind_remove = [-1] ; script_rmpunc = [] ; new_script = '' ; script_rmpunc_sym = [] ; ind_for_pop2 = [] ; script_rmpunc_v2 = [] ; text = [] ; Alf = False
122: for script in lst_script:
123: for i in range(len(script[0])):
124: if script[0][i] in ['(', '[', '{', '<', ')', ']', '}', '>']:
125: ind_remove.append(i)
126: ind_remove.append(len(script[0]))
127: for j in range(0,len(ind_remove),2):
128: new_script += script[0][ind_remove[j]+1:ind_remove[j+1]]
129: script_rmpunc.append([new_script])
130: ind_remove = [-1] ; new_script = ''
131: for b in script_rmpunc:
132: script_rmpunc_v2.append(b[0].strip().split('\n'))
133: #Check sym
134: for k in range(len(script_rmpunc_v2)):
135: for l in range(len(script_rmpunc_v2[k])):
136: for m in range(len(script_rmpunc_v2[k][l])):
137: if script_rmpunc_v2[k][l][m].isalnum() == True:
138: Alf = True
139: break
140: if Alf == False:
141: script_rmpunc_v2[k][l] = ''
142: Alf = False
143: for n in range(len(script_rmpunc_v2)):
144: if script_rmpunc_v2[n] == ['']:
145: ind_for_pop2.append(n)
146: ind_for_pop2 = ind_for_pop2[::-1]
147: for o in range(len(ind_for_pop2)):
148: script_rmpunc_v2.pop(ind_for_pop2[o])
149: return script_rmpunc_v2, ind_for_pop2
150: # ------------------------------------------
151: def write_clean_file(file_in, lst_clean_scrp, file_out, pop):
152: fout = open(file_out, 'w', encoding='utf-8')
153: lst_data = read_file(file_in) ; n = 1
154: for a in pop:
155: lst_data.pop(a)
156: for i in range(len(lst_data)):
157: fout.write(str(n)+'\n') ; n += 1
158: fout.write(lst_data[i][0])
159: for c in lst_clean_scrp[i]:
160: if c != '':
161: fout.write(c+'\n')
162: fout.write('\n')
163: fout.close()
164: return
165: # ------------------------------------------
166: def time_to_millisec(lst_time):
167: time = lst_time.split(',') ; time_ = time[0].split(':')
168: h = int(time_[0]) ; m = int(time_[1]) ; s = int(time_[2]) ; ms = int(time[1])
169: millisec = (h*60*60*1000) + (m*60*1000) + (s*1000) + ms
170: return millisec
171: # ------------------------------------------
172: def milli_to_time(millisec):
173: h = millisec//3600000 ; m = (millisec - (h*3600000))//60000 ; s = (millisec - (h*3600000) - (m*60000))//1000
174: ms = millisec - (h*3600000) - (m*60000) - (s*1000)
175: if h >= 0:
176: if len(str(h)) == 1:
177: h = '0' + str(h)
178: else: h = str(h)
179:
180: if len(str(m)) != 2:
181: m = '0' + str(m)
182: else: m = str(m)
183:
184: if len(str(s)) != 2:
185: s = '0' + str(s)
186: else: s = str(s)
187:
188: if len(str(ms)) == 1:
189: ms = '00' + str(ms)
190: elif len(str(ms)) == 2:
191: ms = '0' + str(ms)
192: else: ms = str(ms)
193: time = str(h)+':'+str(m)+':'+str(s)+','+str(ms)
194: else:
195: time = '00:00:00,000'
196: return time
197: # ------------------------------------------
198: def read_list(lst_i, main_lst):
199: while True:
200: if main_lst.index(lst_i) == len(main_lst)-1:
201: break
202: else:
203: time, scr = lst_i[0],lst_i[1:len(lst_i)]
204: return time,scr
205: return '',''
206: #-----------------------------------------------------
207: def write_merge_file(lst_base, file_out):
208: n = 1
209: fout = open(file_out, 'w', encoding='utf-8')
210: for i in lst_base:
211: fout.write(str(n)+'\n') ; n += 1
212: for k in i:
213: if len(k[0]) == 1:
214: fout.write(k)
215: else:
216: for j in k:
217: fout.write(j)
218: fout.write('\n')
219: fout.close()
220: return
221: #-----------------------------------------------------
6430045821 (96.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 90.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430045821
003:
004: def timetoms(time):
005: time = time.split(":")
006: ms = int(time[0])*3600*1000 + int(time[1])*60000
007: time = time[2].split(",")
008: ms += int(time[0])*1000 + int(time[1])
009: return ms
010:
011: def retime(time):
012: MS = time%1000
013: SS = time//1000%60
014: MM = time//60000%60
015: HH = time//3600000
016: last_time = str(HH).zfill(2) + ":" + str(MM).zfill(2) +\
017: ":" + str(SS).zfill(2) + "," + str(MS).zfill(3)
018: return last_time
019:
020: # ------------------------------------------
021: def shift(file_in, time_shift, file_out):
022: fin = open(file_in, encoding= 'utf-8')
023: fout = open(file_out, 'w', encoding= 'utf-8')
024: sub = fin.readlines()
025: no = 1
026: i = 0
027: while i < len(sub) :
028: k = sub[i].find(" --> ")
029: if k != -1 :
030: atime = timetoms(sub[i][:k:]) + time_shift
031: btime = timetoms(sub[i][k+5:-1:]) + time_shift
032: if atime >= 0 and btime >= 0 :
033: fout.write(str(no) + "\n")
034: fout.write(retime(atime) + " --> " + retime(btime) + "\n")
035: j = i+1
036: while sub[j] != "\n" :
037: fout.write(sub[j])
038: j += 1
039: fout.write("\n")
040: no += 1
041: elif btime > 0 :
042: atime = 0
043: fout.write(str(no) + "\n")
044: fout.write(retime(atime) + " --> " + retime(btime) + "\n")
045: j = i+1
046: while sub[j] != "\n" :
047: fout.write(sub[j])
048: j += 1
049: fout.write("\n")
050: no += 1
051: i += 1
052: fin.close()
053: fout.close()
054: return
055:
056: # ------------------------------------------
057: def group(file) :
058: i = 0
059: group = []
060: while i < len(file) :
061: k = file[i].find(" --> ")
062: if k != -1 :
063: atime = timetoms(file[i][:k:])
064: btime = timetoms(file[i][k+5:-1:])
065: i += 1
066: sub = []
067: while file[i] != "\n" :
068: sub.append(file[i])
069: i += 1
070: group.append([atime,btime,sub])
071: i += 1
072: return group
073:
074: def merge(base_file, merge_file, threshold, file_out):
075: import math
076: bf = open(base_file, encoding= 'utf-8')
077: mf = open(merge_file, encoding= 'utf-8')
078: bgroup = group(bf.readlines())
079: mgroup = group(mf.readlines())
080: for i in mgroup :
081: distance = math.inf
082: for j in range(len(bgroup)) :
083: if abs(i[0]-bgroup[j][0]) < distance :
084: base_add = j
085: distance = abs(i[0]-bgroup[j][0])
086: else : break
087: if distance <= threshold :
088: for subadd in i[2] :
089: bgroup[base_add][2].append(subadd)
090: else :
091: bgroup.append(i)
092: bgroup.sort()
093: no = 1
094: fout = open(file_out, 'w', encoding= 'utf-8')
095: for e in bgroup :
096: fout.write(str(no) + "\n")
097: fout.write(retime(e[0]) + " --> " + retime(e[1]) + "\n")
098: for k in e[2] :
099: fout.write(k)
100: fout.write("\n")
101: no += 1
102: bf.close()
103: mf.close()
104: fout.close()
105: return
106:
107: # ------------------------------------------
108: def clean(file_in, file_out):
109: fin = open(file_in, encoding= 'utf-8')
110: fout = open(file_out, 'w', encoding= 'utf-8')
111: subs = fin.readlines()
112: no = 1
113: i = 2
114: while i < len(subs) :
115: subtitle = ""
116: j = i
117: while subs[j] != "\n" :
118: subtitle += subs[j].strip() + "\n"
119: j += 1
120: ret = ""
121: skip = 0
122: for e in subtitle :
123: if e in ['[','(','<','{'] :
124: skip += 1
125: elif e in [']',')','>','}'] and skip > 0 :
126: skip -= 1
127: elif skip == 0 or e == "\n" :
128: ret += e
129: a = ""
130: sublist = []
131: for k in ret :
132: if k != "\n" :
133: a += k
134: else :
135: a = a.strip() + "\n"
136: sublist.append(a)
137: a = ""
138: write = False
139: for x in sublist :
140: deleteline = True
141: for z in x :
142: if z.isalnum() :
143: deleteline = False
144: write = True
145: break
146: if deleteline :
147: sublist.remove(x)
148: if write :
149: fout.write(str(no) + "\n")
150: fout.write(subs[i-1])
151: for y in range(len(sublist)) :
152: fout.write(sublist[y])
153: fout.write("\n")
154: no += 1
155: i = j+3
156: fin.close()
157: fout.close()
158: return
159:
160: # ------------------------------------------
6430052121 (33.33%)
shift -> 80.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = False |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = False |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = False |
clean -> 20.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
BBB
2
00:01:55,500 --> 00:02:10,600
BBB
3
00:59:50,100 --> 01:00:08,100
ZZZZ
4
01:01:05,100 --> 01:01:18,100
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = False |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = UnboundLocalError("local variable 'stop' referenced before assignment")
File "z:\HW08\_student.py", line 252, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 185, in clean
newsub = sub[j][:start]+sub[j][stop:]
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
111
AAA
2
00:01:55,500 --> 00:02:10,600
222
AAA
3
00:59:40,100 --> 01:00:08,100
333
33
CCC
4
01:01:05,100 --> 01:01:18,100
444
44
CC |
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:50,000 --> 00:01:56,000
111
YYY
2
00:01:55,001 --> 00:01:56,000
222
DDD
3
00:01:56,997 --> 00:01:56,998
333
CCC
4
00:01:56,998 --> 00:01:56,999
444
CCC
5
00:01:56,999 --> 00:04:22,100
555
CCC |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: def becomestr(k) :
007: if k == 0 :
008: return '00'
009: else :
010: if k//10 == 0 :
011: return '0'+str(int(k))
012: else :
013: return str(int(k))
014: def millistr(k) :
015: if k == 0 :
016: return '000'
017: else :
018: if k//10 == 0 :
019: return '00'+str(int(k))
020: elif k//100 == 0 :
021: return '0'+str(int(k))
022: else :
023: return str(int(k))
024: fin = open(file_in, 'r' ,encoding='utf-8')
025: fout = open(file_out, 'w' ,encoding='utf-8')
026: files = fin.readlines()
027: data = []
028: a = []
029: for i in files :
030: if i == '\n' :
031: data.append(a)
032: a = []
033: else :
034: a.append(i)
035: n = 0
036: for i in data :
037: time = i[1][:-1]
038: start,stop = time.split('-->')
039: mainstart,milstart = start.split(',')
040: mainstop,milstop = stop.split(',')
041: milstart = float(milstart) ; milstop = float(milstop)
042: hstart,mstart,sstart = mainstart.split(':')
043: hstop,mstop,sstop = mainstop.split(':')
044: hstart,mstart,sstart = float(hstart),float(mstart),float(sstart)
045: hstop ,mstop ,sstop = float(hstop),float(mstop),float(sstop)
046: sumstart = hstart*3600000+mstart*60000+sstart*1000+milstart+time_shift
047: sumstop = hstop*3600000+mstop*60000+sstop*1000+milstop+time_shift
048: hfinalstart,hfinalstop = sumstart//3600000 , sumstop//3600000
049: mfinalstart,mfinalstop = (sumstart-hfinalstart*3600000)//60000 , (sumstop-hfinalstop*3600000)//60000
050: sfinalstart,sfinalstop = (sumstart-hfinalstart*3600000-mfinalstart*60000)//1000 , (sumstop-hfinalstop*3600000-mfinalstop*60000)//1000
051: milfinalstart,milfinalstop = sumstart-hfinalstart*3600000-mfinalstart*60000-sfinalstart*1000 , sumstop-hfinalstop*3600000-mfinalstop*60000-sfinalstop*1000
052: hfinalstart,mfinalstart,sfinalstart = becomestr(hfinalstart),becomestr(mfinalstart),becomestr(sfinalstart)
053: hfinalstop,mfinalstop,sfinalstop = becomestr(hfinalstop),becomestr(mfinalstop),becomestr(sfinalstop)
054: milfinalstart,milfinalstop = millistr(milfinalstart),millistr(milfinalstop)
055: if sumstop > 0 :
056: if sumstart > 0 :
057: n+=1
058: finalstart = hfinalstart+':'+mfinalstart+':'+sfinalstart+','+milfinalstart
059: finalstop = hfinalstop+':'+mfinalstop+':'+sfinalstop+','+milfinalstop
060: fout.write(str(n)+'\n')
061: fout.write(finalstart+' '+'-->'+' '+finalstop+'\n')
062: for j in i[2:] :
063: fout.write(j)
064: fout.write('\n')
065: if sumstart < 0 :
066: n+=1
067: finalstart = '00:00:00,000'
068: finalstop = hfinalstop+':'+mfinalstop+':'+sfinalstop+','+milfinalstop
069: fout.write(str(n)+'\n')
070: fout.write(finalstart+' '+'-->'+' '+finalstop+'\n')
071: for j in i[2:] :
072: fout.write(j)
073: fout.write('\n')
074: else :
075: pass
076: fin.close
077: fout.close
078: return
079:
080: # ------------------------------------------
081: def merge(base_file, merge_file, threshold, file_out):
082: def alltime(x) :
083: start,stop = x.split('-->')
084: mainstart,milstart = start.split(',')
085: mainstop,milstop = stop.split(',')
086: milstart = float(milstart) ; milstop = float(milstop)
087: hstart,mstart,sstart = mainstart.split(':')
088: hstop,mstop,sstop = mainstop.split(':')
089: hstart,mstart,sstart = float(hstart),float(mstart),float(sstart)
090: hstop ,mstop ,sstop = float(hstop),float(mstop),float(sstop)
091: sumstart = hstart*3600000+mstart*60000+sstart*1000+milstart
092: return sumstart
093: filesbase = open(base_file,'r',encoding='utf-8')
094: filesmerge = open(merge_file,'r',encoding='utf-8')
095: fout = open(file_out,'w',encoding='utf-8')
096: fbase = filesbase.readlines()
097: fmerge = filesmerge.readlines()
098: database = []
099: datamerge = []
100: a = []
101: b = []
102: timebase = []
103: timemerge = []
104: sumbase = []
105: summerge = []
106: mergelist = []
107: timedifflist = []
108: for i in fbase :
109: if i == '\n' :
110: database.append(a)
111: a = []
112: else :
113: a.append(i)
114: for i in fmerge :
115: if i == '\n' :
116: datamerge.append(b)
117: b = []
118: else :
119: b.append(i)
120: for i in database :
121: timebase.append(i[1][:-1])
122: for i in datamerge :
123: timemerge.append(i[1][:-1])
124: for i in timebase :
125: sumbase.append(alltime(i))
126: for i in timemerge :
127: summerge.append(alltime(i))
128: for i in range(len(sumbase)) :
129: timediff = 1000000000
130: for j in range(len(summerge)) :
131: if abs(sumbase[i] - summerge[j]) < timediff and abs(sumbase[i] - summerge[j]) > threshold :
132: timediff = abs(sumbase[i] - summerge[j])
133: mergemin = j
134: timedifflist.append(timediff)
135: for k in datamerge[mergemin][2:] :
136: mergelist.append(k)
137: timediff = 1000000000
138: for i in range(len(database)) :
139: database[i].append(mergelist[i])
140: database[i].append(timedifflist[i])
141: finallist = []
142: for i in range(len(database)) :
143: finallist.append(database[i])
144: for j in range(len(database)) :
145: if database[i][-2] == database[j][-2] and database[j] not in finallist :
146: if database[i][-1] > database[j][-1] :
147: finallist.remove(database[i])
148: break
149: n = 0
150: for i in finallist :
151: n+=1
152: fout.write(str(n)+'\n')
153: for j in i[1:-1] :
154: fout.write(str(j))
155: fout.write('\n')
156: filesbase.close()
157: filesmerge.close()
158: fout.close()
159: return
160:
161: # ------------------------------------------
162: def clean(file_in, file_out):
163: fin = open(file_in, 'r' ,encoding='utf-8')
164: fout = open(file_out, 'w' ,encoding='utf-8')
165: files = fin.readlines()
166: data = []
167: a = []
168: for i in files :
169: if i == '\n' :
170: data.append(a)
171: a = []
172: else :
173: a.append(i)
174: for i in range(len(data)) :
175: sub = data[i][2:]
176: for j in range(len(sub)) :
177: for k in range(len(sub[j])) :
178: if sub[j][k] == '(' or sub[j][k] == '[' or sub[j][k] == '{' or sub[j][k] == '<' :
179: start = k
180: elif sub[j][k] == ')' or sub[j][k] == ']' or sub[j][k] == '}' or sub[j][k] == '>' :
181: stop = k+1
182: else :
183: pass
184: if '(' in sub[j] or '[' in sub[j] or '{' in sub[j] or '<' in sub[j] :
185: newsub = sub[j][:start]+sub[j][stop:]
186: data[i].pop(j+2)
187: data[i].append(newsub)
188: else :
189: newsub = sub[j]
190: data[i].pop(j+2)
191: data[i].append(newsub)
192: for i in range(len(data)) :
193: sub = data[i][2:]
194: for j in range(len(sub)) :
195: tester = ''
196: for k in range(len(sub[j])) :
197: if sub[j][k] != '.' and sub[j][k] != "'" and sub[j][k] != '"' and sub[j][k] != ' ' and sub[j][k] != ',' and sub[j][k] != '/' and sub[j][k] != '?' and sub[j][k] != '!' and sub[j][k] != '#' and sub[j][k] != '_'and sub[j][k] != '-' and sub[j][k] != '$' :
198: tester += sub[j][k]
199: if tester[:-2].isalnum() == True :
200: pass
201: else :
202: data[i].pop(j+2)
203: x = []
204: n = 0
205: for i in data :
206: if len(i) > 2 :
207: x.append(i)
208: for i in x :
209: n+=1
210: fout.write(str(n)+'\n')
211: for j in i[1:] :
212: fout.write(str(j))
213: fout.write('\n')
214: fin.close
215: fout.close
216: return
217:
218: # ------------------------------------------
6430053821 (100.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430053821
003:
004:
005: # ------------------------------------------
006: def str_time_to_milli(time):
007: hr,minute,sec = [int(e) for e in time[0:8].split(":")]
008: milli = int(time[-3::1])
009: want = hr*60*60*1000 + minute*60*1000 + sec*1000 + milli
010: return want
011:
012: def milli_to_str_time(milli):
013: if milli < 0:
014: milli = 0
015: hr = milli//(60*60*1000)
016: milli = milli - (hr*60*60*1000)
017: minute = milli//(60*1000)
018: milli = milli - (minute*60*1000)
019: sec = milli//1000
020: milli = milli - (sec*1000)
021: hr = str(hr)
022: minute = str(minute)
023: sec = str(sec)
024: milli = str(milli)
025: hr = "0"*(2-len(hr)) + hr
026: minute = "0"*(2-len(minute)) + minute
027: sec = "0"*(2-len(sec)) + sec
028: milli = "0"*(3-len(milli)) + milli
029: return (hr+":"+minute+":"+sec+","+milli)
030:
031: def clean_all_punct(string):
032: want = string
033: while "(" in want and ")" in want:
034: for i in range(len(want)):
035: if want[i] == "(":
036: start = i
037: end = want.find(")",i)
038: if end < start:
039: break
040: want = want[0:start] + want[end+1::1]
041: break
042: if end < start:
043: break
044: while "[" in want and "]" in want:
045: for i in range(len(want)):
046: if want[i] == "[":
047: start = i
048: end = want.find("]",i)
049: if end < start:
050: break
051: want = want[0:start] + want[end+1::1]
052: break
053: if end < start:
054: break
055: while "{" in want and "}" in want:
056: for i in range(len(want)):
057: if want[i] == "{":
058: start = i
059: end = want.find("}",i)
060: if end < start:
061: break
062: want = want[0:start] + want[end+1::1]
063: break
064: if end < start:
065: break
066: while "<" in want and ">" in want:
067: for i in range(len(want)):
068: if want[i] == "<":
069: start = i
070: end = want.find(">",i)
071: if end < start:
072: break
073: want = want[0:start] + want[end+1::1]
074: break
075: if end < start:
076: break
077: want = want.strip()
078: return want
079:
080: # ------------------------------------------
081: def shift(file_in, time_shift, file_out):
082: file = open(file_in,"r",encoding="utf-8")
083: ref = ""
084: for line in file:
085: ref += line
086: file.close()
087: ref = ref.split("\n")
088: want = []
089: for i in range(len(ref)):
090: x = []
091: if len(ref[i]) == len('00:00:04,000 --> 00:00:05,000') and "-->" in ref[i]:
092: n = ref.index("",i)
093: for e in range(i,n):
094: x.append(ref[e])
095: if x != []:
096: want.append(x)
097: for i in range(len(want)):
098: time1,time2 = want[i][0].split(" --> ")
099: want[i][0] = time1
100: want[i].insert(1,time2)
101: for i in range(len(want)):
102: a1 = str_time_to_milli(want[i][0])
103: a1 += time_shift
104: a1 = milli_to_str_time(a1)
105: want[i][0] = a1
106: a2 = str_time_to_milli(want[i][1])
107: a2 += time_shift
108: a2 = milli_to_str_time(a2)
109: want[i][1] = a2
110: want.sort()
111: remove_index = []
112: for i in range(len(want)):
113: if want[i][1] == "00:00:00,000":
114: remove_index.append(-i)
115: remove_index.sort()
116: for i in range(len(remove_index)):
117: want.pop(-remove_index[i])
118: want.sort()
119: for i in range(len(want)):
120: want[i][0] = want[i][0] + " --> " + want[i][1]
121: want[i].pop(1)
122: file1 = open(file_out,"w",encoding="utf-8")
123: for i in range(len(want)):
124: file1.write(str(i+1)+"\n")
125: for e in range(len(want[i])):
126: file1.write(want[i][e].strip()+"\n")
127: file1.write("\n")
128: file1.close()
129:
130: # ------------------------------------------
131: def merge(base_file, merge_file, threshold, file_out):
132: file = open(base_file,"r",encoding="utf-8")
133: ref = ""
134: for line in file:
135: ref += line
136: file.close()
137: ref = ref.split("\n")
138: base = []
139: for i in range(len(ref)):
140: x = []
141: if len(ref[i]) == len('00:00:04,000 --> 00:00:05,000') and "-->" in ref[i]:
142: n = ref.index("",i)
143: for e in range(i,n):
144: x.append(ref[e])
145: if x != []:
146: base.append(x)
147: file = open(merge_file,"r",encoding="utf-8")
148: ref1 = ""
149: for line in file:
150: ref1 += line
151: file.close()
152: ref1 = ref1.split("\n")
153: mergex = []
154: for i in range(len(ref1)):
155: x = []
156: if len(ref1[i]) == len('00:00:04,000 --> 00:00:05,000') and "-->" in ref1[i]:
157: n = ref1.index("",i)
158: for e in range(i,n):
159: x.append(ref1[e])
160: if x != []:
161: mergex.append(x)
162: base.sort()
163: mergex.sort()
164: for i in range(len(base)):
165: milli_time = str_time_to_milli(base[i][0][0:12])
166: base[i].insert(0,milli_time)
167: for i in range(len(mergex)):
168: milli_time = str_time_to_milli(mergex[i][0][0:12])
169: mergex[i].insert(0,milli_time)
170: merge_index = []
171: for e in range(len(mergex)):
172: closest = "e"
173: ind_base = "e"
174: ind_merge = "e"
175: for i in range(len(base)):
176: if abs(base[i][0]-mergex[e][0]) <= threshold:
177: if closest == "e":
178: closest = abs(base[i][0]-mergex[e][0])
179: ind_base = i
180: ind_merge = e
181: elif abs(base[i][0]-mergex[e][0]) < closest:
182: closest = abs(base[i][0]-mergex[e][0])
183: ind_base = i
184: ind_merge = e
185: if ind_base != "e" and ind_merge != "e":
186: merge_index.append([ind_base,ind_merge])
187: for [x,y] in merge_index:
188: for i in range(2,len(mergex[y])):
189: base[x].append(mergex[y][i])
190: pop_merge = []
191: for i in range(len(merge_index)):
192: pop_merge.append(-merge_index[i][-1])
193: pop_merge.sort()
194: for i in range(len(pop_merge)):
195: mergex.pop(-pop_merge[i])
196: for item in mergex:
197: base.append(item)
198: for i in range(len(base)):
199: base[i].pop(0)
200: base.sort()
201: file1 = open(file_out,"w",encoding="utf-8")
202: for i in range(len(base)):
203: file1.write(str(i+1)+"\n")
204: for e in range(len(base[i])):
205: file1.write(base[i][e].strip()+"\n")
206: file1.write("\n")
207: file1.close()
208:
209: # ------------------------------------------
210: def clean(file_in, file_out):
211: file = open(file_in,"r",encoding="utf-8")
212: ref = ""
213: for line in file:
214: ref += line
215: file.close()
216: ref = ref.split("\n")
217: want = []
218: for i in range(len(ref)):
219: x = []
220: if len(ref[i]) == len('00:00:04,000 --> 00:00:05,000') and "-->" in ref[i]:
221: n = ref.index("",i)
222: for e in range(i,n):
223: x.append(ref[e])
224: if x != []:
225: want.append(x)
226: for i in range(len(want)):
227: if len(want[i]) == 2:
228: pass
229: else:
230: remove = []
231: for e in range(2,len(want[i])):
232: want[i][1] += "\n" + want[i][e]
233: remove.append(-e)
234: remove.sort()
235: for ind in remove:
236: want[i].pop(-ind)
237: delete = []
238: for i in range(len(want)):
239: if len(want[i]) <= 1:
240: delete.append(-i)
241: delete.sort()
242: for indx in delete:
243: want.pop(-indx)
244: for i in range(len(want)):
245: want[i][1] = clean_all_punct(want[i][1])
246: remove_ind = []
247: for i in range(len(want)):
248: if len(want[i][1]) == 0:
249: remove_ind.append(-i)
250: remove_ind.sort()
251: for index in remove_ind:
252: want.pop(-index)
253: for i in range(len(want)):
254: if "\n" in want[i][1]:
255: a = want[i].pop(1)
256: a = a.split("\n")
257: for item in a:
258: if len(item) != 0:
259: want[i].append(item)
260: for i in range(len(want)):
261: remove_ind = []
262: for e in range(1,len(want[i])):
263: check = False
264: for z in range(len(want[i][e])):
265: if want[i][e][z].isalnum():
266: check = True
267: break
268: if not check:
269: remove_ind.append(-e)
270: remove_ind.sort()
271: if len(remove_ind) != 0:
272: for index in remove_ind:
273: want[i].pop(-index)
274: remove_ind = []
275: for i in range(len(want)):
276: if len(want[i]) == 1:
277: remove_ind.append(-i)
278: remove_ind.sort()
279: if len(remove_ind) != 0:
280: for index in remove_ind:
281: want.pop(-index)
282: want.sort()
283: file1 = open(file_out,"w",encoding="utf-8")
284: for i in range(len(want)):
285: file1.write(str(i+1)+"\n")
286: for e in range(len(want[i])):
287: file1.write(want[i][e].strip()+"\n")
288: file1.write("\n")
289: file1.close()
290:
291: # ------------------------------------------
6430071021 (0.0%)
shift -> 0%
File "", line 101
if
^
SyntaxError: invalid syntax
|
clean -> 0%
File "", line 101
if
^
SyntaxError: invalid syntax
|
merge -> 0%
File "", line 101
if
^
SyntaxError: invalid syntax
|
001: # Subtitle Tools
002: # Your ID
003: def time_to_sec(time):
004: h,m,s = time.split(':')
005: s,ms = s.split(',')
006: h = int(h)*3600000
007: m = int(m)*60000
008: s = int(s)*1000
009: ms = int(ms)
010: milsec = h + m + s +ms
011: return milsec
012: def sec_to_time(sec):
013: h = sec // 3600000
014: m = (sec % 3600000)//60000
015: s = (sec % 60000)//1000
016: ms = sec % 1000
017: l = str(s) + ',' + str(ms)
018: time = str(h) + ':'+ str(m) + ':' + l
019: return time
020: def pair_file(file1 , file2):
021: f1 = open(file1 ,'r' , encoding='utf-8')
022: f2 = open(file2 ,'r' , encoding='utf-8')
023: if f1 != f2 :
024: return 'Error'
025: f.close()
026: return 'Right'
027: def group_file(file):
028: f = open(file ,'w',encoding='utf-8')
029: if l == ' ':
030: l.write('\n')
031: f.close()
032: return f
033:
034: # ------------------------------------------
035: def shift(file_in, time_shift, file_out):
036: k = group_file(file_in)
037: Lost = []
038: for line in k:
039: Lost.append([line])
040: Why = []
041: What = []
042: for p in Lost:
043: change = p.pop(1)
044: p.remove(p[0])
045: start,end = change.split('-->')
046: mus1 = time_to_sec(start)
047: mus2 = time_to_sec(end)
048: Why.append([mus1,mus2])
049: What.append(p)
050: op = []
051: for t in Why:
052: if time_shift > 0:
053: t[0] += time_shift
054: t[1] += time_shift
055: c = sec_to_time(t[0])
056: k = sec_to_time(t[1])
057: j = c + '-->' + k
058: else:
059: if t[0] + time_shift > 0 and t[1] + time_shift > 0:
060: t[0] += time_shift
061: t[1] += time_shift
062: c = sec_to_time(t[0])
063: k = sec_to_time(t[1])
064: j = c + '-->' + k
065: elif t[0] + time_shift < 0 and t[1] + time_shift > 0:
066: c = '00:00:00,000'
067: t[1] += time_shift
068: k = sec_to_time(t[1])
069: j = c + '-->' + k
070: elif t[0] + time_shift < 0 and t[1] + time_shift < 0:
071: What.remove(What[0])
072: Why.remove(t)
073: pass
074: op.append(j)
075: for i in range(len(Why)):
076: print(i+1)
077: print(op[i])
078: for l in p[i]:
079: print(l)
080:
081: return 0
082:
083: # ------------------------------------------
084: def merge(base_file, merge_file, threshold, file_out):
085: bf = group_file(base_file)
086: mf = group_file(merge_file)
087: st = []
088: ed = []
089: for line in bf :
090: time = line[1]
091: start,end = time.split('-->')
092: o = time_to_sec(start)
093: st.append(o)
094: for line in mf:
095: time = line[1]
096: start,end = time.split('-->')
097: p = time_to_sec(start)
098: ed.append(p)
099: for i in range(len(st)):
100: for l in range(len(ed)):
101: if
102:
103:
104:
105: return
106:
107: # ------------------------------------------
108: def clean(file_in, file_out):
109: fin = open(file_in , encoding='utf-8')
110: line = fin.readline()
111: Make = []
112: for line in fin:
113: while line != ' ':
114: Make.append(line)
115: for k in Make:
116: if
117:
118:
119: return
120:
121: # ------------------------------------------
6430076221 (76.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = [['1', ['00:03:20,600', '00:03:23,600'], ['AAAA AAAA AAA.']], ['2', ['00:03:31,000', '00:03:46,100'], ['AAAA AAAA AAA.']], ['3', ['01:01:25,600', '01:01:43,600'], ['AAAA AAAA AAA.']], ['4', ['01:02:40,600', '01:02:53,600'], ['AAAA AAAA AAA.']]]
, close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = [['1', ['00:00:09,600', '00:00:12,600'], ['AAAA AAAA AAA.']], ['2', ['00:00:20,000', '00:00:35,100'], ['AAAA AAAA AAA.']], ['3', ['00:58:14,600', '00:58:32,600'], ['AAAA AAAA AAA.']], ['4', ['00:59:29,600', '00:59:42,600'], ['AAAA AAAA AAA.']]]
, close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = [['1', ['00:00:00,000', '00:00:00,000'], ['AAAA AAAA AAA.']], ['2', ['00:00:00,000', '00:00:00,100'], ['AAAA AAAA AAA.']], ['3', ['00:57:39,600', '00:57:57,600'], ['AAAA AAAA AAA.']], ['4', ['00:58:54,600', '00:59:07,600'], ['AAAA AAAA AAA.']]]
, close_file = True |
clean -> 80.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
AA
A♪
2
00:01:55,500 --> 00:02:10,600
AA
3
01:03:45,100 --> 01:03:48,100
A♪
1 |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 50.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:49,500 --> 00:01:50,000
AAA
4
00:01:50,000 --> 00:01:56,000
111
5
00:01:55,001 --> 00:01:56,000
222
CCC
6
00:01:56,997 --> 00:01:56,998
333
DDD
7
00:01:56,998 --> 00:01:56,999
444
DDD
8
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430076221
003:
004:
005:
006: # ------------------------------------------
007: def shift(file_in, time_shift, file_out):
008: f_i = open(file_in, "r",encoding='utf-8')
009: data = []
010: l_data = []
011: for line_i in f_i:
012: if line_i == "\n":
013: data.append(l_data)
014: l_data=[]
015: else:
016: l_data.append(line_i[:-1])
017: if l_data!=[]:
018: data.append(l_data)
019:
020:
021: data_2=[]
022: for l in data:
023: text=[]
024: for i in l[2:]:
025: text.append(i)
026: data_2.append([l[0],l[1].split(" --> "),text])
027: dh = time_shift//3600000
028: dm = time_shift%3600000//60000
029: ds = time_shift%60000//1000
030: dms = time_shift %1000
031: for l in range(len(data_2)):
032: l_t=[]
033: for t in data_2[l][1]:
034: s_t = t.split(":")
035: h = int(s_t[0])
036: m = int(s_t[1])
037: as_t=s_t[2].split(",")
038: s = int(as_t[0])
039: ms=int(as_t[1])
040: nms = ms+dms
041: ns = s+ds+(nms//1000)
042: nms = nms%1000
043: nm = m+dm+(ns//60)
044: ns = ns%60
045: nh = h+dh+(nm//60)
046: nm = nm%60
047:
048: if nms<0 or ns<0 or nm<0 or nh<0:
049: nms,ns,nm,nh = 0,0,0,0
050: nh = str(nh)
051: nm = str(nm)
052: ns = str(ns)
053: nms = str(nms)
054:
055: while len(nh)<2:
056: nh="0"+nh
057: while len(nm)<2:
058: nm="0"+nm
059: while len(ns)<2:
060: ns="0"+ns
061: while len(nms)<3:
062: nms="0"+nms
063: l_t.append(nh+":"+nm+":"+ns+","+nms)
064: data_2[l][1] = l_t
065:
066: f_o = open(file_out, "w",encoding='utf-8')
067: print(data_2)
068: n=1
069: for l in data_2:
070: if l[1] == ["00:00:00,000","00:00:00,000"]:
071: pass
072: else:
073: f_o.write(str(n)+"\n")
074: n+=1
075: f_o.write(l[1][0]+" --> "+l[1][1]+"\n")
076: for sl in l[2]:
077: f_o.write(sl+"\n")
078: f_o.write("\n")
079:
080:
081: f_i.close()
082: f_o.close()
083:
084:
085:
086:
087: # ------------------------------------------
088:
089:
090: def merge(base_file, merge_file, threshold, file_out):
091: f_b = open(base_file, "r",encoding='utf-8')
092: data = []
093: l_data = []
094: for line_b in f_b:
095: if line_b == "\n":
096: data.append(l_data)
097: l_data=[]
098: else:
099: l_data.append(line_b[:-1])
100: if l_data!=[]:
101: data.append(l_data)
102:
103:
104: data_2=[]
105: for l in data:
106: text=[]
107: for i in l[2:]:
108: text.append(i)
109: data_2.append([l[0],l[1].split(" --> "),text])
110: for l in range(len(data_2)):
111: l_t=[]
112: for t in data_2[l][1]:
113: s_t = t.split(":")
114: h = int(s_t[0])
115: m = int(s_t[1])
116: as_t=s_t[2].split(",")
117: s = int(as_t[0])
118: ms=int(as_t[1])
119:
120: data_2[l].append(h*3600000+m*60000+s*1000+ms)
121:
122:
123: f_m = open(merge_file, "r",encoding='utf-8')
124: mdata = []
125: m_data = []
126: for line_m in f_m:
127: if line_m == "\n":
128: mdata.append(m_data)
129: m_data=[]
130: else:
131: m_data.append(line_m[:-1])
132: if m_data!=[]:
133: mdata.append(m_data)
134:
135:
136: data_3=[]
137: for l in mdata:
138: text=[]
139: for i in l[2:]:
140: text.append(i)
141: data_3.append([l[0],l[1].split(" --> "),text])
142: for l in range(len(data_3)):
143: l_t=[]
144: for t in data_3[l][1]:
145: s_t = t.split(":")
146: h = int(s_t[0])
147: m = int(s_t[1])
148: as_t=s_t[2].split(",")
149: s = int(as_t[0])
150: ms=int(as_t[1])
151:
152: data_3[l].append(h*3600000+m*60000+s*1000+ms)
153:
154:
155:
156:
157:
158:
159: for i in range(len(data_2)):
160: data_2[i][0] = data_2[i][3]
161: data_2[i].append("b")
162: for i in range(len(data_3)):
163: data_3[i][0] = data_3[i][3]
164: data_3[i].append("m")
165:
166: f_data=[]
167: for k in range(len(data_2)):
168: text = data_2[k][2]
169: for j in range(len(data_3)):
170: if abs(data_2[k][0]-data_3[j][0])<threshold:
171: text = data_2[k][2]+data_3[j][2]
172: data_3[j][4]="med"
173: f_data.append([data_2[k][0],data_2[k][1],text])
174:
175: for l in data_3:
176: if l[4]!="med":
177: f_data.append(l)
178:
179: f_data.sort()
180:
181:
182:
183:
184:
185:
186:
187: f_o = open(file_out, "w",encoding='utf-8')
188: n=1
189: for l in f_data:
190: if l[1] == ["00:00:00,000","00:00:00,000"]:
191: pass
192: else:
193: f_o.write(str(n)+"\n")
194: n+=1
195: f_o.write(l[1][0]+" --> "+l[1][1]+"\n")
196: for sl in l[2]:
197: f_o.write(sl+"\n")
198: f_o.write("\n")
199:
200: f_o.close()
201: f_b.close()
202: f_m.close()
203:
204: # ------------------------------------------
205:
206:
207: def clean(file_in, file_out):
208: f_i = open(file_in, "r",encoding='utf-8')
209: data = []
210: l_data = []
211: for line_i in f_i:
212: if line_i == "\n":
213: data.append(l_data)
214: l_data=[]
215: else:
216: l_data.append(line_i[:-1])
217: if l_data!=[]:
218: data.append(l_data)
219: n_data=[]
220: for i in range(len(data)):
221: s_d = []
222: for l in data[i][2:]:
223: s_d.append(l)
224:
225: s_l = []
226:
227: for l in s_d:
228: pn = 0
229: s = ""
230: have_na = False
231: for c in l:
232: if pn<=0 and not c in "(<[{)>]}":
233: s += c
234: elif c in "(<[{":
235: pn+=1
236: elif c in ")>]}":
237: pn -= 1
238: for c in s:
239: if c.isalnum():
240: have_na=True
241: if s == "" or not have_na:
242: pass
243: else:
244: s_l.append(s)
245: data[i][2]=s_l
246:
247:
248: f_o = open(file_out, "w",encoding='utf-8')
249: n=1
250: for l in data:
251: if l[2] == []:
252: pass
253: else:
254: f_o.write(str(n)+"\n")
255: n+=1
256: f_o.write(l[1]+"\n")
257: for line_s in l[2]:
258: f_o.write(line_s+"\n")
259: f_o.write("\n")
260:
261: f_i.close()
262: f_o.close()
263:
264: # ------------------------------------------
6430083621 (90.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430083621
003:
004: # ------------------------------------------
005: ## helper functions ##
006: #text to split list
007: def split_text(srt_text):
008: split_list = srt_text.split('\n\n')
009: split_list = [obj.split('\n') for obj in split_list]
010: split_list = [list(filter(None, obj)) for obj in split_list]
011: return list(filter(None, split_list))
012:
013: #split list to text
014: def list_to_text(split_list):
015: srt_text = ''
016: for text in split_list:
017: srt_text += '\n'.join(text)
018: srt_text += '\n\n'
019: return srt_text
020:
021: #reindex
022: def re_index(split_list):
023: #sort time
024: split_list = sorted(split_list, key=lambda x:get_millisec(get_start_time(x[1])))
025: #reindex
026: obj_len = len(split_list)
027: for indx in range(0,obj_len):
028: indx_str = str(indx+1)
029: split_list[indx][0] = indx_str
030: return split_list
031:
032: #get_start_time
033: def get_start_time(time_str):
034: start_time = time_str.split(' --> ')[0]
035: return start_time
036: def get_end_time(time_str):
037: end_time = time_str.split(' --> ')[1]
038: return end_time
039:
040: def get_millisec(time_string):
041: h,m,s = time_string.split(':')
042: s,ms = s.split(',')
043: time_sec = (int(h)*3600+int(m)*60+int(s))*1000 + int(ms)
044: return time_sec
045:
046: def get_time_format(time_millisec):
047: h = '{0:02}'.format(int((time_millisec/1000)/3600))
048: m = '{0:02}'.format(int((time_millisec/1000)%3600/60))
049: s = '{0:02}'.format(int((time_millisec/1000)%3600%60))
050: ms = '{0:03}'.format(time_millisec%1000)
051: time_format = h+':'+m+':'+s+','+ms
052: return time_format
053:
054: def shift(file_in, time_shift, file_out):
055: fin = open(file_in,encoding='utf-8')
056: fin_list = fin.read()
057: fin.close()
058: fout_list = split_text(fin_list)
059: stack_del = []
060: for indx,fout_obj in enumerate(fout_list):
061: interval_time = fout_obj[1]
062: start_time = get_millisec(get_start_time(interval_time))
063: end_time = get_millisec(get_end_time(interval_time))
064: new_start_time = start_time + time_shift
065: new_end_time = end_time + time_shift
066: cast_new_start_time = 0 if new_start_time < 0 else new_start_time
067: new_interval_time = get_time_format(cast_new_start_time) + ' --> ' + get_time_format(new_end_time)
068: fout_list[indx][1] = new_interval_time
069: if (new_start_time <= 0) & (new_end_time <= 0):
070: stack_del.append(indx)
071: fout_list = [del_obj for del_indx, del_obj in enumerate(fout_list) if del_indx not in stack_del]
072: fout_list = re_index(fout_list)
073: fout_text = list_to_text(fout_list)
074: fout = open(file_out,'w',encoding='utf-8')
075: fout.writelines(fout_text)
076: fout.close()
077:
078: # ------------------------------------------
079: def merge(base_file, merge_file, threshold, file_out):
080: #base file
081: basein = open(base_file,encoding='utf-8')
082: basein_text = basein.read()
083: basein.close()
084: basein_list = split_text(basein_text)
085: #merge file
086: mergein = open(merge_file,encoding='utf-8')
087: mergein_text = mergein.read()
088: mergein.close()
089: mergein_list = split_text(mergein_text)
090: #distance calculation
091: match_dict = {}
092: diff_list = []
093: for base_indx,base_obj in enumerate(basein_list):
094: base_interval_time = base_obj[1]
095: base_start_time = get_millisec(get_start_time(base_interval_time))
096: tmp_dist = []
097: for merge_indx,merge_obj in enumerate(mergein_list):
098: merge_interval_time = merge_obj[1]
099: merge_start_time = get_millisec(get_start_time(merge_interval_time))
100: time_diff = abs(base_start_time-merge_start_time)
101: tmp_dist.append(time_diff)
102: tmp_min_diff = [(base_indx,i,v) for i,v in enumerate(tmp_dist) if v <= threshold]
103: diff_list.extend(tmp_min_diff)
104: # drop duplicates
105: for i,v in enumerate(diff_list):
106: match_dict.setdefault(v[1],[]).append((v[0],v[2]))
107: for i,v in match_dict.items():
108: if len(v) > 1:
109: match_dict[i] = [min(v,key=lambda t: t[1])]
110: #merge srt list
111: fout_list = basein_list.copy()
112: del_list = []
113: for i,v in match_dict.items():
114: fout_list[v[0][0]].extend(mergein_list[i][2:])
115: del_list.append(i)
116: remain_merge_list = [del_obj for del_indx, del_obj in enumerate(mergein_list) if del_indx not in del_list]
117: fout_list.extend(remain_merge_list)
118: #re index
119: fout_list = re_index(fout_list)
120:
121: fout_text = list_to_text(fout_list)
122: fout = open(file_out,'w',encoding='utf-8')
123: fout.writelines(fout_text)
124: fout.close()
125:
126: # ------------------------------------------
127: def clean(file_in, file_out):
128: fin = open(file_in,encoding='utf-8')
129: fin_text = fin.read()
130: fin.close()
131: fin_list = split_text(fin_text)
132: start_alp = ['(','[','{','<']
133: end_alp = [')',']','}','>']
134: del_srt = []
135: for srt_indx,srt_value in enumerate(fin_list):
136: result = ''
137: con_del = False
138: sub_title = '\n'.join(srt_value[2:])
139: for indx,value in enumerate(list(sub_title)):
140: if (any(value in s for s in start_alp)):
141: con_del = True
142: elif (any(value in s for s in end_alp)):
143: con_del = False
144: elif con_del == False:
145: result += value
146: fin_list[srt_indx][2:] = [result]
147: alnum_count = 0
148: for i in result:
149: if i.isalnum():
150: alnum_count+=1
151: if alnum_count == 0 :
152: del_srt.append(srt_indx)
153: fout_list = [del_obj for del_indx, del_obj in enumerate(fin_list) if del_indx not in del_srt]
154: fout_list = re_index(fout_list)
155: fout_text = list_to_text(fout_list)
156: fout = open(file_out,'w',encoding='utf-8')
157: fout.writelines(fout_text)
158: fout.close()
159:
160:
161: # ------------------------------------------
6430089421 (100.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: def str_to_int(time):
002: a = time.split(':')
003: b = a[2].split(',')
004: c = int(b[0])*1000
005: d = c + int(b[1])
006: e = int(a[0])*1000*60*60
007: f = int(a[1])*1000*60
008: g = e+f+d
009: return g
010:
011: def int_to_str(number):
012: a = number//(1000*60*60)
013: b = (number%(1000*60*60))//(1000*60)
014: c = (number%(1000*60))//1000
015: d = number%1000
016: e = str(a).zfill(2)+':'+str(b).zfill(2)+':'+str(c).zfill(2)+','+str(d).zfill(3)
017: return e
018:
019: def read_file(file):
020: a = open(file , encoding='utf-8')
021: b = a.read().strip().split('\n\n')
022: d = []
023: for i in range(len(b)):
024: c = b[i].split('\n')
025: e = c[1].split('-->')
026: f = str_to_int(e[0])
027: g = str_to_int(e[1])
028: d.append([f,g,c[2:]])
029: a.close()
030: return d
031:
032: def fileout(outfile , infile):
033: a = open(outfile, 'w', encoding='utf-8')
034: for i in range(len(infile)):
035: a.write(str(i+1)+'\n')
036: a.write(int_to_str(infile[i][0])+' --> '+int_to_str(infile[i][1])+'\n')
037: a.write('\n'.join(infile[i][2])+'\n')
038: a.write('\n')
039: a.close()
040:
041: def check(massage,start,stop):
042: while start in massage and stop in massage:
043: a = massage.index(start)
044: b = massage.index(stop)
045: massage = massage[:a]+massage[b+1:]
046: return massage
047:
048: def shift(file_in, time_shift, file_out):
049: a = read_file(file_in)
050: d = []
051: for i in range(len(a)):
052: b = a[i][0] + time_shift
053: c = a[i][1] + time_shift
054: if b < 0:
055: b = 0
056: if c < 0:
057: c = 0
058: if b == 0 and c == 0:
059: pass
060: else:
061: d.append([b,c,a[i][2]])
062: fileout(file_out , d)
063:
064: def merge(base_file, merge_file, threshold, file_out):
065: a = read_file(base_file)
066: b = read_file(merge_file)
067: d = []
068: for i in range(len(b)):
069: min = abs(b[i][0]-a[0][0])
070: pos = 0
071: for e in range(len(a)):
072: c = abs(b[i][0]-a[e][0])
073: if c < min:
074: min = c
075: pos = e
076: if min <= threshold:
077: a[pos][2] += b[i][2]
078: else:
079: d.append(b[i])
080: a += d
081: a.sort()
082: fileout(file_out,a)
083:
084: def clean(file_in, file_out):
085: a = read_file(file_in)
086: j = []
087: for i in range(len(a)):
088: b = '\n'.join(a[i][2])
089: c = check(b,'(',')')
090: d = check(c,'[',']')
091: e = check(d,'{','}')
092: f = check(e,'<','>')
093: g = f.split('\n')
094: h = []
095: for k in range(len(g)):
096: for z in g[k]:
097: if z.isalnum() == True:
098: h.append(g[k])
099: break
100: if h == []:
101: pass
102: else:
103: j.append([a[i][0],a[i][1],h])
104: fileout(file_out,j)
6430090021 (93.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 80.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = IndexError('string index out of range')
File "z:\HW08\_student.py", line 261, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 211, in clean
while part[line][-1] == ' ':
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430090021
003:
004: def is_timestamp(line):
005: return line.count(':') >= 4 and '-->' in line
006:
007: def timestamp_to_millisec_shift(timestamp:str, time_shift:int = 0):
008: time = int(timestamp[:timestamp.index(':')]) * 60
009: timestamp = timestamp[timestamp.index(':') + 1:]
010: time += int(timestamp[:timestamp.index(':')])
011: time *= 60000
012: timestamp = timestamp[timestamp.index(':') + 1:]
013: timestamp = timestamp[:timestamp.index(',')] + timestamp[timestamp.index(',') + 1:]
014: time += int(timestamp) + time_shift
015: return time
016:
017: def millisec_to_timestamp(time:int):
018: min = time // 60000
019: time %= 60000
020: time = f"00000{str(time)}"[-5:]
021: time = time[:2] + ',' + time[2:]
022: hr = f"0{str(min // 60)}"[-(2 if min // 60 < 10 else len(str(min // 60))):]
023: min = f"0{str(min % 60)}"[-2:]
024: return f"{hr}:{min}:{time}"
025:
026: def sort_srt_by_start_time(sections:list):
027: Out = []
028: for item in range(len(sections)):
029: start = sections[item][0][:sections[item][0].index('-')].strip()
030: sections[item] = timestamp_to_millisec_shift(start), sections[item]
031: sections.sort()
032: for item in range(len(sections)):
033: Out.append(sections[item][1])
034: return Out
035:
036: def find_section_end(original:list, line_start:int):
037: for line_index in range(line_start + 1, len(original)):
038: if is_timestamp(original[line_index]):
039: return line_index - 2
040: return len(original) - 1
041:
042: def get_srt_section(original:list):
043: Out = []
044: for line_index in range(len(original)):
045: if is_timestamp(original[line_index]):
046: Out.append(original[line_index : find_section_end(original, line_index) + 1])
047: for line in range(len(Out[-1])):
048: if not '\n' in Out[-1][line]:
049: Out[-1][line]+='\n'
050: return Out
051:
052: def srt_remove_end_spacing(original:list):
053: for section in range(len(original)):
054: while original[section][-1] == '\n':
055: original[section] = original[section][:-1]
056: # ------------------------------------------
057: def shift(file_in:str, time_shift:int, file_out:str): # might as well do this
058: """Create a copy of a file in .srt with all timestamps shifted by a specific amount
059:
060: :param str file_in: Name of the original file to shift the timestamps
061: :param int time_shift: The amount of time to shift the timestamps in millisecond
062: :param str file_out: Name of the output file.
063:
064: """
065: content_copy = open(file_in,"r", encoding='utf-8').readlines()
066: out_name = file_out
067: if file_out[-4:] != '.srt':
068: out_name += ".srt"
069: new_file = open(out_name, "w", encoding='utf-8')
070: new_file.truncate(0)
071: line_number = 1
072: content_copy = sort_srt_by_start_time(get_srt_section(content_copy))
073: sep_dist = 0
074: for line in content_copy[0]:
075: if line == '\n':
076: sep_dist += 1
077: srt_remove_end_spacing(content_copy)
078: for section_index in range(len(content_copy)):
079: content_copy[section_index].extend(['\n' for _ in range(sep_dist)])
080: line = content_copy[section_index][0]
081: start = line[:line.index('-')].strip()
082: end = line[line.index('>') + 1:].strip()
083: end_time = timestamp_to_millisec_shift(end, time_shift)
084: if end_time > 0:
085: end = millisec_to_timestamp(end_time)
086: start_time = timestamp_to_millisec_shift(start, time_shift)
087: if start_time <= 0:
088: start_time = 0
089: start = millisec_to_timestamp(start_time)
090: content_copy[section_index][0] = start + " --> " + end + '\n'
091: new_file.writelines([str(line_number)] + ['\n'] + content_copy[section_index])
092: line_number+=1
093: new_file.close()
094: return
095:
096: # ------------------------------------------
097: def merge(base_file:str, merge_file:str, threshold:int, file_out:str):
098: """Create a new .srt file with subtitles from both of the input files.
099: Merge timestamps within the specified threshold based on the base_file.
100:
101: :param str base_file: Name of the file to base the merged timestamps on.
102: :param str merge_file: Name of the file to merge the subtitles.
103: :param int threshold: Merge threshold in millisecond
104: :param str file_out: Name of the output file.
105:
106: """
107: base = open(base_file, 'r', encoding='utf-8').readlines()
108: out_name = file_out
109: if file_out[-4:] != '.srt':
110: out_name += ".srt"
111: new_file = open(out_name, "w", encoding='utf-8')
112: new_file.truncate(0)
113: base = sort_srt_by_start_time(get_srt_section(base))
114: base_time = []
115: for section in base:
116: start = section[0][:section[0].index('-')].strip()
117: base_time.append([timestamp_to_millisec_shift(start), len(base_time)+1])
118: additive = open(merge_file, 'r', encoding='utf-8').readlines()
119: additive = sort_srt_by_start_time(get_srt_section(additive))
120: additive_time = []
121: for section in additive:
122: start = section[0][:section[0].index('-')].strip()
123: additive_time.append([timestamp_to_millisec_shift(start), len(additive_time)+1])
124: sep_dist = 0
125: for line in base[0]:
126: if line == '\n':
127: sep_dist += 1
128: srt_remove_end_spacing(base)
129: srt_remove_end_spacing(additive)
130: line_number = 1
131: merge_index = 0
132: for section_index in range(len(base_time)):
133: merged_section = base[section_index]
134: try:
135: cont = True
136: while additive_time[merge_index][0] - base_time[section_index][0] <= threshold and cont:
137: time_difference = additive_time[merge_index][0] - base_time[section_index][0]
138: if time_difference >= -threshold:
139: if section_index+1 < len(base_time):
140: if abs(time_difference) > abs(additive_time[merge_index][0] - base_time[section_index+1][0]):
141: break
142: merged_section.extend(additive[merge_index][1:])
143: else:
144: new_file.writelines([str(line_number) + "\n"] + additive[merge_index] + ['\n' for _ in range(sep_dist)])
145: line_number += 1
146: merge_index += 1
147: merged_section.extend(['\n' for _ in range(sep_dist)])
148: except IndexError:
149: pass
150: new_file.writelines([str(line_number) + "\n"] + merged_section)
151: line_number += 1
152: if merge_index < len(additive_time):
153: for line in range(merge_index, len(additive_time)):
154: new_file.writelines([str(line_number) + "\n"] + additive[merge_index + line-merge_index] + ['\n' for _ in range(sep_dist)])
155: line_number += 1
156: new_file.close()
157: return
158:
159: # ------------------------------------------
160: def clean(file_in:str, file_out:str):
161: """Create a new .srt file with subtitles from the input file while removing
162: bracketed subtitles and non character.
163:
164: :param str file_in: Name of the file to clean up.
165: :param str file_out: Name of the output file.
166:
167: """
168: original = open(file_in, 'r', encoding='utf-8').readlines()
169: out_name = file_out
170: if file_out[-4:] != '.srt':
171: out_name += ".srt"
172: new_file = open(out_name, 'w', encoding='utf-8')
173: new_file.truncate(0)
174: original = sort_srt_by_start_time(get_srt_section(original))
175: sep_dist = 0
176: for line in original[0]:
177: if line == '\n':
178: sep_dist += 1
179: srt_remove_end_spacing(original)
180: line_number = 1
181: for section in original:
182: part = section[1:]
183: part = list("".join(part))
184: for bracket in (('(',')'),('[',']'),('{','}'),('<','>')):
185: while part.count(bracket[0])>0 and part.count(bracket[1])>0:
186: closing_index = part.index(bracket[1])
187: opening_index = part.index(bracket[0])
188: cont = True
189: while opening_index < closing_index and cont:
190: try:
191: opening_index = opening_index + part[opening_index+1].index(bracket[0]) + 1
192: except ValueError:
193: cont = False
194: for char_index in range(opening_index, closing_index+1):
195: if part[char_index] != '\n':
196: part[char_index] = -1
197: for char_index in range(part.count(-1)):
198: part.remove(-1)
199: while part[0] == ' ':
200: part.remove(' ')
201: count = 0
202: del bracket
203: for char in part:
204: if char.isalnum():
205: count = 1
206: if count > 0:
207: npart = []
208: part = "".join(part).split("\n")
209: for line in range(len(part)):
210: if len(part[line]) > 0:
211: while part[line][-1] == ' ':
212: part[line] = part[line][:-1]
213: for line in part:
214: count = 0
215: for char in range(len(line)):
216: if line[char].isalnum():
217: count=1
218: if count == 1:
219: npart.append(line)
220: part = npart
221: for line in range(len(part)):
222: part[line] = part[line] + '\n'
223: new_file.writelines([str(line_number) +'\n', section[0]] + part + ['\n' for _ in range(sep_dist)])
224: line_number +=1
225: new_file.close()
226: return
227:
228: # ------------------------------------------#
6430096821 (83.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:49,500 --> 00:01:50,000
AAA
4
00:01:50,000 --> 00:01:56,000
111
5
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
6
00:01:56,997 --> 00:01:56,998
333
7
00:01:56,998 --> 00:01:56,999
444
8
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430096821 Noppawut Polpornpichet
003: #-------------------------------------------
004: #sub function
005: def start_stop_time(line):
006: start,stop = [str(e) for e in line.strip().split("-->")]
007: start = start.strip()
008: stop = stop.strip()
009: return start,stop
010:
011: def time_to_milli(time):
012: x,milli = [str(e) for e in time.split(",")]
013: milli = int(milli)
014: hour,minute,sec = [int(e) for e in x.split(":")]
015: milli += sec*1000
016: milli += minute*1000*60
017: milli += hour*1000*60*60
018: return milli
019:
020: def fill_zero(number,digit):
021: ans = ""
022: if len(number) < digit :
023: ans = "0"*(digit-len(number)) + number
024: return ans
025: else :
026: return str(number)
027:
028: def milli_to_time(milli):
029: milli = int(milli)
030: hour = milli//(1000*60*60)
031: milli = milli-(hour*1000*60*60)
032: minute = milli//(1000*60)
033: milli = milli-(minute*1000*60)
034: sec = milli//(1000)
035: milli = milli-(sec*1000)
036: ans = fill_zero(str(hour),2) + ":" + fill_zero(str(minute),2) + ":" + fill_zero(str(sec),2) + "," + fill_zero(str(milli),3)
037: return ans
038:
039: def new_line(sent):
040: if len(sent) != 0 :
041: if sent[-1] != "\n" :
042: return sent+"\n"
043: return sent
044:
045: def isallnum(strr):
046: for e in strr :
047: if e.isalnum() :
048: return True
049: return False
050:
051: def cleaned_script(script):
052: cleaned_script = ""
053: index_start = []
054: index_stop = []
055: for i in range(len(script)) :
056: if script[i] in "([{<" :
057: index_start.append(i)
058: elif script[i] in ")}]>" :
059: index_stop.append(i)
060: index_start.append(len(script))
061: index_stop = [-1]+index_stop
062: for i in range(len(index_start)) :
063: cleaned_script += script[index_stop[i]+1:index_start[i]]
064: return cleaned_script
065: # ------------------------------------------
066: #main function
067:
068: def shift(file_in, time_shift, file_out):
069: fin = open(file_in , encoding='utf-8')
070: fout = open(file_out, 'w', encoding='utf-8')
071: script_sequence = 1
072: line = fin.readline()
073: while len(line) != 0 :
074: if "-->" in line :
075: start,stop = start_stop_time(line)
076: start = time_to_milli(start) + time_shift
077: stop = time_to_milli(stop) + time_shift
078: script = ""
079: line = fin.readline()
080: while len(line.strip()) != 0 :
081: script += line
082: line = fin.readline()
083:
084: if start <= 0 and stop <= 0 :
085: pass
086: else :
087: if start >= 0 and stop >= 0 :
088: pass
089: if start < 0 and stop >= 0 :
090: start = 0
091:
092: fout.write(str(script_sequence))
093: fout.write("\n")
094: fout.write(milli_to_time(start) + " --> " + milli_to_time(stop))
095: fout.write("\n")
096: fout.write(script)
097: fout.write("\n")
098: script_sequence += 1
099: start = ""
100: stop = ""
101:
102:
103: line = fin.readline()
104: fin.close()
105: fout.close()
106:
107: def merge(base_file, merge_file, threshold, file_out):
108: base = open(base_file , encoding='utf-8')
109: merge = open(merge_file , encoding='utf-8')
110: fout = open(file_out, 'w', encoding='utf-8')
111:
112: #[milli_start,(str(timestart --> timestop)),script]
113: base_list = []
114: merge_list = []
115:
116: #เอา base_file เข้า base_list
117: base_line = base.readline()
118: while len(base_line) != 0 :
119: if "-->" in base_line :
120: l = []
121: basestart,basestop = start_stop_time(base_line)
122: base_start = time_to_milli(basestart)
123: l.append(base_start)
124: l.append(base_line)
125: script = ''
126: base_line = base.readline()
127: while len(base_line.strip()) != 0 :
128: script += base_line
129: base_line = base.readline()
130: l.append(new_line(script))
131: base_list.append(l)
132: else :
133: base_line = base.readline()
134:
135: #เอา merge_file เข้า merge_list
136: merge_line = merge.readline()
137: while len(merge_line) != 0 :
138: if "-->" in merge_line :
139: l = []
140: mergestart,mergestop = start_stop_time(merge_line)
141: merge_start = time_to_milli(mergestart)
142: l.append(merge_start)
143: l.append(merge_line)
144: script = ''
145: merge_line = merge.readline()
146: while len(merge_line.strip()) != 0 :
147: script += merge_line
148: merge_line = merge.readline()
149: l.append(new_line(script))
150: merge_list.append(l)
151: else :
152: merge_line = merge.readline()
153:
154: #เอาที่อยู่ในช่วง threshold มารวมกัน
155: delete_list = []
156: for e in merge_list :
157: minn = threshold
158: found = False
159: for i in range(len(base_list)) :
160: if abs(e[0]-base_list[i][0]) < minn :
161: minn = abs(e[0]-base_list[i][0])
162: merge_index = i
163: found = True
164: if found :
165: base_list[merge_index][2] += e[2]
166: delete_list.append(e)
167: for e in delete_list :
168: merge_list.remove(e)
169:
170: # รวมเตรียม write
171: write_list = base_list + merge_list
172: write_list.sort()
173:
174: # write
175: for i in range(len(write_list)):
176: fout.write(new_line(str(i+1)))
177: fout.write(new_line(write_list[i][1]))
178: fout.write(new_line(write_list[i][2]))
179: fout.write("\n")
180:
181: base.close()
182: merge.close()
183: fout.close()
184:
185: def clean(file_in, file_out):
186: fin = open(file_in , encoding='utf-8')
187: fout = open(file_out, 'w', encoding='utf-8')
188: script_sequence = 1
189: fin_line = fin.readline()
190: while len(fin_line) != 0 :
191: if "-->" in fin_line :
192: time_line = fin_line
193: fin_line = fin.readline()
194: script = ""
195: while len(fin_line.strip()) != 0 :
196: script += fin_line
197: fin_line = fin.readline()
198:
199: #เอาวงเล็บออก
200: cleanedd_script = cleaned_script(script)
201:
202: if isallnum(cleanedd_script) : #เช็คว่ายังมีตัวหนังสือหรือตัวเลขอยู่ไหม ถ้ามีก็ write
203: fout.write(new_line(str(script_sequence)))
204: script_sequence += 1
205: fout.write(new_line(time_line))
206: fout.write(new_line(cleanedd_script))
207: fout.write("\n")
208: else :
209: pass
210: fin_line = fin.readline()
211:
212: fin.close()
213: fout.close()
214: # ------------------------------------------
6430098021 (80.0%)
shift -> 90.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
1
00:00:00,000 --> 00:00:00,000
AAAA AAAA AAA.
2
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
3
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
4
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 70.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555 |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430098021
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: file_list = read_file(file_in)
007:
008: for i in file_list:
009: i[1] += time_shift
010: if i[1] < 0:
011: i[1] = 0
012: i[2] += time_shift
013: if i[2] < 0:
014: i[2] = 0
015:
016: output(file_list,file_out)
017:
018: # ------------------------------------------
019: def merge(base_file, merge_file, threshold, file_out):
020: base_list = read_file(base_file)
021: merge_list = read_file(merge_file)
022:
023: merged = []
024: stack = False
025: len_merge = len(merge_list)
026: len_base = len(base_list)
027: while len_base != 0 and len_merge != 0:
028: if mergeyesorno(base_list[0],merge_list[0],threshold) == True:
029: stack = True
030: base_list[0].append(merge_list[0][3])
031: merge_list.pop(0)
032: len_merge = len(merge_list)
033: else:
034: if stack == True:
035: stack = False
036: merged.append(base_list[0])
037: base_list.pop(0)
038: len_base = len(base_list)
039:
040: elif base_list[0][1] <= merge_list[0][1]:
041: merged.append(base_list[0])
042: base_list.pop(0)
043: len_base = len(base_list)
044:
045: elif merge_list[0][1] < base_list[0][1]:
046: merged.append(merge_list[0])
047: merge_list.pop(0)
048: len_merge = len(merge_list)
049:
050: if len_merge == 0 and len_base > 0:
051: for i in base_list:
052: merged.append(i)
053: elif len_base == 0 and len_merge > 0:
054: for i in merge_list:
055: merged.append(i)
056:
057: output(merged,file_out)
058:
059: # ------------------------------------------
060: def clean(file_in, file_out):
061: file_list = read_file(file_in)
062:
063: for i in file_list:
064: STR = ""
065: for j in i[3]:
066: if j in "[]{}()<>":
067: STR += "{}"
068: else:
069: STR += j
070: List = STR.split("{}")
071: i[3] = "".join(List[::2]).strip()
072:
073: check = False
074: for i in file_list:
075: for j in i[3]:
076: check = j.isalnum()
077: if check == True:
078: break
079: if check == False:
080: i[3] = ""
081:
082: c = 0
083: l = len(file_list)
084: while c < l:
085: if file_list[c][3] == "":
086: file_list.pop(c)
087: else:
088: c += 1
089: l = len(file_list)
090:
091: output(file_list,file_out)
092: return
093:
094: # ------------------------------------------
095:
096: def turn_to_millisec(x):
097: x = x.strip()
098: S = ""
099: for i in x:
100: if i != ":" and i != ",":
101: S += i
102: else :
103: S += " "
104: S1 = [int(e) for e in S.split()]
105: millisec = S1[0]*60*60*1000 + S1[1]*60*1000 + S1[2]*1000 + S1[3]
106: return millisec
107:
108: def turn_millisec_to_display(x):
109: HH = x // (3600 * 1000)
110: MM = (x - (3600 * 1000 * HH)) // (60 * 1000)
111: SS = (x - (3600 * 1000 * HH + 60 * 1000 * MM)) // 1000
112: MS = (x - (3600 * 1000 * HH + 60 * 1000 * MM + 1000 * SS))
113: dis = ["0" * (2 - len(str(HH))) + str(HH), "0" * (2 - len(str(MM))) + str(MM), "0" * (2 - len(str(SS))) + str(SS),"0" * (3 - len(str(MS))) + str(MS)]
114: return ":".join(dis[:3:]) + "," + dis[3]
115:
116: def read_file(x):
117: input = open(x,"r")
118: STR1 = ""
119: for i in input:
120: STR1 += i
121:
122: LIST1 = STR1.strip().split("\n\n")
123: LIST2 = []
124: for i in LIST1:
125: sp = ""
126: c2 = 0
127: for j in i:
128: if j != "\n":
129: sp += j
130: elif j == "\n":
131: if c2 < 2:
132: sp += "{}"
133: c2 += 1
134: else:
135: sp += j
136: SP = sp.split("{}")
137: start,end = [e for e in SP[1].split(" --> ")]
138: LIST2.append([SP[0] , turn_to_millisec(start) , turn_to_millisec(end) , SP[2]])
139: input.close()
140: return LIST2
141:
142: def output(x,file_out):
143: STR = ""
144: c = 1
145: for i in x:
146: for j in range(len(i)):
147: if j == 0:
148: STR += str(c) + "\n"
149: c += 1
150: elif j == 1:
151: STR += turn_millisec_to_display(i[j]) + " --> "
152: elif j == 2:
153: STR += turn_millisec_to_display(i[j]) + "\n"
154: else:
155: STR += str(i[j]) + "\n"
156: STR += "\n"
157:
158: out = open(file_out,"w")
159: out.write(STR)
160: out.close()
161:
162: def mergeyesorno(base,merge,threshold):
163: if abs(base[1] - merge[1]) <= threshold:
164: return True
165: else :
166: return False
6430100621 (46.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 38.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
[BBBBB]
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
(BBBB)
BBB
3
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪[11
1] |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 294, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 163, in merge
h1=sublist1[i][1][0:2]
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 294, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 163, in merge
h1=sublist1[i][1][0:2]
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430100621
003: import math
004: #--------------------------------------------
005: def changetime(time,time_shift) :
006: if time_shift >= 0 :
007: time[0][3] = time[0][3] + int(time_shift)
008: if time[0][3] >= 1000 :
009: rsec=time[0][3]//1000
010: time[0][3] = time[0][3]%1000
011: time[0][2] = time[0][2]+rsec
012: if time[0][2] >= 60 :
013: rmin=time[0][2]//60
014: time[0][2] = time[0][2]%60
015: time[0][1] = time[0][1]+rmin
016: if time[0][1] >= 60 :
017: rhrs=time[0][1]//60
018: time[0][1] = time[0][1]%60
019: time[0][0] = time[0][0]+rhrs
020:
021: time[1][3] = time[1][3] + int(time_shift)
022: if time[1][3] >= 1000 :
023: rsec=time[1][3]//1000
024: time[1][3] = time[1][3]%1000
025: time[1][2] = time[1][2]+rsec
026: if time[1][2] >= 60 :
027: rmin=time[1][2]//60
028: time[1][2] = time[1][2]%60
029: time[1][1] = time[1][1]+rmin
030: if time[1][1] >= 60 :
031: rhrs=time[0][1]//60
032: time[1][1] = time[1][1]%60
033: time[1][0] = time[1][0]+rhrs
034:
035: else :
036: time[0][3] = time[0][3] + int(time_shift)
037: if time[0][3] < 0 :
038: rsec=time[0][3]//1000
039: time[0][3] = time[0][3]%1000
040: time[0][2] = time[0][2]+rsec
041: if time[0][2] < 0 :
042: rmin=time[0][2]//60
043: time[0][2] = time[0][2]%60
044: time[0][1] = time[0][1]+rmin
045: if time[0][1] < 0 :
046: rhrs=time[0][1]//60
047: time[0][1] = time[0][1]%60
048: time[0][0] = time[0][0]+rhrs
049:
050: time[1][3] = time[1][3] + int(time_shift)
051: if time[1][3] < 0 :
052: rsec=time[1][3]//1000
053: time[1][3] = time[1][3]%1000
054: time[1][2] = time[1][2]+rsec
055: if time[1][2] < 0 :
056: rmin=time[1][2]//60
057: time[1][2] = time[1][2]%60
058: time[1][1] = time[1][1]+rmin
059: if time[1][1] < 0 :
060: rhrs=time[1][1]//60
061: time[1][1] = time[1][1]%60
062: time[1][0] = time[1][0]+rhrs
063: #แก้ลบ
064: if time[0][0] < 0 :
065: time[0][0] = 0
066: time[0][1] = 0
067: time[0][2] = 0
068: time[0][3] = 0
069: if time[1][0] < 0 :
070: time[1][0] = 0
071: time[1][1] = 0
072: time[1][2] = 0
073: time[1][3] = 0
074:
075: time1=str(time[0][0]).zfill(2)+':'+str(time[0][1]).zfill(2)+':'+str(time[0][2]).zfill(2)+','+str(time[0][3]).zfill(3)
076: time2=str(time[1][0]).zfill(2)+':'+str(time[1][1]).zfill(2)+':'+str(time[1][2]).zfill(2)+','+str(time[1][3]).zfill(3)
077: out=time1+' --> '+time2
078: if time[0][0] == 0 and time[0][1] == 0 and time[0][2] == 0 and time[0][3] == 0 \
079: and time[1][0] == 0 and time[1][1] == 0 and time[1][2] == 0 and time[1][3] == 0 :
080: out = 'delete this'
081: return out
082: #--------------------------------------------
083: def trandata(file_in,time_shift):
084: fin = open(file_in,'r', encoding='utf-8')
085: line=fin.readline()
086: i=0
087: sublist=[]
088: subtem=['']
089:
090: while len(line) > 0 :
091: if line[:-1].isnumeric() :
092: del subtem[-1]
093: sublist.append(subtem)
094: subtem=[]
095: subtem.append(int(line))
096: line=fin.readline()
097:
098: elif len(line) == 30 and '-->' in line :
099: h1=line[0:2]
100: m1=line[3:5]
101: s1=line[6:8]
102: ms1=line[9:12]
103: h2=line[17:19]
104: m2=line[20:22]
105: s2=line[23:25]
106: ms2=line[26:29]
107: timelist=[[int(h1),int(m1),int(s1),int(ms1)],[int(h2),int(m2),int(s2),int(ms2)]]
108: subtem.append(changetime(timelist,time_shift)) ###############
109: line=fin.readline()
110: else :
111: subtem.append(line[:-1])
112: line=fin.readline()
113: if len(line) <=0 :
114: sublist.append(subtem)
115: del sublist[0]
116: fin.close()
117:
118: return sublist
119: #--------------------------------------------
120: def writes(sublist,file_out) :
121: fout=open(file_out,'w+',encoding='utf-8')
122: for i in range (0,len(sublist)):
123: for j in range (0,len(sublist[i])):
124: k=str(sublist[i][j])+'\n'
125: fout.write(k)
126: fout.write('\n')
127: fout.close()
128: # ------------------------------------------
129: def shift(file_in, time_shift, file_out):
130: ###shift('C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\SRT_Files\\test3_en.srt',0,'C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\testfix4.txt') ##########
131: sublist = trandata(file_in,time_shift)
132: count=0
133: newsub=sublist
134: for i in range (0,len(sublist)) :
135: if sublist[i][1] == 'delete this' :
136: count+=1
137: count1=count+1
138: for i in range(0,count) :
139: del sublist[0]
140: for i in range (0,len(newsub)) :
141: newsub[i][0] -= count
142: writes(newsub,file_out)
143: return
144: # ------------------------------------------
145: def merge(base_file, merge_file, threshold, file_out):
146: ####merge('C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\SRT_Files\\test3_en.srt','C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\SRT_Files\\test3_th.srt',1000,'C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\testfix4.txt')
147: sublist1 = trandata(base_file,0)
148: sublist2 = trandata(merge_file,0)
149: k=[]
150: for i in range (0,len(sublist1)) :
151: k.append(sublist1[i])
152: if len(sublist1[i]) == 4 :
153: sublist1[i][2] +=('\n'+sublist1[i][3])
154: del sublist1[i][3]
155: m=[]
156: for i in range (0,len(sublist2)) :
157: m.append(sublist2[i])
158: if len(sublist2[i]) == 4 :
159: sublist2[i][2] +=('\n'+sublist2[i][3])
160: del sublist2[i][3]
161:
162: for i in range(0,len(sublist1)) :
163: h1=sublist1[i][1][0:2]
164: m1=sublist1[i][1][3:5]
165: s1=sublist1[i][1][6:8]
166: ms1=sublist1[i][1][9:12]
167: starttime = (((int(h1)*3600)+(int(m1)*60)+int(s1))*1000)+int(ms1)
168: sublist1[i].insert(0,starttime)
169: for i in range(0,len(sublist2)) :
170: h1=sublist2[i][1][0:2]
171: m1=sublist2[i][1][3:5]
172: s1=sublist2[i][1][6:8]
173: ms1=sublist2[i][1][9:12]
174: starttime = ((int(h1)*3600+int(m1)*60+int(s1))*1000)+int(ms1)
175: sublist2[i].insert(0,starttime)
176:
177: totallist=[]
178: sublist2.append([-999999999])
179: for i in range(0,len(sublist1)) :
180: for j in range (0,len(sublist2)) : ####################################
181: if math.fabs(int(sublist1[i][0])-int(sublist2[j][0])) <= int(threshold) :
182: totallist.append([sublist1[i],sublist2[j]])
183: del sublist2[j]
184: break
185: if j == len(sublist2)-1 :
186: totallist.append(sublist1[i])######
187: for i in range (0,len(sublist2)) :
188: totallist.append(sublist2[i])
189:
190: del totallist[-1]
191:
192: ftotal=[]
193: for i in range (0,len(totallist)) :
194: if len(totallist[i]) == 2 :
195: tem=[]
196: tem.extend(totallist[i][0])
197: tem.append(totallist[i][1][3])
198: ftotal.append(tem)
199: else :
200: ftotal.append(totallist[i])
201:
202: ####เรียง ตัดเลข เรียงเลข
203: count=1
204: ftotal.sort()
205: for i in range (0,len(ftotal)) :
206: del ftotal[i][0]
207: ftotal[i][0] = count
208: count+=1
209: writes(ftotal,file_out)
210: return
211:
212: # ------------------------------------------
213: def clean(file_in, file_out):
214: #######clean('C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\SRT_Files\\test3_en.srt','C:\\Users\\Napat\\Documents\\ปี1\\COM PROG\\HW8\\texfix4.txt')
215: sublist = trandata(file_in,0)
216: k=[]
217: for i in range (0,len(sublist)) :
218: k.append(sublist[i])
219: if len(sublist[i]) == 4 :
220: sublist[i][2] +=('\n'+sublist[i][3])
221: del sublist[i][3]
222:
223: for i in range (0,len(sublist)) :
224: for j in range (4) :
225: firstnum = -1
226: secondnum = -1
227: firstnum=sublist[i][2].find('(')
228: secondnum=sublist[i][2].find(')')
229: if firstnum == -1 and secondnum == -1 :
230: firstnum=sublist[i][2].find('[')
231: secondnum=sublist[i][2].find(']')
232: if firstnum == -1 and secondnum == -1 :
233: firstnum=sublist[i][2].find('{')
234: secondnum=sublist[i][2].find('}')
235: if firstnum == -1 and secondnum == -1 :
236: firstnum=sublist[i][2].find('<')
237: secondnum=sublist[i][2].find('>')
238: if firstnum != -1 and secondnum != -1 :
239: sublist[i][2] = sublist[i][2][0:firstnum]+sublist[i][2][secondnum+1:len(sublist[i][2])]
240: #### หา plaintext
241: plaintext=''
242: for k in range (0,len(sublist[i][2])) :
243: if sublist[i][2][k] in "!@#$%^&*_+-=/\\\',.\"?<>[]{}()\:\;♪ " :
244: plaintext+=''
245: else :
246: plaintext+=sublist[i][2][k]
247: plaintext=plaintext.replace('\n','')
248: if plaintext.isalnum() == False :
249: sublist[i][2] = 'delete this'
250: sublist.sort()
251: newsub=[]
252: count=1
253: for i in range (0,len(sublist)) :
254: if sublist[i][2] != 'delete this' :
255: newsub.append(sublist[i])
256: for i in range (0,len(newsub)) :
257: newsub[i][0] = count
258: count+=1
259: writes(newsub,file_out)
260: return
261: # ------------------------------------------
6430117321 (66.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 0%
Traceback (most recent call last):
File "", line 268, in __test_merge_
return __test_(merge, cases)
NameError: name 'merge' is not defined
|
001: # Subtitle Tools
002: # Your ID : 6430117321
003: def shift(file_in, time_shift, file_out):
004: fin = open(file_in, "r",encoding = 'utf-8')
005: lst=[]
006: n=0
007: a =[]
008: for line in fin:
009: x=line.strip()
010: if x!="":
011: a.append(x)
012: else:
013: lst.append(a)
014: a=[]
015: for i in lst:
016: time=i[1] ; t1,t2=time.split(" --> ") ; t1_lst,t2_lst = t1.split(":"),t2.split(":") ; sec1,sec2 = t1_lst[2].split(","),t2_lst[2].split(",")
017: millisec1,millisec2= int(sec1[0]+sec1[1])+(int(t1_lst[1])*60*1000)+(int(t1_lst[0])*60*60*1000) , int(sec2[0]+sec2[1])+(int(t2_lst[1])*60*1000)+(int(t2_lst[0])*60*60*1000)
018: diff_first = millisec1+time_shift ; diff_second= millisec2+time_shift
019: hr_1, hr_2 = (diff_first//(60*60*1000)),(diff_second//(60*60*1000)) ; min_1,min_2 = (diff_first%(60*60*1000))//(60*1000),(diff_second%(60*60*1000))//(60*1000) ;sec_1,sec_2 =(diff_first%(60*60*1000))%(60*1000),(diff_second%(60*60*1000))%(60*1000)
020: if diff_second<=0:
021: i[1]="no NEED"
022: else:
023: if diff_first<=0:
024: if len(str(sec_2))<4:
025: t2_lst[2]='00'+','+(str(sec_2)[0:len(str(sec_2))])
026: else:
027: if len(str(sec_2))<5:
028: t2_lst[2]='0'+(str(sec_2)[0])+','+(str(sec_2)[1:len(str(sec_2))])
029: else:
030: t2_lst[2]=(str(sec_2))[0:2]+','+(str(sec_2))[2:len(str(sec_2))]
031: if len(str(min_2))<2:
032: t2_lst[1]='0'+(str(min_2)[0])
033: else:
034: t2_lst[1]=(str(min_2))
035: if len(str(hr_2))<2:
036: t2_lst[0]='0'+(str(hr_2)[0])
037: else:
038: t2_lst[0]=(str(hr_2))
039: i[1]="00:00:00,000 --> "+":".join(t2_lst)
040: else:
041: #1
042: if len(str(sec_1))<4:
043: t1_lst[2]='00'+','+(str(sec_1)[0:len(str(sec_1))])
044: else:
045: if len(str(sec_1))<5:
046: t1_lst[2]='0'+(str(sec_1)[0])+','+(str(sec_1)[1:len(str(sec_1))])
047: else:
048: t1_lst[2]=(str(sec_1))[0:2]+','+(str(sec_1))[2:len(str(sec_1))]
049: if len(str(min_1))<2:
050: t1_lst[1]='0'+(str(min_1)[0])
051: else:
052: t1_lst[1]=(str(min_1))
053: if len(str(hr_1))<2:
054: t1_lst[0]='0'+(str(hr_1)[0])
055: else:
056: t1_lst[0]=(str(hr_1))
057: #2
058: if len(str(sec_2))<4:
059: t2_lst[2]='00'+','+(str(sec_2)[0:len(str(sec_2))])
060: else:
061: if len(str(sec_2))<5:
062: t2_lst[2]='0'+(str(sec_2)[0])+','+(str(sec_2)[1:len(str(sec_2))])
063: else:
064: t2_lst[2]=(str(sec_2))[0:2]+','+(str(sec_2))[2:len(str(sec_2))]
065: if len(str(min_2))<2:
066: t2_lst[1]='0'+(str(min_2)[0])
067: else:
068: t2_lst[1]=(str(min_2))
069: if len(str(hr_2))<2:
070: t2_lst[0]='0'+(str(hr_2)[0])
071: else:
072: t2_lst[0]=(str(hr_2))
073: i[1]= ":".join(t1_lst)+" --> "+":".join(t2_lst)
074: n=1
075: change=[]
076: for i in lst:
077: if 'no NEED' in i:
078: pass
079: else:
080: i[0]=str(n)
081: change.append(i)
082: n+=1
083: fout = open(file_out , 'w', encoding='utf-8')
084: for i in change:
085: for each_line in i:
086: fout.write(each_line)
087: fout.write("\n")
088: fout.write("\n")
089: fout.close()
090: return
091:
092: # ------------------------------------------
093: #def merge(base_file, merge_file, threshold, file_out):
094:
095: # return
096:
097: # ------------------------------------------
098: def remov_bra(snt):
099: no_bra = ''
100: skip1 = True ; skip2 = True ; skip3 = True ; skip4 = True
101: for i in snt:
102: if i == '[':
103: skip1 = False
104: elif i == '(':
105: skip2 = False
106: elif i == '{':
107: skip3 = False
108: elif i == '<':
109: skip4 = False
110: elif i == ']' and skip1 == False:
111: skip1 = True
112: elif i == ')'and skip2 == False:
113: skip2 = True
114: elif i == '}' and skip3 == False:
115: skip3 = True
116: elif i == '>' and skip4 == False:
117: skip4 = True
118: elif skip1 == True and skip2 == True and skip3==True and skip4==True:
119: no_bra += i
120: return no_bra
121: def clean(file_in, file_out):
122: y=[]
123: fin = open(file_in, "r",encoding = 'utf-8')
124: fout = open(file_out , 'w', encoding='utf-8')
125: a =[]
126: lst=[]
127: for line in fin:
128: if line.strip()!="":
129: for i in line.strip():
130: if i.isalnum() == True:
131: a.append(line.strip())
132: break
133: else:
134: if a==[]:#\n
135: pass
136: else:
137: lst.append(a)
138: a=[]
139: lst_new_=[]
140: for i in lst:
141: if len(i)>1:
142: if '-->' in i[1]:
143: lst_new_.append(i)
144: else:
145: lst_new_[-1]+=i
146: if len(i)==1:
147: lst_new_[-1]+=i
148: # print(lst_new_)
149: w=[]
150: lst_no_brack=[]
151: for i in lst_new_:
152: r=''
153: x=0
154: for n in i:
155: x+=1
156: if n==i[0] or n==i[1]:
157: w.append(n)
158: else:
159: r+=n+'@'
160: if x==len(i):
161: n=r[:-1]
162: k=[]
163: for i in remov_bra(n).split('@'):
164: if i!='':
165: k.append(i)
166: if k!=[]:
167: w+=k
168: lst_no_brack.append(w)
169: w=[]
170: # print(lst_no_brack)
171: count=1
172: lst_newest =[]
173: a=[]
174: lst=[]
175: for i in lst_no_brack:
176: for n in i:
177: for k in n:
178: if k.isalnum() == True:
179: a.append(n.strip())
180: break
181: lst.append(a)
182: a=[]
183: # print(lst)
184: for i in lst:
185: if len(i)>=3:
186: i[0]=str(count)
187: lst_newest.append(i)
188: count+=1
189: # print(lst_newest)
190: for i in lst_newest:
191: for each_line in i:
192: fout.write(each_line.strip())
193: fout.write("\n")
194: fout.write("\n")
195: fout.close()
196: return
197: #clean('test2_th.srt', 'test_result')
198:
199: # ------------------------------------------
6430118021 (70.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 60.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123 AA♪
A♪
2
01:03:45,100 --> 01:03:48,100
A♪1 |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 50.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:50,000 --> 00:01:56,000
111
YYY
AAA
3
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
4
00:01:56,997 --> 00:01:56,998
333
444
555
5
00:01:57,480 --> 00:02:22,100
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID
003: # 6430118021
004: #-------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: aa = open(file_in, encoding='utf-8')
007: bb = open(file_out, 'w', encoding='utf-8')
008: xx = 0 ; round_101 = 1 ; colum = 0
009: for line in aa:
010: colum += 1
011: if colum == 2 :
012: t1,t2 = line[0:-1:1].split("-->")
013: t1n = milisec_101(t1) + int(time_shift)
014: t2n = milisec_101(t2) + int(time_shift)
015: if t1n > 0 and t2n > 0 :
016: bb.write(str(round_101)+"\n")
017: bb.write(comeback_101(t1n)+" --> "+comeback_101(t2n)+"\n")
018: round_101 += 1
019: xx = 1
020: elif t1n < 0 and t2n > 0 :
021: bb.write(str(round_101)+"\n")
022: bb.write("00:00:00,000"+" --> "+comeback_101(t2n)+"\n")
023: round_101 += 1
024: xx = 1
025: else :
026: colum = -100
027: elif line == "\n":
028: if colum >= -97 :
029: bb.write(line)
030: colum = 0
031: xx = 0
032: elif xx == 1 :
033: bb.write(line)
034:
035: aa.close()
036: bb.close()
037: # ------------------------------------------
038: def merge(base_file, merge_file, threshold, file_out):
039: fbase = open(base_file , encoding='utf-8')
040: fmerge = open(merge_file , encoding='utf-8')
041: fout = open(file_out, 'w', encoding='utf-8')
042: database_101 = {} ; datamerge_101 = {} ; dataall_101 = {}
043: base_time,base_text,fulltime1 = split_101(fbase)
044: merge_time,merge_text,fulltime2 = split_101(fmerge)
045: for i in range(len(base_time)):
046: database_101[base_time[i]] = base_text[i]
047: dataall_101[base_time[i]] = base_text[i]
048: for i in range(len(merge_time)):
049: dataall_101[merge_time[i]] = merge_text[i]
050: datamerge_101[merge_time[i]] = merge_text[i]
051: mix_time = base_time + merge_time
052: mix_time.sort()
053: mix_timeplus = [] ; yyy = 0 ; near = 0
054: mix_time.append(0)
055: for i in range(0,len(mix_time)-1,1):
056: near += 1
057: if near == 1 :
058: if abs(mix_time[i]-mix_time[i+1]) <= threshold:
059: if abs(mix_time[i+1]-mix_time[i+2]) <= threshold:
060: mix_timeplus.append([mix_time[i],mix_time[i+1],\
061: mix_time[i+2]])
062: near = -2
063: else :
064: mix_timeplus.append([mix_time[i],mix_time[i+1]])
065: near = -1
066: else :
067: mix_timeplus.append([mix_time[i]])
068: near = 0
069: round_102 = 1 ; qqq = 0
070: for i in range(len(mix_timeplus)) :
071: if len(mix_timeplus[i]) == 1 :
072: if mix_timeplus[i][0] in database_101 :
073: fout.write(str(round_102)+"\n")
074: fout.write(fulltime1[mix_timeplus[i][0]]) #
075: fout.write(database_101[mix_timeplus[i][0]])
076: round_102 += 1
077: qqq = 1
078: elif mix_timeplus[i][0] in datamerge_101 :
079: fout.write(str(round_102)+"\n")
080: fout.write(fulltime2[mix_timeplus[i][0]]) #
081: fout.write(datamerge_101[mix_timeplus[i][0]])
082: round_102 += 1
083: qqq = 1
084: if qqq == 1 :
085: fout.write("\n")
086: qqq = 0
087: elif len(mix_timeplus[i]) == 2 :
088: if mix_timeplus[i][0] in database_101 :
089: fout.write(str(round_102)+"\n")
090: fout.write(fulltime1[mix_timeplus[i][0]]) #
091: fout.write(database_101[mix_timeplus[i][0]])
092: fout.write(datamerge_101[mix_timeplus[i][1]])
093: round_102 += 1
094: qqq = 1
095: elif mix_timeplus[i][0] in datamerge_101 :
096: fout.write(str(round_102)+"\n")
097: fout.write(fulltime1[mix_timeplus[i][1]]) #
098: fout.write(database_101[mix_timeplus[i][1]])
099: fout.write(datamerge_101[mix_timeplus[i][0]])
100: round_102 += 1
101: qqq = 1
102: if qqq == 1 :
103: fout.write("\n")
104: qqq = 0
105: elif len(mix_timeplus[i]) == 3 :
106: if mix_timeplus[i][0] in database_101 :
107: fout.write(str(round_102)+"\n")
108: fout.write(fulltime1[mix_timeplus[i][0]])
109: fout.write(database_101[mix_timeplus[i][0]])
110: fout.write(dataall_101[mix_timeplus[i][1]])
111: fout.write(dataall_101[mix_timeplus[i][2]])
112: round_102 += 1
113: qqq = 1
114: elif mix_timeplus[i][1] in database_101 :
115: fout.write(str(round_102)+"\n")
116: fout.write(fulltime1[mix_timeplus[i][1]])
117: fout.write(database_101[mix_timeplus[i][1]])
118: fout.write(dataall_101[mix_timeplus[i][0]])
119: fout.write(dataall_101[mix_timeplus[i][2]])
120: round_102 += 1
121: qqq = 1
122: elif mix_timeplus[i][2] in database_101 :
123: fout.write(str(round_102)+"\n")
124: fout.write(fulltime1[mix_timeplus[i][2]])
125: fout.write(database_101[mix_timeplus[i][2]])
126: fout.write(dataall_101[mix_timeplus[i][0]])
127: fout.write(dataall_101[mix_timeplus[i][1]])
128: round_102 += 1
129: qqq = 1
130: if qqq == 1 :
131: fout.write("\n")
132: qqq = 0
133: fbase.close()
134: fmerge.close()
135: fout.close()
136: # ------------------------------------------
137: def clean(file_in, file_out):
138: fin = open(file_in , encoding='utf-8')
139: fout = open(file_out , 'w', encoding='utf-8')
140: colum = 0 ; xx = [] ; round_101 = 1
141: lock = 0 ; yy = [] ; sp_lock = 0 ; kk = 0
142: for line in fin :
143: colum += 1
144: if colum == 3:
145: yy = findthem(line)
146: elif line == "\n":
147: colum = 0
148: xx = []
149: lock = 0
150: yy = []
151: elif lock == 1 :
152: yy = findthem(line)
153: fout.write(yy)
154: yy = []
155: elif colum == 2:
156: time = line
157: if len(yy) != 0 and yy != "\n":
158: for i in yy :
159: if i.isalnum() == True:
160: kk += 1
161: break
162: if kk > 0 :
163: if round_101 == 1 :
164: fout.write(str(round_101)+"\n")
165: fout.write(time)
166: fout.write(yy)
167: round_101 += 1
168: lock = 1
169: yy = []
170: kk = 0
171: else :
172: fout.write("\n")
173: fout.write(str(round_101)+"\n")
174: fout.write(time)
175: fout.write(yy)
176: round_101 += 1
177: lock = 1
178: yy = []
179: kk = 0
180: fin.close()
181: fout.close()
182: # ------------------------------------------
183: def milisec_101(pp) :
184: tt = 0
185: aa = pp.split(":")
186: bb,cc = aa[2].split(",")
187: tt += (int(aa[0]))*60*60*1000
188: tt += (int(aa[1]))*60*1000
189: tt += (int(bb))*1000
190: tt += int(cc)
191: return tt
192: # ------------------------------------------
193: def comeback_101(aa) :
194: bb = [0,0] ; dd = [0,0]
195: cc = [0,0]
196: cc[1] = more_2(aa%1000)
197: cc[0] = more_2((aa//1000)%60)
198: bb[1] = more_2((aa//(1000*60))%60)
199: bb[0] = more_2((aa//(1000*60*60))%60)
200: if len(cc[1]) == 2 :
201: cc[1] = "0" + cc[1]
202: dd[1] = ",".join(cc)
203: dd[0] = ":".join(bb)
204: ee = ":".join(dd)
205: return ee
206: # ------------------------------------------
207: def more_2(aa):
208: bb = ""
209: if len(str(aa)) == 1 :
210: bb += "0"+str(aa)
211: else :
212: bb += str(aa)
213: return bb
214: # ------------------------------------------
215: def split_101(aa):
216: time = [] ; text = []
217: x = 0 ; tt = "" ; colum = 0
218: fulltime = {}
219: for line in aa:
220: colum += 1
221: if colum == 2:
222: t1,t2 = line[0:-1:1].split("-->")
223: time.append(milisec_101(t1[0:-1:1]))
224: fulltime[milisec_101(t1[0:-1:1])] = line
225: x = 1
226: elif line == "\n" :
227: text.append(tt)
228: tt = ""
229: x = 0
230: colum = 0
231: elif x == 1 :
232: tt += line
233: return time,text,fulltime
234: # ------------------------------------------
235: def findthem(aa):
236: z = 1 ; xx = []
237: for i in aa:
238: if z > 0 :
239: if i in "()[]{}<>" :
240: z = z*(-1)
241: else :
242: xx.append(i)
243: elif i in "()[]{}<>" :
244: z = z*(-1)
245: yy = "".join(xx)
246: return yy
247: # ------------------------------------------
6430119621 (100.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430119621
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: fn = open(file_in, encoding='utf-8')
007: fn_shifted = open(file_out, "w", encoding='utf-8')
008: scene = []
009: fn_list_scene = []
010: for i in fn:
011: if i != "\n":
012: scene.append(i)
013: else:
014: scene.append(i)
015: fn_list_scene.append(scene)
016: scene = []
017:
018: n = 1
019: for list_scene in fn_list_scene:
020: time = list_scene[1].strip("\n")
021: time_remove = ""
022: for i in time:
023: if i in ":,":
024: time_remove += " "
025: else:
026: time_remove += i
027: a = time_remove.split(" --> ")
028: start = a[0].split() ; stop = a[1].split()
029: sum_start = int(start[3]) + int(start[2])*1000 + int(start[1])*60*1000 + int(start[0])*60*60*1000
030: sum_stop = int(stop[3]) + int(stop[2])*1000 + int(stop[1])*60*1000 + int(stop[0])*60*60*1000
031: sum_start_shifted = sum_start + time_shift
032: sum_stop_shifted = sum_stop + time_shift
033: if sum_stop_shifted > 0:
034: if sum_start_shifted < 0:
035: fn_shifted.write(str(n)+"\n")
036: sum_start_shifted = 0
037:
038: millisecond_start = str(sum_start_shifted % 1000)
039: if len(millisecond_start) != 3:
040: millisecond_start = "0"*(3-len(millisecond_start))+millisecond_start
041: second_start = str((sum_start_shifted // 1000) % 60)
042: if len(second_start) != 2:
043: second_start = "0"*(2-len(second_start))+second_start
044: minute_start = str((sum_start_shifted // 60000) % 60)
045: if len(minute_start) != 2:
046: minute_start = "0"*(2-len(minute_start))+minute_start
047: hour_start = str(sum_start_shifted // 3600000)
048: if len(hour_start) != 2:
049: hour_start = "0"*(2-len(hour_start))+hour_start
050:
051: millisecond_stop = str(sum_stop_shifted % 1000)
052: if len(millisecond_stop) != 3:
053: millisecond_stop = "0"*(3-len(millisecond_stop))+millisecond_stop
054: second_stop = str((sum_stop_shifted // 1000) % 60)
055: if len(second_stop) != 2:
056: second_stop = "0"*(2-len(second_stop))+second_stop
057: minute_stop = str((sum_stop_shifted // 60000) % 60)
058: if len(minute_stop) != 2:
059: minute_stop = "0"*(2-len(minute_stop))+minute_stop
060: hour_stop = str(sum_stop_shifted // 3600000)
061: if len(hour_stop) != 2:
062: hour_stop = "0"*(2-len(hour_stop))+hour_stop
063:
064: start_shifted = hour_start+":"+minute_start+":"+second_start+","+millisecond_start
065: stop_shifted = hour_stop+":"+minute_stop+":"+second_stop+","+millisecond_stop
066:
067: time_shifted = start_shifted+" --> "+stop_shifted+"\n"
068: fn_shifted.write(time_shifted)
069:
070: else:
071: fn_shifted.write(str(n)+"\n")
072:
073: millisecond_start = str(sum_start_shifted % 1000)
074: if len(millisecond_start) != 3:
075: millisecond_start = "0"*(3-len(millisecond_start))+millisecond_start
076: second_start = str((sum_start_shifted // 1000) % 60)
077: if len(second_start) != 2:
078: second_start = "0"*(2-len(second_start))+second_start
079: minute_start = str((sum_start_shifted // 60000) % 60)
080: if len(minute_start) != 2:
081: minute_start = "0"*(2-len(minute_start))+minute_start
082: hour_start = str(sum_start_shifted // 3600000)
083: if len(hour_start) != 2:
084: hour_start = "0"*(2-len(hour_start))+hour_start
085:
086: millisecond_stop = str(sum_stop_shifted % 1000)
087: if len(millisecond_stop) != 3:
088: millisecond_stop = "0"*(3-len(millisecond_stop))+millisecond_stop
089: second_stop = str((sum_stop_shifted // 1000) % 60)
090: if len(second_stop) != 2:
091: second_stop = "0"*(2-len(second_stop))+second_stop
092: minute_stop = str((sum_stop_shifted // 60000) % 60)
093: if len(minute_stop) != 2:
094: minute_stop = "0"*(2-len(minute_stop))+minute_stop
095: hour_stop = str(sum_stop_shifted // 3600000)
096: if len(hour_stop) != 2:
097: hour_stop = "0"*(2-len(hour_stop))+hour_stop
098:
099: start_shifted = hour_start+":"+minute_start+":"+second_start+","+millisecond_start
100: stop_shifted = hour_stop+":"+minute_stop+":"+second_stop+","+millisecond_stop
101:
102: time_shifted = start_shifted+" --> "+stop_shifted+"\n"
103: fn_shifted.write(time_shifted)
104:
105: for i in range(2,len(list_scene)):
106: fn_shifted.write(list_scene[i])
107: n += 1
108: fn.close()
109: fn_shifted.close()
110:
111: return
112:
113: # ------------------------------------------
114: def time_to_milli(time): #12:12:12,500
115: time_remove = ""
116: for i in time:
117: if i in ":,":
118: time_remove += " "
119: else:
120: time_remove += i #12 12 12 500
121: a = time_remove.split()
122: return int(a[3]) + int(a[2])*1000 + int(a[1])*60*1000 + int(a[0])*60*60*1000
123:
124: def milli_to_time(milli):
125: millisecond = str(milli % 1000)
126: if len(millisecond) != 3:
127: millisecond = "0"*(3-len(millisecond))+millisecond
128: second = str((milli // 1000) % 60)
129: if len(second) != 2:
130: second= "0"*(2-len(second))+second
131: minute = str((milli // 60000) % 60)
132: if len(minute) != 2:
133: minute = "0"*(2-len(minute))+minute
134: hour = str(milli // 3600000)
135: if len(hour) != 2:
136: hour = "0"*(2-len(hour))+hour
137: time = hour+":"+minute+":"+second+","+millisecond #12:12:12,500
138: # return time
139:
140: def make_scene_list(a): # a = แต่ละบรรทัด
141: s = []
142: fn_list_scene = []
143: for i in a:
144: if i != "\n":
145: s.append(i)
146: else:
147: s.append(i)
148: fn_list_scene.append(s)
149: s = []
150: return fn_list_scene
151:
152:
153:
154: def merge(base_file, merge_file, threshold, file_out):
155: fn_base = open(base_file, encoding='utf-8')
156: fn_merge = open(merge_file, encoding='utf-8')
157: fn_out = open(file_out, "w", encoding='utf-8')
158:
159: list_scene_sum = [] #รวม
160: #-----------------------------------------------
161: base_list = make_scene_list(fn_base)
162: remain_base = list(base_list)
163: time_base = []
164: for i in base_list:
165: time_base.append(time_to_milli(i[1].split(" --> ")[0]))
166: #-----------------------------------------------
167: merge_list = make_scene_list(fn_merge)
168: remain_merge = list(merge_list)
169: time_merge = []
170: for i in merge_list:
171: time_merge.append(time_to_milli(i[1].split(" --> ")[0]))
172: #-----------------------------------------------
173: for i in range(len(time_base)):
174: if i == len(time_base)-1:
175: target1 = time_base[i]
176: target2 = 0
177: text = []
178: for a in range(len(time_merge)):
179: if (abs(target1 - time_merge[a]) <= threshold) and (abs(target1 - time_merge[a]) < abs(target2 - time_merge[a])):
180: text += merge_list[a][2:-1]
181: for b in text:
182: remain_base[i].insert(-1, b)
183: text = []
184: merge_list.pop(a)
185: merge_list.insert(a,[])
186: remain_merge.pop(a)
187: remain_merge.insert(a,0)
188: else:
189: target1 = time_base[i]
190: target2 = time_base[i+1]
191: text = []
192: for a in range(len(time_merge)):
193: if (abs(target1 - time_merge[a]) <= threshold) and (abs(target1 - time_merge[a]) < abs(target2 - time_merge[a])):
194: text += merge_list[a][2:-1]
195: for b in text:
196: remain_base[i].insert(-1, b)
197: text = []
198: merge_list.pop(a)
199: merge_list.insert(a,[])
200: remain_merge.pop(a)
201: remain_merge.insert(a,0)
202:
203: remain = []
204: for i in remain_merge:
205: if i != 0 :
206: remain.append(i)
207: for i in remain_base:
208: i.pop(0)
209: for i in remain:
210: i.pop(0)
211:
212: list_scene_sum += remain_base
213: list_scene_sum += remain
214:
215: scene_sum = list(list_scene_sum)
216: scene_sum.sort()
217: n = 1
218: for a in scene_sum:
219: fn_out.write(str(n)+"\n")
220: for i in a:
221: fn_out.write(i)
222: n += 1
223:
224: fn_base.close()
225: fn_merge.close()
226: fn_out.close()
227: return
228: # ------------------------------------------
229: def remove_bracklet(a):
230: select = ""
231: skip = 0
232: for i in a:
233: if i in "[{<(":
234: skip += 1
235: elif i in ")>}]" and skip > 0:
236: skip -= 1
237: elif skip == 0:
238: select += i
239: return select
240:
241: def clean(file_in, file_out):
242: fn = open(file_in, encoding='utf-8')
243: fn_cleaned = open(file_out, "w", encoding='utf-8')
244: s = []
245: fn_list_scene = []
246: for i in fn:
247: if i != "\n":
248: s.append(i)
249: else:
250: s.append(i)
251: fn_list_scene.append(s)
252: s = []
253: list_scene_sum = []
254: for scene in fn_list_scene:
255: c_scene = list(scene)
256: list_scene = []
257: list_scene.append(c_scene[1])
258: text = c_scene[2:-1]
259: text = "@".join(text)
260: text_cleaned = remove_bracklet(text)
261: if text_cleaned != "":
262: if "@" not in text_cleaned:
263: for a in text_cleaned:
264: key = a.isalnum()
265: if key :
266: list_scene.append(text_cleaned)
267: break
268: elif "@" in text_cleaned:
269: list_text_cleaned = text_cleaned.split("@")
270: for i in list_text_cleaned:
271: for a in i:
272: key = a.isalnum()
273: if key :
274: list_scene.append(i)
275: break
276: if len(list_scene) > 1:
277: list_scene.append(c_scene[-1])
278: list_scene_sum.append(list_scene)
279: n = 1
280: for scene_sum in list_scene_sum:
281: fn_cleaned.write(str(n)+"\n")
282: for i in scene_sum:
283: fn_cleaned.write(i)
284: n += 1
285: fn.close()
286: fn_cleaned.close()
287:
288: return
289: # ------------------------------------------
6430130421 (46.67%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 60.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 305, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 193, in clean
P_ListBase[0].pop(0)
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
DDD
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430130421
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: fin = open(file_in , encoding='utf-8')
007: ListDataIn=[]
008: List=['',]
009: for line in fin:
010: if line == '\n':
011: ListDataIn.append(List)
012: List=[]
013: List.append(line)
014: fin.close()
015:
016: #Adjust time Shift
017: for i in range(len(ListDataIn)):
018: MinorList=ListDataIn[i]
019: NewTime=TimeShift(MinorList[2],time_shift)
020: ListDataIn[i][2]=NewTime
021:
022: #Delete Cell
023: for i in range(len(ListDataIn)):
024: if ListDataIn[i][2] =='X\n':
025: ListDataIn[i]='X'
026: for i in range(len(ListDataIn)):
027: if 'X' in ListDataIn:
028: ListDataIn.remove('X')
029:
030: #Adjust Number
031: for i in range(len(ListDataIn)):
032: ListDataIn[i][1]=str(i+1)+'\n'
033:
034: #Prepare for Print
035: ListDataIn[0].pop(0)
036: fout = open(file_out, 'w', encoding='utf-8')
037: for i in ListDataIn:
038: for j in i:
039: fout.write(j)
040: fout.close()
041: return
042:
043: # ------------------------------------------
044: def merge(base_file, merge_file, threshold, file_out):
045: fbase = open(base_file, encoding='utf-8')
046: ListBase=[]
047: List=['',]
048: for line in fbase:
049: if line == '\n':
050: ListBase.append(List)
051: List=[]
052: List.append(line)
053: fbase.close()
054:
055: fmerge= open(merge_file, encoding='utf-8')
056: ListMerge=[]
057: List=['',]
058: for line in fmerge:
059: if line == '\n':
060: ListMerge.append(List)
061: List=[]
062: List.append(line)
063: fmerge.close()
064:
065: #Change Time start in both to MiliSec
066: # ListMerge #ListBase
067: #Create list without pointer in and out
068: PsuedoListBase=[]
069: for i in range(len(ListBase)):
070: PsuedoListBase.append(list(ListBase[i]))
071:
072: for i in range(len(ListBase)) :
073: PsuedoListBase[i][2]=FindMili(ListBase[i][2])
074: #Create list without pointer in and out
075: PsuedoListMerge=[]
076: for i in range(len(ListMerge)):
077: PsuedoListMerge.append(list(ListMerge[i]))
078:
079: for i in range(len(ListMerge)) :
080: PsuedoListMerge[i][2]=FindMili(ListMerge[i][2])
081:
082: #MergeCell
083: # PsuedoListBase
084: # PsuedoListMerge
085: # P_PsuedoListMerge
086: P_PsuedoListMerge=list(PsuedoListMerge)
087: for i in range(len(PsuedoListBase)):
088: for j in range(len(PsuedoListMerge)):
089: if abs(int(PsuedoListBase[i][2]) - int(PsuedoListMerge[j][2])) <= threshold :
090: if PsuedoListMerge[j] in P_PsuedoListMerge:
091: P_PsuedoListMerge.remove(PsuedoListMerge[j])
092: for k in PsuedoListMerge[j][3::] :
093: PsuedoListBase[i].append(k)
094: #insertCell,dontMerge
095: for i in P_PsuedoListMerge :
096: PsuedoListBase.append(i)
097: for i in range(len(PsuedoListBase)):
098: PsuedoListBase[i][0]=''
099: PsuedoListBase[i][1]=''
100: PsuedoListBase.sort()
101:
102: NewList=[]
103: Finish=[]
104: for i in range(len(ListBase)):
105: NewList.append(list(ListBase[i]))
106: for i in range(len(ListMerge)):
107: NewList.append(list(ListMerge[i]))
108:
109: for i in range(len(PsuedoListBase)):
110: Found=False
111: for k in range(len(NewList)):
112: if Found==False:
113: if PsuedoListBase[i][3] in NewList[k]:
114: Found=True
115: Finish.append(list(PsuedoListBase[i]))
116: Finish[-1][2]=NewList[k][2]
117: for i in range(len(Finish)):
118: Finish[i][0]='\n'
119: Finish[i][1]=str(i+1)+'\n'
120:
121: Finish[0].pop(0)
122: fout = open(file_out, 'w', encoding='utf-8')
123: for i in Finish:
124: for j in i:
125: fout.write(j)
126: fout.close()
127: return
128: # ------------------------------------------
129: def clean(file_in, file_out):
130: fin = open(file_in, encoding='utf-8')
131: ListBase=[]
132: List=['',]
133: for line in fin:
134: if line == '\n':
135: ListBase.append(List)
136: List=[]
137: List.append(line)
138: fin.close()
139: stList=['(','[','{','<']
140: ndList=[')',']','}','>']
141: for z in range(5):
142: for i in range(len(ListBase)):
143: Foundst=False
144: st=[]
145: Foundnd=False
146: nd=[]
147: IndexBack=[]
148: for j in range(len(ListBase[i][3::])):
149: for k in ListBase[i][3::][j] :
150: if (Foundst==False) or (Foundnd==False):
151: if k in stList:
152: Foundst=True
153: st.append(j)
154: IndexBack.append(ListBase[i][3::][j].index(k))
155: if Foundst==True:
156: if k in ndList:
157: Foundnd=True
158: nd.append(j)
159: IndexBack.append(ListBase[i][3::][j].index(k))
160: if (len(st)!=0) and (len(nd)!=0):
161: if st==nd:
162: New=ListBase[i][3::][st[-1]].replace(ListBase[i][3::][st[-1]][IndexBack[0]:IndexBack[1]+1:],'')
163: del ListBase[i][st[-1]+3]
164: ListBase[i].insert(st[-1]+3,New)
165: elif st != nd :
166: del ListBase[i][st[-1]+3]
167: ListBase[i].insert(st[-1]+3,'\n')
168: del ListBase[i][nd[-1]+3]
169: ListBase[i].insert(nd[-1]+3,'\n')
170: # 2
171: for i in range(len(ListBase)):
172: for j in range(len(ListBase[i][3::])):
173: if (ListBase[i][3::][j][:-1:].isalnum())==True:
174: pass
175: else:
176: BCheck=False
177: for m in ListBase[i][3::][j][:-1:]:
178: if m.isalnum() == True:
179: BCheck=True
180: if BCheck==False:
181: del ListBase[i][j+3]
182: ListBase[i].insert(j+3,'\n')
183: P_ListBase=[]
184: for i in range(len(ListBase)):
185: P_ListBase.append(list(ListBase[i]))
186:
187: for i in range(len(ListBase)):
188: if '\n'in ListBase[i][3::] :
189: P_ListBase.remove(ListBase[i])
190:
191: for i in range(len(P_ListBase)):
192: P_ListBase[i][1]=str(i+1)+'\n'
193: P_ListBase[0].pop(0)
194: fout = open(file_out, 'w', encoding='utf-8')
195: for i in P_ListBase:
196: for j in i:
197: fout.write(j)
198: fout.close()
199:
200: return
201:
202: # ------------------------------------------
203:
204: def TimeShift(Time,time_shift):
205: DeleteText=False
206: MiliSecA=0
207: MiliSecC=0
208: A,B,C=Time.split()
209: MiliSecA+=int(A[0:2])*3600000
210: MiliSecA+=int(A[3:5])*60000
211: MiliSecA+=int(A[6:8])*1000
212: MiliSecA+=int(A[9:12])
213: MiliSecA+=time_shift
214:
215: MiliSecC+=int(C[0:2])*3600000
216: MiliSecC+=int(C[3:5])*60000
217: MiliSecC+=int(C[6:8])*1000
218: MiliSecC+=int(C[9:12])
219: MiliSecC+=time_shift
220:
221: if (MiliSecA <=0) and (MiliSecC<=0):
222: DeleteText=True
223:
224: if DeleteText==False:
225: #hrA='00' ; minA='00' ; secA='00' ; milisecA='000'
226: hrA=str(MiliSecA//3600000)
227: if len(hrA)!=2:
228: hrA= '0'+hrA
229: minA=str((MiliSecA%3600000)//60000)
230: if len(minA)!=2:
231: minA= '0'+minA
232: secA=str(((MiliSecA%3600000)%60000)//1000)
233: if len(secA)!=2:
234: secA= '0'+secA
235: milisecA=str(((MiliSecA%3600000)%60000)%1000)
236: if 2<=len(milisecA)<3:
237: milisecA='0'+milisecA
238: else:
239: if 1<=len(milisecA)<2:
240: milisecA='00'+milisecA
241: a=hrA + ':' + minA+':'+secA+','+milisecA
242: #hrC='00' ; minC='00' ; secC='00' ; milisecC='000'
243: hrC=str(MiliSecC//3600000)
244: if len(hrC)!=2:
245: hrC= '0'+hrC
246: minC=str((MiliSecC%3600000)//60000)
247: if len(minC)!=2:
248: minC= '0'+minC
249: secC=str(((MiliSecC%3600000)%60000)//1000)
250: if len(secC)!=2:
251: secC= '0'+secC
252: milisecC=str(((MiliSecC%3600000)%60000)%1000)
253: if 2<=len(milisecC)<3:
254: milisecC='0'+milisecC
255: else:
256: if 1<=len(milisecC)<2:
257: milisecC='00'+milisecC
258: c=hrC+':'+minC+':'+secC+','+milisecC
259:
260: Result= a+' '+B+''+c+'\n'
261: else:
262: Result='X\n'
263: return Result
264:
265: def FindMili(A):
266: MiliSec=0
267: MiliSec+=int(A[0:2])*3600000
268: MiliSec+=int(A[3:5])*60000
269: MiliSec+=int(A[6:8])*1000
270: MiliSec+=int(A[9:12])
271: return MiliSec
6430133321 (66.67%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430133321
003:
004: # ------------------------------------------
005:
006: def str_int(time):
007: hr,mi,sc = time.strip().split(":")
008: sc = sc[:2]+sc[3:]
009: return int(hr)*3600000+int(mi)*60000+int(sc)
010:
011: def int_str(time):
012: hr = '0'*(2-len(str(time//3600000)))+str(time//3600000)
013: time %=3600000
014: mi = '0'*(2-len(str(time//60000)))+str(time//60000)
015: time %=60000
016: sc = '0'*(5-len(str(time)))+str(time)
017: sc = sc[:2]+','+sc[2:]
018:
019: return hr+':'+mi+':'+sc
020:
021: def time_text(timest,timeed):
022: return int_str(timest)+' --> '+int_str(timeed)+'\n'
023:
024: def shift(file_in, time_shift, file_out):
025: fi=open(file_in,encoding = "utf-8")
026: fo=open(file_out,"w",encoding = "utf-8")
027: cou=1
028: text=[]
029: line = fi.readline()
030: while(len(line)!=0):
031: if(line=='\n'):
032: timest,timeed=[max(str_int(e)+time_shift,0) for e in text[1].split("-->")]
033: if timeed!=0:
034: fo.write('%d\n'%cou)
035: cou+=1
036: fo.write(time_text(timest,timeed)+''.join(text[2:]))
037: text.clear()
038: line = fi.readline()
039: text.append(line)
040: line = fi.readline()
041:
042: fi.close()
043: fo.close()
044: return
045:
046: # ------------------------------------------
047:
048:
049: def merge(base_file, merge_file, threshold, file_out):
050: ba=open(base_file,encoding = "utf-8")
051: mf=open(merge_file,encoding = "utf-8")
052: fo=open(file_out,'w',encoding = "utf-8")
053:
054: data=[]
055: data_mf=[]
056: text=[]
057:
058: line = ba.readline()
059: while(len(line)!=0):
060: if(line=='\n'):
061: timest,timeed=[str_int(e) for e in text[1].split("-->")]
062: data.append([timest,timeed,''.join(text[2:])])
063: text.clear()
064: line = ba.readline()
065: text.append(line)
066: line = ba.readline()
067:
068: c_ba =len(data)
069: text.clear()
070: line = mf.readline()
071:
072: while(len(line)!=0):
073: if(line=='\n'):
074: timest,timeed=[str_int(e) for e in text[1].split("-->")]
075: data_mf.append([timest,timeed,''.join(text[2:])])
076: text.clear()
077: line = mf.readline()
078: text.append(line)
079: line = mf.readline()
080:
081: poi=0
082:
083: for i in range(len(data_mf)):
084: pm=-1
085: rt=1e9
086: for j in range(poi,c_ba):
087: if data[j][0]-threshold<=data_mf[i][0]<=data[j][0]+threshold:
088: if rt>abs(data[j][0]-data_mf[i][0]):
089: rt=abs(data[j][0]-data_mf[i][0])
090: pm=j
091: elif data[j][0]+threshold<data_mf[i][0]:
092: poi+=1
093: else:
094: break
095: if pm==-1:
096: data.append(data_mf[i])
097: else:
098: data[pm].append(data_mf[i][2])
099:
100: data.sort()
101:
102: for i in range(len(data)):
103: fo.write('%d\n'%(i+1))
104: fo.write(time_text(data[i][0],data[i][1])+''.join(data[i][2:])+'\n')
105:
106: ba.close()
107: mf.close()
108: fo.close()
109: return
110:
111: # ------------------------------------------
112:
113: def rule(text):
114: text_re=''
115: r1='([{<'
116: r2=')]}>'
117: ch=False
118: for e in text:
119: if e in r1:
120: ch=True
121: elif e in r2:
122: ch=False
123: else:
124: if not ch:
125: text_re+=e
126:
127: text_re2=''
128:
129: for i in text_re.split('\n'):
130: ch=False
131: for j in i.strip():
132: if j.isalnum():
133: ch=True
134: break
135: if ch: text_re2+=i+'\n'
136: return text_re2
137:
138: def clean(file_in, file_out):
139: fi=open(file_in,encoding = "utf-8")
140: fo=open(file_out,"w",encoding = "utf-8")
141: cou=1
142: text=[]
143: line=fi.readline()
144: while(len(line)!=0):
145: if(line=='\n'):
146: text[2]=rule(''.join(text[2:]))
147: if len(text[2])>0:
148: fo.write('%d\n'%cou+text[1]+text[2]+'\n')
149: cou+=1
150: text.clear()
151: line = fi.readline()
152: text.append(line)
153: line = fi.readline()
154: fi.close()
155: fo.close()
156: return
157:
158: # ------------------------------------------
159:
160: #shift('C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/SRT_Files/test1_en.srt', -5000, 'C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/test1_en_shifted.srt')
161: # merge('C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/SRT_Files/test4_en.srt','C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/SRT_Files/test4_th.srt',1000,'C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/test4_en_th_cleaned.srt')
162: clean('C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/SRT_Files/test3_en.srt','C:/Users/Tata/Desktop/Python/CU/Greader/Year2021/HW8/test3_en_cleaned.srt')
6430145921 (86.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 90.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
00:59:50,100 --> 01:00:08,100
3
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 70.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
DDD
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430145921
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: fin = open(file_in, encoding='utf-8')
007: lst = []
008: count = 1
009: found = True
010: for line in fin:
011: x = line
012: if '-->' in line:
013: x = line.split(':')
014: h = int(x[0])*60*60*1000 ; m =int(x[1])*60*1000; s = int(x[2][:2])*1000; ms = int(x[2][3:6])
015: total = h + m + s + ms + time_shift
016: h1 = int(x[2][12:])*60*60*1000; m1 = int(x[3])*60*1000 ; s1= int(x[-1][:2])*1000; ms1 = int(x[-1][3:6])
017: total1 = h1 + m1 + s1 + ms1 + time_shift
018: if total < 0 and total1 < 0:
019: lst = lst[:-1]
020: found = False
021: count -= 1
022: if total < 0 and total1 >=0:
023: H = total1//60//60//1000; M = (total1//60//1000) - (H*60); S = (total1//1000) - (M*60) - (H*60*60); MS =total1%1000
024: H = '00'+str(H); M = '00'+str(M); S = '00'+str(S); MS = '000'+str(MS)
025: x = '00:00:00,000 --> '+ H[-2:] +':'+ M[-2:]+':'+S[-2:]+','+MS[-3:]+'\n'
026: if total >=0 and total1 >=0:
027: H1 = total // 3600000;M1 = (total // 60000) - (H1 * 60);S1 = (total // 1000) - (M1 * 60) - (H1 * 60*60);MS1 = total % 1000
028: H1 = '00' + str(H1);M1 = '00' + str(M1);S1 = '00' + str(S1);MS1 = '000' + str(MS1)
029: H = total1//3600000; M = (total1//60000) - (H*60); S = (total1//1000) - (M*60) - (H*60*60); MS =total1%1000
030: H = '00' + str(H);M = '00' + str(M);S = '00' + str(S);MS = '000' + str(MS)
031: x = H1[-2:] +':'+ M1[-2:]+':'+S1[-2:]+','+MS1[-3:] + ' --> '+ H[-2:] +':'+ M[-2:]+':'+S[-2:]+','+MS[-3:]+'\n'
032: if found:
033: lst.append(x)
034: if x == '\n':
035: found = True
036: count += 1
037: fin_change = lst
038: fin.close()
039: fout = open(file_out, 'w', encoding='utf-8')
040: found = False
041: i = 1
042: for info in fin_change:
043: if '-->' in info:
044: found = True
045: while i <= count and not found:
046: fout.write(str(i) + "\n")
047: i += 1
048: break
049: if found:
050: fout.write(info)
051: if '\n' == info:
052: found = False
053: fout.close()
054:
055: # ------------------------------------------
056: def merge(base_file, merge_file, threshold, file_out):
057: base = []; merge = []; total_file = []; out = []; use_merge = []; unuse_merge =[]; final_out = []
058: f_base = open(base_file, encoding='utf-8')
059: for line in f_base:
060: base.append(line)
061: f_base.close()
062: f_merge = open(merge_file, encoding='utf-8')
063: for line in f_merge:
064: merge.append(line)
065: f_merge.close()
066: for i in range(len(base)):
067: if '-->' in base[i]:
068: j = 0
069: found = True
070: x = base[i].split(':')
071: h = int(x[0])*60*60*1000 ; m =int(x[1])*60*1000; s = int(x[2][:2])*1000; ms = int(x[2][3:6])
072: base_time = h + m + s + ms
073: while j < len(merge):
074: n = 0
075: if '-->' in merge[j]:
076: y = merge[j].split(':')
077: h1 = int(y[0]) * 60 * 60 * 1000; m1 = int(y[1]) * 60 * 1000; s1 = int(y[2][:2]) * 1000; ms1 = int(y[2][3:6])
078: merge_time = h1 + m1 + s1 + ms1
079: if abs(merge_time - base_time) <= threshold:
080: if found:
081: total_file.append(base[i-1])
082: k = i
083: while base[k] != '\n':
084: total_file.append(base[k])
085: k += 1
086: n = 1
087: if k >= len(base):
088: break
089: found = False
090: title_m = []
091: use_merge.append(merge[j])
092: while merge[j+1] != '\n':
093: title_m.append(merge[j+1])
094: j += 1
095: n = 1
096: if j+1 >= len(merge):
097: break
098: if title_m != []:
099: for info in title_m:
100: total_file.append(info)
101: total_file.append('\n')
102: if n == 0:
103: j += 1
104: found = True
105: for line in base:
106: if '-->' in line:
107: if line in total_file:
108: no = total_file.index(line)
109: while total_file[no] != '\n':
110: out.append(total_file[no])
111: no += 1
112: found = False
113: else:
114: i = base.index(line)
115: while base[i] != '\n':
116: out.append(base[i])
117: i +=1
118: found = False
119: else:
120: if line == '\n':
121: found = True
122: if found:
123: out.append(line)
124: found = True
125: k = 1
126: while k < len(merge):
127: if '-->' in merge[k]:
128: if merge[k] in use_merge:
129: unuse_merge = unuse_merge[:-1]
130: found = False
131: else:
132: unuse_merge.append(merge[k])
133: else:
134: if found:
135: unuse_merge.append(merge[k])
136: if merge[k] == '\n':
137: found = True
138: k += 1
139: i, j = 0, 0; found1 = False; found2 = False; total = 0; total1 = 0
140: while i < len(out) and j < len(unuse_merge):
141: if '-->' in out[i]:
142: x = out[i].split(':')
143: h = int(x[0]) * 60 * 60 * 1000; m = int(x[1]) * 60 * 1000; s = int(x[2][:2]) * 1000; ms = int(x[2][3:6])
144: total = h + m + s + ms
145: found1 = True
146: if '-->' in unuse_merge[j]:
147: y = unuse_merge[j].split(':')
148: h1 = int(y[0]) * 60 * 60 * 1000; m1 = int(y[1]) * 60 * 1000; s1 = int(y[2][:2]) * 1000; ms1 = int(y[2][3:6])
149: total1 = h1 + m1 + s1 + ms1
150: found2 = True
151: if found1 and found2:
152: if total < total1:
153: found1 = False
154: final_out.append(out[i-1])
155: k = i
156: while out[k] != '\n':
157: final_out.append(out[k])
158: k += 1
159: if k >= len(out):break
160: if out[k] == '\n':
161: final_out.append('\n')
162: elif total > total1:
163: found2 = False
164: final_out.append(unuse_merge[j-1])
165: r = j
166: while unuse_merge[r] != '\n':
167: final_out.append(unuse_merge[r])
168: r += 1
169: if r >= len(unuse_merge):break
170: if unuse_merge[r] == '\n':
171: final_out.append('\n')
172: if not found1:
173: if i == len(out):
174: i -= 1
175: found1 = True
176: total = 0
177: else:
178: i += 1
179: if not found2:
180: if j == len(unuse_merge):
181: j -= 1
182: found2 = True
183: total1 = 0
184: else:
185: j += 1
186: if j == len(unuse_merge) -1:
187: a = i
188: final_out.append(out[a-1])
189: while a < len(out):
190: final_out.append(out[a])
191: a += 1
192: elif i == len(out) -1:
193: b = j
194: final_out.append(unuse_merge[b-1])
195: while b < len(unuse_merge):
196: final_out.append(unuse_merge[b])
197: b += 1
198: fout = open(file_out, 'w', encoding='utf-8')
199: found = False
200: i = 1
201: for info in final_out:
202: if '-->' in info:
203: found = True
204: while not found:
205: fout.write(str(i) + "\n")
206: i += 1
207: break
208: if found:
209: fout.write(info)
210: if final_out.index(info)+2 < len(final_out):
211: if '\n' == info:
212: found = False
213: fout.close()
214:
215: # ------------------------------------------
216: def clean(file_in, file_out):
217: fin = open(file_in, encoding='utf-8')
218: found = 0
219: lst = []
220: lst_change = []
221: out = []
222: for line in fin:
223: i = 0
224: text = ''
225: while i < len(line):
226: if '-->' not in line:
227: if line[i] in '(){}[]<>':
228: found += 1
229: i += 1
230: if found % 2 == 0:
231: text += line[i]
232: i += 1
233: if line[-1] != '\n':
234: text += '\n'
235: if line == '\n':
236: lst.append(line)
237: elif text != '':
238: lst.append(text.strip()+'\n')
239: found = False
240: for i in range(len(lst)):
241: for e in lst[i]:
242: if e.isalnum():
243: found = True
244: break
245: if found:
246: lst_change.append(lst[i])
247: found = False
248: if lst[i] == '\n' and '-->' in lst[i-1]:
249: n = 0
250: for e in lst[i+1]:
251: if e.isalnum():
252: n = 1
253: break
254: if n == 0:
255: lst_change.append('\n')
256: if lst[i] == '\n' and lst[i-1] == '\n' and '-->' not in lst[i-3]:
257: lst_change = lst_change[:-3]
258: for i in range(len(lst_change)):
259: out.append(lst_change[i])
260: if i + 2 < len(lst_change):
261: if '-->' in lst_change[i] and '-->' in lst_change[i+2]:
262: out = out[:-2]
263: fin.close()
264: fout = open(file_out, 'w', encoding='utf-8')
265: found = False
266: i = 1
267: for e in range(len(out)):
268: if '-->' in out[e]:
269: found = True
270: while not found:
271: fout.write(str(i) + "\n")
272: i += 1
273: break
274: if found:
275: fout.write(out[e])
276: if e + 2 < len(out):
277: if '-->' in out[e + 2]:
278: fout.write('\n')
279: found = False
280: fout.close()
281:
282: # ------------------------------------------
283: clean('test3_en.srt','test.srt')
6430146521 (86.67%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 80.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
00:01:55,500 --> 00:02:10,600
3
00:59:50,100 --> 01:00:08,100
4
01:01:05,100 --> 01:01:18,100
5
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
DDD
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430146521
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: x=open(file_in,"r" , encoding='utf-8')
007: data=[]
008: subdata=[]
009: for line in x:
010: if line=="\n":
011: data.append(subdata)
012: subdata=[]
013: else:
014: subdata.append(line[:-1])
015: if subdata!=[]:
016: data.append(subdata)
017: x.close()
018: c=1
019: newdata=[]
020: for i in range(len(data)):
021: sft=data[i][1].split(" --> ")
022: start=sft[0].split(":")
023: end=sft[1].split(":")
024: hs=int(start[0])
025: ms=int(start[1])
026: ssall=start[2].split(",")
027: ss=int(ssall[0])
028: mils=int(ssall[1])
029: he=int(end[0])
030: me=int(end[1])
031: seall=end[2].split(",")
032: se=int(seall[0])
033: mile=int(seall[1])
034: startsum=mils+ss*1000+ms*1000*60+hs*1000*60*60
035: endsum=mile+se*1000+me*1000*60+he*1000*60*60
036: ns=startsum+time_shift
037: ne=endsum+time_shift
038: nhs=str(ns//(3600000))
039: nms=str((ns//(60000))%60)
040: nss=str((ns//1000)%60)
041: nmils=str(ns%1000)
042: nhe=str(ne//(3600000))
043: nme=str((ne//(60000))%60)
044: nse=str((ne//1000)%60)
045: nmile=str(ne%1000)
046: while len(nhs)<2:
047: nhs="0"+nhs
048: while len(nhe)<2:
049: nhe="0"+nhe
050: while len(nms)<2:
051: nms="0"+nms
052: while len(nme)<2:
053: nme="0"+nme
054: while len(nss)<2:
055: nss="0"+nss
056: while len(nse)<2:
057: nse="0"+nse
058: while len(nmils)<3:
059: nmils="0"+nmils
060: while len(nmile)<3:
061: nmile="0"+nmile
062: if ns>=0:
063: newsubdata=[]
064: newsubdata.append(c)
065: newsubdata.append(nhs+":"+nms+":"+nss+","+nmils+" --> "+nhe+":"+nme+":"+nse+","+nmile)
066: for j in range(len(data[i])):
067: if j<=1:
068: pass
069: else:
070: newsubdata.append(data[i][j])
071: c+=1
072: newdata.append(newsubdata)
073: elif ns<0 and ne>0:
074: newsubdata=[]
075: newsubdata.append(c)
076: newsubdata.append("00:00:00,000"+" --> "+nhe+":"+nme+":"+nse+","+nmile)
077: for j in range(len(data[i])):
078: if j<=1:
079: pass
080: else:
081: newsubdata.append(data[i][j])
082: c+=1
083: newdata.append(newsubdata)
084: elif ns<0 and ne<0:
085: pass
086:
087:
088: y=open(file_out,"w" , encoding='utf-8')
089: for i in range(len(newdata)):
090: for j in newdata[i]:
091: y.write(str(j)+"\n")
092: y.write("\n")
093: y.close()
094:
095:
096: # ------------------------------------------
097: def merge(base_file, merge_file, threshold, file_out):
098: x1=open(base_file,"r" , encoding='utf-8')
099: data1=[]
100: subdata1=[]
101: for line in x1:
102: if line=="\n":
103: data1.append(subdata1)
104: subdata1=[]
105: else:
106: subdata1.append(line[:-1])
107: if subdata1!=[]:
108: data1.append(subdata1)
109: x1.close()
110:
111: x2=open(merge_file,"r" , encoding='utf-8')
112: data2=[]
113: subdata2=[]
114: for line in x2:
115: if line=="\n":
116: data2.append(subdata2)
117: subdata2=[]
118: else:
119: subdata2.append(line[:-1])
120: if subdata2!=[]:
121: data2.append(subdata2)
122: x2.close()
123: newdata=[]
124: alldata=[]
125: c=1
126: for i in range(len(data1)):
127: sft=data1[i][1].split(" --> ")
128: start=sft[0].split(":")
129: end=sft[1].split(":")
130: hs=int(start[0])
131: ms=int(start[1])
132: ssall=start[2].split(",")
133: ss=int(ssall[0])
134: mils=int(ssall[1])
135: he=int(end[0])
136: me=int(end[1])
137: seall=end[2].split(",")
138: se=int(seall[0])
139: mile=int(seall[1])
140: startsum=mils+ss*1000+ms*1000*60+hs*1000*60*60
141: endsum=mile+se*1000+me*1000*60+hs*1000*60*60
142:
143: nhs=str(startsum//(3600000))
144: nms=str((startsum//(60000))%60)
145: nss=str((startsum//1000)%60)
146: nmils=str(startsum%1000)
147: nhe=str(endsum//(3600000))
148: nme=str((endsum//(60000))%60)
149: nse=str((endsum//1000)%60)
150: nmile=str(endsum%1000)
151: while len(nhs)<2:
152: nhs="0"+nhs
153: while len(nhe)<2:
154: nhe="0"+nhe
155: while len(nms)<2:
156: nms="0"+nms
157: while len(nme)<2:
158: nme="0"+nme
159: while len(nss)<2:
160: nss="0"+nss
161: while len(nse)<2:
162: nse="0"+nse
163: while len(nmils)<3:
164: nmils="0"+nmils
165: while len(nmile)<3:
166: nmile="0"+nmile
167: data1[i].insert(0,startsum)
168:
169: for i in range(len(data2)):
170: sft2=data2[i][1].split(" --> ")
171: start2=sft2[0].split(":")
172: end2=sft2[1].split(":")
173: hs2=int(start2[0])
174: ms2=int(start2[1])
175: ssall2=start2[2].split(",")
176: ss2=int(ssall2[0])
177: mils2=int(ssall2[1])
178: he2=int(end2[0])
179: me2=int(end2[1])
180: seall2=end2[2].split(",")
181: se2=int(seall2[0])
182: mile2=int(seall2[1])
183: startsum2=mils2+ss2*1000+ms2*1000*60+hs2*1000*60*60
184: endsum2=mile2+se2*1000+me2*1000*60+he2*1000*60*60
185:
186: nhs2=str(startsum2//(3600000))
187: nms2=str((startsum2//(60000))%60)
188: nss2=str((startsum2//1000)%60)
189: nmils2=str(startsum2%1000)
190: nhe2=str(endsum2//(3600000))
191: nme2=str((endsum2//(60000))%60)
192: nse2=str((endsum2//1000)%60)
193: nmile2=str(endsum2%1000)
194: while len(nhs2)<2:
195: nhs2="0"+nhs2
196: while len(nhe2)<2:
197: nhe2="0"+nhe2
198: while len(nms2)<2:
199: nms2="0"+nms2
200: while len(nme2)<2:
201: nme2="0"+nme2
202: while len(nss2)<2:
203: nss2="0"+nss2
204: while len(nse2)<2:
205: nse2="0"+nse2
206: while len(nmils2)<3:
207: nmils2="0"+nmils2
208: while len(nmile2)<3:
209: nmile2="0"+nmile2
210: data2[i].insert(0,startsum2)
211: alldata=[]
212: txt=[]
213: newsubdata=[]
214: for i in range(len(data2)):
215: data2[i].insert(1,False)
216: for i in range(len(data1)):
217: newsubdata.append(data1[i][0])
218: newsubdata.append(data1[i][2])
219: for x in data1[i][3:]:
220: txt.append(x)
221: for j in range(len(data2)):
222: if abs(data1[i][0]-data2[j][0])<=int(threshold):
223: for y in data2[j][4:]:
224: txt.append(y)
225: data2[j][1]=True
226: newsubdata.append(txt)
227: alldata.append(newsubdata)
228: newsubdata=[]
229: txt=[]
230: for i in range(len(data2)):
231: if data2[i][1]==False:
232: newsubdata.append(data2[i][0])
233: newsubdata.append(data2[i][3])
234: for x in data2[i][4:]:
235: txt.append(x)
236: newsubdata.append(txt)
237: alldata.append(newsubdata)
238: newsubdata=[]
239: txt=[]
240: alldata.sort()
241:
242:
243: y=open(file_out,"w" , encoding='utf-8')
244: c=1
245: for i in range(len(alldata)):
246: y.write(str(c)+"\n")
247: y.write(alldata[i][1]+"\n")
248: for x in alldata[i][2]:
249: y.write(x+"\n")
250: y.write("\n")
251: c+=1
252: y.close()
253:
254:
255:
256:
257: # ------------------------------------------
258: def clean(file_in, file_out):
259: x=open(file_in,"r" , encoding='utf-8')
260: data=[]
261: subdata=[]
262: for line in x:
263: if line=="\n":
264: data.append(subdata)
265: subdata=[]
266: else:
267: subdata.append(line[:-1])
268: if subdata!=[]:
269: data.append(subdata)
270: x.close()
271:
272: alltxt=[]
273: txt=[]
274: datax=[]
275:
276: for i in range(len(data)):
277: datax.append(data[i][1])
278: for x in data[i][2:]:
279: txt.append(x)
280: datax.append(txt)
281: alltxt.append(datax)
282: datax=[]
283: txt=[]
284:
285: for i in range(len(alltxt)):
286: amo=True
287: sugoma=[]
288: for x in alltxt[i][1]:
289: sus=""
290: for c in x:
291: if c in "<([{":
292: amo=False
293: elif c in ")}]>":
294: amo=True
295: elif amo==True:
296: sus+=c
297: if sus=="":
298: pass
299: else:
300: sugoma.append(sus)
301: alltxt[i].append(sugoma)
302:
303: for i in range(len(alltxt)):
304: sugoma=[]
305: for x in alltxt[i][2]:
306: sus=""
307: for c in x:
308: if c.isalnum()==True:
309: sugoma.append(x)
310: break
311: alltxt[i].append(sugoma)
312:
313: y=open(file_out,"w" , encoding='utf-8')
314: c=1
315: for i in range(len(alltxt)):
316: y.write(str(c)+"\n")
317: y.write(alltxt[i][0]+"\n")
318: for x in alltxt[i][-1]:
319: y.write(x+"\n")
320: y.write("\n")
321: c+=1
322: y.close()
323:
324:
325:
326: # ------------------------------------------
6430151621 (93.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 80.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
00:01:55,500 --> 00:02:10,600
3
00:59:50,100 --> 01:00:08,100
4
01:01:05,100 --> 01:01:18,100
5
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430151621
003:
004: def time2num(time):
005: hour,minute,second = time.split(':')
006: hour = int(hour) ; minute = int(minute) ; second,millisec = [int(i) for i in second.split(',')]
007: total_time = (hour*3600000)+(minute*60000)+(second*1000)+millisec
008: return total_time
009:
010: def num2time(total_time):
011: hour = total_time//3600000
012: total_time %= 3600000
013: minute = total_time//60000
014: total_time %= 60000
015: second = total_time//1000
016: millisec = total_time % 1000
017: time_set = [str(i) for i in [hour, minute, second]]
018: for i in range(len(time_set)):
019: while len(time_set[i]) < 2:
020: time_set[i] = '0'+time_set[i]
021: millisec = str(millisec)
022: while len(millisec)<3:
023: millisec = '0'+millisec
024: time_set.append(millisec)
025: a,b,c,d = time_set
026: time_set = [a,':',b,':',c,',',d]
027: time_final = ''.join(time_set)
028: return time_final
029:
030: def rmv_bracket(string):
031: temp = '' ; is_avoid = False
032: for j in string:
033: if j in ['(','[','{','<','>','}',']',')']:
034: is_avoid = not is_avoid
035: continue
036: if not is_avoid:
037: temp += j
038: return temp
039:
040: def rmv_empty(string):
041: if string == '':
042: return ''
043: else:
044: has_alnum = False
045: for i in string:
046: has_alnum = has_alnum or i.isalnum()
047: if has_alnum: return string
048: else: return ''
049: return string
050:
051: # ------------------------------------------
052: def shift(file_in, time_shift, file_out):
053: # ***********************************************************************
054: data_read = open(file_in, 'r', encoding='utf-8') ; data_raw = [] ; temp = [] ; count = 1
055: for i in data_read:
056: if i == '\n':
057: data_raw.append(list(temp)) ; temp = []
058: else: temp.append(i.split('\n')[0])
059: data_read.close()
060: # shifting
061: data_shifted = []
062: for data in data_raw:
063: time_in,time_out = data[1].split(' --> ')
064: time_in = time2num(time_in)+time_shift
065: time_out = time2num(time_out)+time_shift
066: if time_in < 0:time_in = 0
067: if time_out < 0:time_out = 0
068: time_final_formated = ''.join([num2time(time_in),' --> ',num2time(time_out)])
069: if time_final_formated == '00:00:00,000 --> 00:00:00,000':
070: continue
071: else:
072: data.pop(1)
073: data.insert(1,time_final_formated)
074: data.pop(0)
075: data.insert(0,str(count))
076: data_shifted.append(data)
077: count += 1
078: for i in range(len(data_shifted)):
079: for j in range(len(data_shifted[i])):
080: data_shifted[i][j] += '\n'
081: data_write = open(file_out, 'w', encoding = 'utf-8')
082: for i in data_shifted:
083: for j in i:
084: data_write.write(j)
085: data_write.write('\n')
086: data_write.close()
087: # 2 testcases checked
088: # ------------------------------------------
089: def merge(base_file, merge_file, threshold, file_out):
090: data_read = open(base_file, 'r', encoding='utf-8') ; data_a = [] ; temp = []
091: for i in data_read:
092: if i == '\n':
093: data_a.append(list(temp)) ; temp = []
094: else: temp.append(i.split('\n')[0])
095: data_read.close()
096: data_read = open(merge_file, 'r', encoding='utf-8') ; data_b = [] ; temp = []
097: for i in data_read:
098: if i == '\n':
099: data_b.append(list(temp)) ; temp = []
100: else: temp.append(i.split('\n')[0])
101: data_read.close()
102: # merging
103: data_a_time = [];data_b_time = []
104: for data in data_a:
105: time_in,time_out = data[1].split(' --> ')
106: data.pop(1);data.pop(0)
107: data.insert(0,time_out)
108: data.insert(0,time2num(time_in))
109: data_a_time.append(data)
110: for data in data_b:
111: time_in,time_out = data[1].split(' --> ')
112: # '00:00:00,000'
113: data.pop(1);data.pop(0)
114: data.insert(0,time_out)
115: data.insert(0,time2num(time_in))
116: data_b_time.append(data)
117: # data_x_time = [int(time_in),str(time_out),sub]*x
118: # sub_merging
119: data_a_merged_sub = [];data_b_merged_sub = []
120: for i in range(len(data_a_time)):
121: each_sub_time = data_a_time[i][0]
122: each_sub_time_out = data_a_time[i][1]
123: temp = ''.join(data_a_time[i][2:]) #sub joined
124: data_a_merged_sub.append([each_sub_time,each_sub_time_out,temp])
125: for i in range(len(data_b_time)):
126: each_sub_time = data_b_time[i][0]
127: each_sub_time_out = data_b_time[i][1]
128: temp = ''.join(data_b_time[i][2:]) #sub joined
129: data_b_merged_sub.append([each_sub_time,each_sub_time_out,temp])
130: # adding sub seperator
131: for i in range(len(data_a_time)):data_a_time[i][2] += '\n'
132: for i in range(len(data_b_time)):data_b_time[i][2] += '\n'
133: data_a_time.sort();data_b_time.sort()
134: # [time,time,sub,sub,sub]
135: time_gap = [];to_add = [] # time_gap = [num,num,num,num] sort as data_a_time
136: for i in data_b_time: # to merge list
137: for j in data_a_time: # base list
138: time_gap.append(abs(i[0]-j[0]))
139: lowest_gap = min(time_gap)
140: if lowest_gap > threshold:
141: to_add.append(i) # to_add = [int(time_in),str(time_out),sub]*x
142: time_gap = []
143: continue
144: else:
145: target_base_ind = time_gap.index(lowest_gap)
146: data_a_time[target_base_ind].append(''.join(i[2:]))
147: time_gap = []
148: for i in to_add:
149: data_a_time.append(i)
150: data_a_time.sort()
151: output = [];count = 1
152: for i in data_a_time: # [int(time_in),str(time_out),sub,sub,sub]
153: temp = []
154: total_time = i[0]
155: time_out = i[1]
156: for j in i[2:]:
157: if j[-1:] != '\n':
158: temp.append(j+'\n')
159: else:
160: temp.append(j)
161: subs = ''.join(temp)
162: if subs[-1:] != '\n':
163: subs += '\n'
164: time_final = num2time(total_time)
165: time_final_formated = time_final+' --> '+time_out
166: output.append([ count, time_final_formated, subs])
167: count += 1
168: data_write = open(file_out, 'w', encoding = 'utf-8')
169: for i in output:
170: for j in i:
171: data_write.write(str(j))
172: data_write.write('\n')
173: data_write.close()
174: #merge('test2_en.srt','test2_th.srt',1000,'test2_handmade_merged.srt')
175: #merge('eng.srt','th.srt',1000,'test2_handmade_merged.srt')
176: #checked
177: # ------------------------------------------
178: def clean(file_in, file_out):
179: data_read = open(file_in, 'r', encoding='utf-8') ; data = [] ; temp = []
180: for i in data_read:
181: if i == '\n':
182: data.append(list(temp)) ; temp = []
183: else: temp.append(i.split('\n')[0])
184: for i in range(len(data)):
185: data[i].pop(0)
186: data_read.close()
187: # processing
188: subs_ctx_removed = []
189: for i in data: # รวมโดยมี \n
190: temp_2 = []
191: subs = i[1:] ; time = i[0] ; temp = [] ; subs_added = []
192: for i in subs: # adding \n
193: i += '\n'
194: subs_added.append(i)
195: sub = rmv_bracket(''.join(subs_added)) #sub = string
196: sub = sub.split('\n')[:-1]
197: temp = [time]
198: for j in sub:
199: result = rmv_empty(j)
200: if result == '':
201: continue
202: else:
203: temp.append(result)
204: subs_ctx_removed.append(temp)
205: count = 1
206: for i in range(len(subs_ctx_removed)):
207: subs_ctx_removed[i].insert(0, count)
208: count += 1
209: data_write = open(file_out, 'w', encoding='utf-8')
210: for i in subs_ctx_removed:
211: for j in i:
212: data_write.write(str(j)+'\n')
213: data_write.write('\n')
214: data_write.close()
215: return
216: #clean('test2_en.srt', 'test1_en_cleaned.srt')
217: #print(rmv_bracket(""))
218: #import os
219: #os.system('fc test1_en_cleaned.srt test2_en_cleaned.srt')
220: # ------------------------------------------
6430154521 (53.33%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 80.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123 ♪
A♪
2
00:01:55,500 --> 00:02:10,600
$$
3
00:59:50,100 --> 01:00:08,100
♫♫ ♫
4
01:01:05,100 --> 01:01:18,100
♪♪
5
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,500 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:57,480 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID 6430154521
003:
004: def read_next(f):
005: while True:
006: t = f.readline()
007: if len(t) == 0:
008: break
009: return t
010: return -1
011:
012: def change_file_to_list(file):
013: x = [] ; A = read_next(file)
014: while A != -1 :
015: x.append(A)
016: A = read_next(file)
017: z = [] ; y = []
018: for e in x:
019: if e != "\n":
020: z.append(e)
021: else :
022: y.append(z[1:])
023: z = []
024: return y
025:
026: def easy_read_time(alist):
027: A = []
028: for e in alist :
029: x = " ".join(e[0].strip().split("-->")).split()
030: for e in x:
031: q = e.split(":") ; r = q[2].split(",") ; q.remove(q[2])
032: A.append(q+r)
033: return A
034: def change_time(alist,time):
035: A = easy_read_time(alist) ;C =[];D = []
036: for k in A:
037: ms = int(k[3]) ; s = int(k[2])
038: mn = int(k[1]) ; hr = int(k[0])
039: All_ms = (hr*60*60*1000)+(mn*60*1000)+(s*1000)+ms+time
040: if All_ms >= 0:
041: hr = All_ms // (60*60*1000)
042: All_ms -= hr * 60*60*1000
043: mn = All_ms // (60*1000)
044: All_ms -= mn *60*1000
045: s = All_ms // 1000
046: All_ms -= s*1000
047: ms = All_ms
048: k[0] = "0"*(2-len(str(hr)))+str(hr) ; k[1] = "0"*(2-len(str(mn)))+str(mn)
049: k[2] = "0"*(2-len(str(s)))+str(s) ; k[3] = "0"*(3-len(str(ms)))+str(ms)
050: else :
051: k[0] = "00" ; k[1] = "00"
052: k[2] = "00" ; k[3] = "000"
053: for i in A :
054: y = ",".join([":".join(i[:3])]+[i[3]])
055: C.append([y])
056: for i in range(0,len(C),2):
057: D.append("-->".join(C[i]+C[i+1])+"\n")
058: return D
059:
060: def Clean_time(A,B):
061: C = [] ; D = []
062: for i in range(len(A)):
063: A[i][0] = B[i]
064: for e in A:
065: if e[0] != '00:00:00,000-->00:00:00,000\n' :
066: C.append(e)
067: for i in range(len(C)):
068: C[i].insert(0,str(i+1)+"\n")
069: C[i].append("\n")
070: for k in C:
071: D += k
072: return D
073: def time_to_clock(All_ms):
074: All_ms >= 0
075: hr = All_ms // (60*60*1000)
076: All_ms -= hr * 60*60*1000
077: mn = All_ms // (60*1000)
078: All_ms -= mn *60*1000
079: s = All_ms // 1000
080: All_ms -= s*1000
081: ms = All_ms
082: hr = "0"*(2-len(str(hr)))+str(hr) ; mn = "0"*(2-len(str(mn)))+str(mn)
083: s = "0"*(2-len(str(s)))+str(s) ; ms = "0"*(3-len(str(ms)))+str(ms)
084: x = ":".join([hr,mn,s])+","+ms
085: return x
086: def change_to_read(alist):
087: for i in range(len(alist)):
088: k = alist[i][0]
089: ms_1 = time_to_clock(k[0])
090: ms_2 = time_to_clock(k[1])
091: alist[i][0][0] = ms_1
092: alist[i][0][1] = ms_2
093: for i in range(len(alist)):
094: k = " --> ".join(alist[i][0])+"\n"
095: alist[i][0] = k
096: alist[i].append("\n")
097: alist[i].insert(0,str(i+1)+"\n")
098: return
099: def merge_1(A,B,TA,TB,threshold):
100: x = [] ; y = [] ;w= []; Floud = True
101: for i in range(len(A)):
102: A[i][0] = TA[i][0]
103: A[i].insert(1,TA[i][1])
104: for k in range(len(B)):
105: B[k][0] = TB[k][0]
106: B[k].insert(1,TB[k][1])
107: for o in range(len(B)):
108: for u in range(len(A)):
109: if abs(B[o][0]-A[u][0]) <= threshold :
110: x.append([abs(B[o][0]-A[u][0]),u])
111: x.sort()
112: if len(x) == 0:
113: y.append(B[o])
114: Floud = False
115: if Floud == True :
116: z = x[0][1]
117: A[z] += B[o][2::]
118: if A[z][0] <= B[o][0] :
119: A[z][0] = B[o][0]
120: if A[z][1] <= B[o][1] :
121: A[z][1] = B[o][1]
122: x = []
123: Floud = True
124: A += y
125: A.sort()
126: for k in A:
127: w.append([[k[0],k[1]]]+k[2::])
128: return w
129: def time_1(alist):
130: x = [] ; y = [] ; A = easy_read_time(alist)
131: for k in A:
132: ms = int(k[3]) ; s = int(k[2])
133: mn = int(k[1]) ; hr = int(k[0])
134: All_ms = (hr*60*60*1000)+(mn*60*1000)+(s*1000)+ms
135: x.append(All_ms)
136: for i in range(0,len(x),2):
137: y.append([x[i],x[i+1]])
138: return y
139: def clean_list(alist):
140: x = "" ; y = [] ; z = [] ; D = [] ; fould = False
141: for i in range(len(alist)):
142: for k in range(1,len(alist[i])):
143: for e in range(len(alist[i][k])):
144: if alist[i][k][e] in ["<","[","{","("] :
145: fould = True
146: if alist[i][k][e] in [">","]","}",")"] :
147: fould = False
148: if fould == False :
149: if not alist[i][k][e] in [">","]","}",")"] :
150: x += alist[i][k][e]
151: alist[i][k] = x
152: x = ""
153: for i in range(len(alist)):
154: for k in range(1,len(alist[i])):
155: for e in range(len(alist[i][k])):
156: if not ("A" <= str(e) <= "z" or "0" <= str(e) <= "9" or "ก" <= str(e) <= "ฮ" or "ะ" <= str(e) <= "ไ" ) :
157: alist[i][k] = "\n"
158: for i in range(len(alist)):
159: for k in range(len(alist[i])):
160: if alist[i][k] != "\n" and alist[i][k] != "" and alist[i][k] != "- \n":
161: y.append(alist[i][k])
162: z.append(y)
163: y = []
164: for i in range(len(z)):
165: z[i].insert(0,str(i+1)+"\n")
166: z[i].append("\n")
167: for k in z:
168: D += k
169: return D
170:
171: # ------------------------------------------
172: def shift(file_in, time_shift, file_out):
173: file_1 = open(file_in, encoding='utf-8')
174: file_2 = open(file_out, 'w', encoding='utf-8')
175: A = change_file_to_list(file_1)
176: B = change_time(A,time_shift)
177: C = Clean_time(A,B)
178: for i in range(len(C)):
179: file_2.write(C[i])
180: file_1.close()
181: file_2.close()
182: return
183: # ------------------------------------------
184: def merge(base_file, merge_file, threshold, file_out):
185: file_1 = open(base_file, encoding='utf-8')
186: file_2 = open(merge_file, encoding='utf-8')
187: file_3 = open(file_out, "w",encoding='utf-8')
188: q = []
189: A = change_file_to_list(file_1)
190: B = change_file_to_list(file_2)
191: TA = time_1(A)
192: TB = time_1(B)
193: x = merge_1(A,B,TA,TB,threshold)
194: change_to_read(x)
195: for e in x :
196: q += e
197: for m in q:
198: file_3.write(m)
199: file_1.close()
200: file_2.close()
201: file_3.close()
202: return
203:
204: # ------------------------------------------
205: def clean(file_in, file_out):
206: file_1 = open(file_in, encoding='utf-8')
207: file_2 = open(file_out, "w",encoding='utf-8')
208: A = change_file_to_list(file_1)
209: x = clean_list(A)
210: for m in x:
211: file_2.write(m)
212: file_1.close()
213: file_2.close()
214: return
215:
216: # ------------------------------------------
6430156821 (15.0%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 246, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 97, in shift
time_i,i,time_f = j[1].split()
, stdout = , close_file = |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 246, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 97, in shift
time_i,i,time_f = j[1].split()
, stdout = , close_file = |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 246, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 97, in shift
time_i,i,time_f = j[1].split()
, stdout = , close_file = |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 45.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
00:01:55,500 --> 00:02:10,600
$$(AA
AA)
3
01:03:45,100 --> 01:03:48,100
A♪[11
1] |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 246, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 130, in merge
time_i,i,time_f = j[0].split()
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 246, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 130, in merge
time_i,i,time_f = j[0].split()
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430156821
003: # ------------------------------------------
004: def time(t):
005: h,m,ms = t.split(":")
006: h = int(h)
007: m = int(m)
008: ms_new = ""
009: for i in ms:
010: if i in "0123456789":
011: ms_new += i
012: ms_new = int(ms_new)
013: totaltime = (h*60*60*1000) + (m*60*1000) + ms_new
014: return totaltime
015:
016: def time_change(time,change):
017: h,m,ms = time.split(":")
018: h = int(h)
019: m = int(m)
020: ms_new = ""
021: for i in ms:
022: if i in "0123456789":
023: ms_new += i
024: ms_new = int(ms_new)
025: totaltime = (h*60*60*1000) + (m*60*1000) + ms_new
026: if totaltime + change > 0:
027: totaltime += change
028: else:
029: totaltime = 0
030: h_out = str(totaltime//(60*60*1000))
031: m_out = str((totaltime%(60*60*1000))//(60*1000))
032: ms_out = str((totaltime%(60*60*1000))%(60*1000))
033: if len(h_out) == 1:
034: h_out = "0"+h_out
035: if len(m_out) == 1:
036: m_out = "0"+m_out
037: ms_out = "0000"+ ms_out
038: out = h_out + ":" + m_out + ":" + ms_out[-5:-3] + ',' + ms_out[-3:]
039: return out
040:
041: def grouping_list(t):
042: result = []
043: sub = []
044: for c in range(len(t)):
045: if t[c] != "\n":
046: sub.append(t[c].strip())
047: else:
048: result.append(sub)
049: sub = []
050: result.append(sub)
051: return result
052:
053: def del_sym(word):
054: word_out = ''
055: if "[" in word:
056: ini = word.find('[')
057: final = word.find(']')
058: for i in range(len(word)):
059: if not (ini <= i <= final):
060: word_out += word[i]
061:
062: elif "{" in word:
063: ini = word.find('{')
064: final = word.find('}')
065: for i in range(len(word)):
066: if not (ini <= i <= final):
067: word_out += word[i]
068:
069: elif "(" in word:
070: ini = word.find('(')
071: final = word.find(')')
072: for i in range(len(word)):
073: if not (ini <= i <= final):
074: word_out += word[i]
075:
076: elif "<" in word:
077: ini = word.find('<')
078: final = word.find('>')
079: for i in range(len(word)):
080: if not (ini <= i <= final):
081: word_out += word[i]
082:
083: else:
084: word_out = word
085: return word_out
086: #------------------------------------------
087: def shift(file_in, time_shift, file_out):
088: temp = []
089: li_out = []
090: fin = open(file_in, "r",encoding='utf-8')
091: for line in fin:
092: temp.append(line)
093: fin.close()
094: li = grouping_list(temp)
095: li_new = li.copy()
096: for j in li:
097: time_i,i,time_f = j[1].split()
098: time_i = time_change(time_i,time_shift)
099: time_f = time_change(time_f,time_shift)
100: j[1] = time_i + " " + i + " " + time_f
101: if time_i == time_f:
102: li_new.remove(j)
103: for i in range(1,len(li_new)+1):
104: li_out.append([str(i)])
105: for j in range(len(li_new)):
106: li_out[j] += li_new[j][1:]
107: fout = open(file_out, "w",encoding='utf-8')
108: for sub in li_out:
109: for c in sub:
110: fout.write(c+'\n')
111: fout.write("\n")
112: fout.close()
113: return
114: #------------------------------------------
115: def merge(base_file, merge_file, threshold, file_out):
116: li_out = []
117: #base
118: base_temp = []
119: fin = open(base_file, "r",encoding='utf-8')
120: for line in fin:
121: base_temp.append(line)
122: fin.close()
123: list_base = grouping_list(base_temp)
124: for i in range(len(list_base)):
125: list_base[i] = list_base[i][1:]
126: list_base.sort()
127: fout = list_base.copy()
128: num_base = []
129: for j in list_base:
130: time_i,i,time_f = j[0].split()
131: num_base.append(time(time_i))
132:
133: #merge
134: merge_temp = []
135: fin = open(merge_file, "r",encoding='utf-8')
136: for line in fin:
137: merge_temp.append(line)
138: fin.close()
139: list_merge = grouping_list(merge_temp)
140: list_merge.sort()
141: for i in range(len(list_merge)):
142: list_merge[i] = list_merge[i][1:]
143: num_merge = []
144: for j in list_merge:
145: time_i,i,time_f = j[0].split()
146: num_merge.append(time(time_i))
147:
148: #เทียบเวลาตาม threshold
149: for i in range(len(num_merge)):
150: c = 0
151: for j in range(len(num_base)):
152: if abs(num_merge[i] - num_base[j]) <= threshold:
153: break
154: c += 1
155: if c != len(num_base):
156: fout[c] += list_merge[i][1:]
157: else:
158: fout.append(list_merge[i])
159: fout.sort()
160: for i in range(1,len(fout)+1):
161: li_out.append([str(i)])
162: for j in range(len(fout)):
163: li_out[j] += fout[j]
164:
165: #fileout
166: output = open(file_out, "w",encoding='utf-8')
167: for sub in li_out:
168: for c in sub:
169: output.write(c+'\n')
170: output.write("\n")
171: output.close()
172: return
173: #------------------------------------------
174: def clean(file_in, file_out):
175: #เริ่ม
176: temp = []
177: li_out = []
178: fin = open(file_in, "r",encoding='utf-8')
179: for line in fin:
180: temp.append(line)
181: fin.close()
182: li = grouping_list(temp)
183: licop = li.copy()
184: #---------------------------------
185: for sub_li in licop:
186: c = 0
187: for i in range(2,len(sub_li)):
188: sub_li[i] = del_sym(sub_li[i])
189: co = 0
190: for letter in sub_li[i]:
191: if letter.isalnum() is True:
192: break
193: co += 1
194: if co == len(sub_li[i]):
195: sub_li[i] = ""
196: c += len(sub_li[i])
197: if c == 0:
198: li.remove(sub_li)
199:
200: for i in range(1,len(li)+1):
201: li_out.append([str(i)])
202: for j in range(len(li)):
203: li_out[j] += li[j][1:]
204: fout = open(file_out, "w",encoding='utf-8')
205: for sub in li_out:
206: for c in sub:
207: fout.write(c+'\n')
208: fout.write("\n")
209: fout.close()
210: return
211: #------------------------------------------
6430158021 (50.0%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
merge -> 50.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
AAA
3
00:01:50,000 --> 00:01:56,000
111
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6430158021
003:
004: # ------------------------------------------
005:
006: def shift(file_in, time_shift, file_out):
007: fr = open(file_in, "r", encoding="utf-8")
008: fw = open(file_out, "w", encoding="utf-8")
009:
010: block = fr.read().split("\n\n")
011: index = 1
012:
013: for b in block[:-1]: # ignore last element
014: timeStamp, *sub = b.split("\n")[1:] # remove indexing number
015: t = []
016:
017: for time in timeStamp.strip().split(" --> "): # split timestamp between start and end
018: hh, mm, ss = time.split(":")
019: ss, ms = [int(i) for i in ss.split(",")]
020:
021: # convert to ms
022: ms += int(hh) * 3600 * 1000 + int(mm) * 60 * 1000 + int(ss) * 1000 + time_shift
023: ms = max(ms, 0)
024:
025: hh = ms // (3600 * 1000)
026: ms %= (3600 * 1000)
027:
028: mm = ms // (60 * 1000)
029: ms %= (60 * 1000)
030:
031: ss = ms // (1000)
032: ms %= (1000)
033:
034: t.append("{:02d}:{:02d}:{:02d},{:03d}".format(hh, mm, ss, ms))
035:
036: line = " --> ".join(t)
037:
038: if (line != "00:00:00,000 --> 00:00:00,000"):
039: line = "{}\n{}\n\n".format(str(index), "\n".join(line))
040:
041: fw.write(line)
042: index += 1
043:
044: fr.close(); fw.close()
045:
046: # ------------------------------------------
047:
048: def merge(base_file, merge_file, threshold, file_out):
049: bfr = open(base_file, "r", encoding="utf-8")
050: mfr = open(merge_file, "r", encoding="utf-8")
051: fw = open(file_out, "w", encoding="utf-8")
052:
053: # blocks = [
054: # [ms, timeStamp, [text, text, ...]],
055: # [ms, timeStamp, [text, text, ...]]
056: # ]
057: blocks = []
058:
059: baseBlock = bfr.read().split("\n\n")[:-1]; baseIndex = 0
060: mergeBlock = mfr.read().split("\n\n")[:-1]; mergeIndex = 0
061:
062: while (baseIndex < len(baseBlock) and mergeIndex < len(mergeBlock)):
063: timeStamp1, *sub1 = baseBlock[baseIndex].split("\n")[1:]; text1 = "\n".join(sub1)
064: timeStamp2, *sub2 = mergeBlock[mergeIndex].split("\n")[1:]; text2 = "\n".join(sub2)
065:
066: # get base file time in ms
067: hh1, mm1, ss1 = timeStamp1.split(" --> ")[0].split(":")
068: ss1, ms1 = [int(i) for i in ss1.split(",")]
069: ms1 += int(hh1) * 3600 * 1000 + int(mm1) * 60 * 1000 + int(ss1) * 1000
070:
071: # get merge file time in ms
072: hh2, mm2, ss2 = timeStamp2.split(" --> ")[0].split(":")
073: ss2, ms2 = [int(i) for i in ss2.split(",")]
074: ms2 += int(hh2) * 3600 * 1000 + int(mm2) * 60 * 1000 + int(ss2) * 1000
075:
076: # compare with previous block
077: if (len(blocks) > 0 and abs(blocks[-1][0] - ms2) <= threshold):
078: blocks[-1][2].append(text2)
079: mergeIndex += 1
080:
081: # if ms is in threshold, merge them
082: elif (abs(ms1 - ms2) <= threshold):
083: blocks.append([ms1, timeStamp1, [text1, text2]])
084: baseIndex += 1; mergeIndex += 1
085:
086: elif (ms1 < ms2):
087: blocks.append([ms1, timeStamp1, [text1]])
088: baseIndex += 1
089:
090: elif (ms2 < ms1):
091: blocks.append([ms2, timeStamp2, [text2]])
092: mergeIndex += 1
093:
094: for i, b in enumerate(blocks):
095: fw.write("{}\n{}\n{}\n\n".format(i + 1, b[1], "\n".join(b[2])))
096:
097: bfr.close(); mfr.close(); fw.close()
098:
099: # ------------------------------------------
100:
101: def clean(file_in, file_out):
102: fr = open(file_in, "r", encoding="utf-8")
103: fw = open(file_out, "w", encoding="utf-8")
104:
105: block = fr.read().split("\n\n")
106: index = 1
107: isInBracket = False
108:
109: for b in block[:-1]: # ignore last element
110: timeStamp, *sub = b.split("\n")[1:] # remove indexing number
111: s = []
112:
113: for line in sub: # iterate each line
114: isContainCharacter = False; l = ""
115:
116: for c in line: # iterate each character in a line
117: isContainCharacter = isContainCharacter or (c.isalnum() and not isInBracket)
118:
119: if (c == "(" or c == "{" or c == "[" or c == "<"):
120: isInBracket = True
121:
122: if (not isInBracket):
123: l += c
124:
125: elif (c == ")" or c == "}" or c == "]" or c == ">"):
126: isInBracket = False
127:
128: if (isContainCharacter):
129: s.append(l.strip())
130:
131: if (s != []):
132: line = "{}\n{}\n{}\n\n".format(index, timeStamp, "\n".join(s))
133:
134: fw.write(line)
135: index += 1
136:
137: fr.close(); fw.close()
138:
139: # ------------------------------------------
6430160221 (100.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 100.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = 1
['[']
0
2
['(']
0
3
['<']
0
['<']
0
4
['{']
0
, close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = 1
['[']
0
['[']
0
2
['(']
0
['(']
0
3
['<']
0
['<']
0
['<']
0
4
['{']
0
['<']
0
, close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = 1
['[']
0
['[']
0
2
['(']
0
['(']
0
3
['<']
0
['<']
0
['<']
0
4
['{']
0
['<']
0
, close_file = True |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = 1
['<']
0
2
['{']
0
['(']
0
3
['<']
0
['<']
0
4
5
['[']
0
, close_file = True |
merge -> 100.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
001: # Subtitle Tools
002: # 6430160221
003: def inter(k):
004: l = []
005: for i in range(len(k)):
006: if k[i] in ':,':
007: l.append(i)
008: return l
009: def timetomil(k):
010: t1,t2,t3 = inter(k)
011: a = int(k[0:t1])
012: b = int(k[t1+1:t2])
013: c = int(k[t2+1:t3])
014: d = int(k[t3+1:])
015: ss = (((a*3600)+(b*60)+(c))*1000)+d
016: return ss
017: def miltotime(k):
018: k = int(k)
019: a = k//(3600000)
020: k -= a*(3600000)
021: b = k//(60000)
022: k-=b*(60000)
023: c = k//(1000)
024: k-=c*1000
025: d = k
026: a = str(a)
027: b = str(b)
028: c = str(c)
029: d = str(d)
030: ll = [a,b,c,d]
031: for i in range(len(ll)):
032: if len(ll[i])<2:
033: ll[i] = '0'+ll[i]
034: if len(ll[3])<3:
035: ll[3] = '0'+ll[3]
036: return ll[0]+':'+ll[1]+':'+ll[2]+','+ll[3]
037: def getstring(now,time_shift):
038: time_shift = int(time_shift)
039: k = now
040: tt =[]
041: for j in range(len(k)):
042: if k[j] == ' ':
043: tt.append(j)
044: ts = k[0:tt[0]]
045: te = k[tt[1]+1:]
046: a = timetomil(ts)
047: b = timetomil(te)
048: a = max(0,a+time_shift)
049: b = max(0,b+time_shift)
050: return [a,b]
051:
052: #####################################################3
053: def shift(file_in, time_shift, file_out):
054: time_shift = int(time_shift)
055: fn = open(file_in,'r',encoding="utf8")
056: info = []
057: for i in fn:
058: info.append(i)
059: kun = []
060: kun.append(-1)
061: for i in range(len(info)):
062: if info[i]=='\n':
063: kun.append(i)
064: #####################################################3
065: ginfo = []
066: for i in range(len(kun)-1):
067: ginfo.append(info[kun[i]+1:kun[i+1]+1])
068: mo = []
069:
070: #####################################################3
071: for i in range(len(ginfo)):
072: ans = getstring(ginfo[i][1],time_shift)
073: if ans[0]==0 and ans[1]==0:
074: ginfo[i][1] = -1
075: else:
076: aa = miltotime(ans[0])
077: bb = miltotime(ans[1])
078: ginfo[i][1] = aa+' --> '+bb+'\n'
079:
080: for i in range(len(ginfo)):
081: if ginfo[i][1]!=-1:
082: mo.append(ginfo[i])
083: for i in range(len(mo)):
084: mo[i][0] = str(i+1)+'\n'
085:
086: fn.close()
087: fout = open(file_out,'w',encoding = 'utf8')
088: for i in mo:
089: for j in i:
090: fout.write(j)
091: fout.close()
092: #####################################################3
093:
094: def merge(base_file, merge_file, threshold, file_out):
095: fn1 = open(base_file,'r',encoding="utf8")
096: fn2 = open(merge_file,'r',encoding="utf8")
097: info = []
098: for i in fn1:
099: info.append(i)
100: kun = []
101: kun.append(-1)
102: for i in range(len(info)):
103: if info[i]=='\n':
104: kun.append(i)
105: eginfo = []
106: for i in range(len(kun)-1):
107: eginfo.append(info[kun[i]+1:kun[i+1]+1])
108: mo = []
109: time_shift = 0
110: #for i in eginfo:
111: # print(i)
112: ##############################################################
113: info = []
114: for i in fn2:
115: info.append(i)
116: kun = []
117: kun.append(-1)
118: for i in range(len(info)):
119: if info[i]=='\n':
120: kun.append(i)
121: tginfo = []
122: for i in range(len(kun)-1):
123: tginfo.append(info[kun[i]+1:kun[i+1]+1])
124: mo = []
125: time_shift = 0
126: #for i in tginfo:
127: # print(i)
128: threshold = int(threshold)
129: #######################################################
130: utt = []
131: for i in tginfo:
132: utt.append(i[0:2]+['t'])
133: uet = []
134: for i in eginfo:
135: uet.append(i[0:2]+['e'])
136: ########################################################
137: for i in range(len(utt)):
138: utt[i]+=getstring(utt[i][1],0)
139: utt[i].remove(utt[i][1])
140: for i in range(len(uet)):
141: uet[i]+=getstring(uet[i][1],0)
142: uet[i].remove(uet[i][1])
143: ut = []
144: ue = []
145: for i in range(len(utt)):
146: ut.append(utt[i][2:]+[utt[i][1]]+[int(utt[i][0].strip('\n'))])
147: for i in range(len(uet)):
148: ue.append(uet[i][2:]+[uet[i][1]]+[int(uet[i][0].strip('\n'))])
149: ue.sort()
150: ut.sort()
151: vec = []
152: for i in range(len(ut)+len(ue)):
153: vec.append([-1]*2)
154: mm = list(ue)+list(ut)
155: mm.sort()
156:
157: dis = [0]*(len(ut)+200)
158: for i in range(len(mm)):
159: k = list(mm[i])
160: op = []
161: if mm[i][2]=='e':continue
162: mn = threshold+20
163: for j in range(-3,4):
164: a = i+j
165: if a<0:continue
166: if a>=len(mm):continue
167: if j==0:continue
168: if mm[a][2]=='t':continue
169: if abs(mm[a][0]-mm[i][0])>threshold:continue
170: op.append([abs(mm[a][0]-mm[i][0]),mm[a][2],mm[a][3],mm[i][3]])
171: op.sort()
172: if len(op)>0:op = op[0]
173: if len(op)>1:
174: dis[op[3]] = 1
175: vec[op[2]].append(op[3])
176:
177: last = []
178: for i in range(len(mm)):
179: if mm[i][2]=='t' :
180: if dis[mm[i][3]]==0:
181: last.append(['t',mm[i][3]])
182: else:
183: num = mm[i][3]
184: ppp = []
185: if len(vec[num])==2:ppp+=(['e',mm[i][3]])
186: else:ppp+=(['e',mm[i][3]])
187: for j in vec[num]:
188: if j!=-1:
189: ppp+=([j])
190: last.append(ppp)
191: eginfo = [0]+eginfo
192: tginfo = [0]+tginfo
193:
194: for i in last:
195: if len(i)>2:
196: num = i[1]
197: eginfo[num] = eginfo[num][:len(eginfo[num])-1]
198: for j in vec[num]:
199: if j !=-1:
200: eginfo[num]+=tginfo[j][2:len(tginfo[j])-1]
201: eginfo[num]+=['\n']
202: fn1.close()
203: fn2.close()
204: ##########################################################
205: cc = 0
206: fout = open(file_out,'w',encoding ='utf8')
207: for i in last:
208: cc+=1
209: num = i[1]
210: if i[0]=='e':
211: eginfo[num][0] = str(cc)+'\n'
212: k = eginfo[num]
213: for i in k:
214: fout.write(i)
215: else:
216: tginfo[num][0] = str(cc)+'\n'
217: k = tginfo[num]
218:
219: for i in k:
220: fout.write(i)
221: fout.close()
222:
223: def clean(file_in, file_out):
224: fn = open(file_in,'r',encoding="utf8")
225: info = []
226: for i in fn:
227: info.append(i)
228: kun = []
229: kun.append(-1)
230: for i in range(len(info)):
231: if info[i]=='\n':
232: kun.append(i)
233: #####################################################3
234: ginfo = []
235: for i in range(len(kun)-1):
236: ginfo.append(info[kun[i]+1:kun[i+1]+1])
237: #('\n')
238:
239: l = []
240: for i in ginfo:
241: l.append(''.join(i[2:]))
242:
243: #(l)
244: o = '[{(<'
245: e = ']})>'
246: ans = []
247: #('\n')
248: cop = 0
249: for i in l:
250: cop +=1
251: print(cop)
252: print('\n')
253: s = []
254: tick = []
255: for j in i:
256: if j not in o and j not in e:
257: s.append(j)
258: elif j in o:
259: s.append(j)
260: tick.append(j)
261: elif j in e:
262: if len(tick)==0:
263: s.append(j)
264: continue
265: if len(s)==0:
266: s.append(j)
267: continue
268: if o.index(tick[-1])==e.index(j):
269: while(s[-1]!=tick[-1]):
270: s.pop()
271: s.pop()
272: print(tick)
273: tick.pop()
274: print(len(tick))
275: else:
276: s.append(j)
277: s = ''.join(s)
278:
279: ans.append(s)
280: last = []
281:
282: cnt = 0
283: for i in range(len(ans)):
284:
285: kk = [-1]
286: lam = []
287: for j in range(len(ans[i])):
288: if ans[i][j]=='\n':
289: kk.append(j)
290: for k in range(len(kk)-1):
291: lam.append(ans[i][kk[k]+1:kk[k+1]+1])
292: last.append(lam)
293: check = [[0]]
294:
295: for i in last:
296: cc = []
297: re = False
298: for j in i:
299: tak = False
300: ch = j[:len(j)-1]
301: for k in ch:
302: if k.isalnum():tak=True
303: re = re|tak
304: cc.append(tak)
305: cc.append(re)
306: check.append(cc)
307: last = [0] +last
308: #(check)
309: #(last)
310: real = []
311:
312: for i in range(len(last)):
313: if i==0:continue
314: if check[i][-1]==False:
315: continue
316: pup = []
317: pup.append(ginfo[i-1][1])
318: for j in range(len(last[i])):
319: if check[i][j]==True:
320: pup.append(last[i][j])
321: pup.append('\n')
322: real.append(pup)
323: #(real)
324:
325: fn.close()
326: ##################################################################
327: fout = open(file_out,'w',encoding = 'utf8')
328: cc = 0
329: for i in real:
330: cc+=1
331: fout.write(str(cc)+'\n')
332: for j in i:
333: fout.write(j.strip()+'\n')
334:
335: fout.close()
6431118121 (21.33%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 154, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 154, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 154, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 64.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = False |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = False |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = False |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = False |
2
01:03:45,100 --> 01:03:48,100
A♪[11
3 |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 154, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 154, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6431118121
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: f_in = open(file_in,"r")
007: file_out = ""
008: for line in f_in:
009: if line.count(':') == 0:
010: file_out += line
011: else:
012: x1 = line.replace(',','')
013: t1,t2 = x1.split("-->")
014: t1 = t1.split(':')
015: t2 = t2.split(':')
016: time1 = 0
017: time1 += int(t1[0])*3600
018: time1 += int(t1[1])*60
019: time1 += int(t1[2])
020: time1 += time_shift
021: if time1 <= 0:
022: time1 = 0
023: time2 = 0
024: time2 += int(t2[0])*3600
025: time2 += int(t2[1])*60
026: time2 += int(t2[2])
027: time2 += time_shift
028: if time2 <= 0:
029: time2 = 0
030: t1 = set_time(time1)
031: t2 = set_time(time2)
032: file_out += t1 +' --> '+ t2+'\n'
033: return file_out
034:
035: # ------------------------------------------
036: def merge(base_file, merge_file, threshold, file_out):
037:
038: return
039: # ------------------------------------------
040: def clean(file_in, file_out):
041: f_in = open(file_in,"r")
042: r = '()[]{}<>'
043: c = 0
044: num = 0
045: index = []
046: indext = []
047: out = ''
048: for line in f_in:
049: for i in line:
050: if i in r : #find line[i] in '()[]{}<>'
051: c += 1
052: index.append(line.find(i))
053: if i.isalnum() == True: #fine char
054: num += 1
055: if c >= 2: #กรณีมีเครื่องหมาย[]
056: x = line[(index[0]):(index[1])+1]
057: w = line.replace(x,'')
058: if c == 4:
059: for p in w:
060: if p in r:
061: indext.append(w.find(p))
062: x2 = w[(indext[0]):(indext[1])+1]
063: w = w.replace(x2,'')
064: wn = 0
065: for i in w: #check must have char befor clean
066: #if 'a'<= i.lower() <= 'z' or '0' <= i <= '9' or i in 'กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรลวศษสหฬอฮ':
067: if i.isalnum() == True:
068: wn += 1
069: if wn != 0: #have char
070: out += w
071: elif num != 0 : #check must have char
072: if '0' < line[0] <= '9' and len(line) <= 25:
073: out += '$'+'\n'
074: elif line.count(':') == 0:
075: out += line
076: else:
077: out += line
078: c = 0
079: num = 0
080: index = []
081: indext = []
082: out = out.split('$')
083: ma = ''
084: g = 0
085:
086: for i in range(len(out)):
087: if out[i].count('\n') != 2 and out[i].count('\n') != 0:
088: g += 1
089: ma += '\n'+str(g)
090: ma += out[i]
091: ma = ma.strip()
092: file = open(file_out,'w')
093: file.write(ma)
094: return file_out
095: # ------------------------------------------
096: def set_time(x):
097: ma = ''
098: d = ''
099: s = x%(60000)
100: ss = s//1000
101: ss = '0'*(2-len(str(ss)))+str(ss)
102: sss = s%1000
103: sss = '0'*(3-len(str(sss)))+str(sss)
104: d += str(ss)+','+str(sss)
105:
106: if x//(60000) >= 1:
107: m = x//(60000)
108: m = '0'*(2-len(str(m)))+str(m)
109: else:
110: m = 0
111: m = '0'*(2-len(str(m)))+str(m)
112: if x//(60000*3600) >= 1:
113: h = x//(60000*3600)
114: h = '0'*(2-len(str(h)))+str(h)
115: else:
116: h = 0
117: h = '0'*(2-len(str(h)))+str(h)
118: ma = h+':'+m+':'+d
119: return ma
6431132921 (0.0%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = NameError("name 'millisec' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 16, in shift
time1 = millisec(t1)
, stdout = , close_file = |
|
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = NameError("name 'millisec' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 16, in shift
time1 = millisec(t1)
, stdout = , close_file = |
|
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = NameError("name 'millisec' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 16, in shift
time1 = millisec(t1)
, stdout = , close_file = |
|
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 0.0%
[30, 'clean_1.srt', 'out.srt']
err = NameError("name 'g' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 107, in clean
m += str(g)
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = NameError("name 'g' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 107, in clean
m += str(g)
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = NameError("name 'g' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 107, in clean
m += str(g)
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = NameError("name 'g' is not defined")
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 107, in clean
m += str(g)
, stdout = , close_file = |
|
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 206, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = IndexError('list index out of range')
File "z:\HW08\_student.py", line 205, in __test_
ret = func(*args)
File "z:\HW08\_student.py", line 71, in merge
k1 = minusnear(ts1, ts2, threshold, ws1, ws2)
File "z:\HW08\_student.py", line 128, in minusnear
ch += ws1[i] + ws2[j]
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: #6431132921
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: f_in = open(file_in, "r")
007: file_out = ""
008: k = 1
009: c = 0
010: for line in f_in:
011: if line.count(':') != 0:
012: x1 = line.replace(',', '')
013: t1, t2 = x1.split("-->")
014: t1 = t1.split(':')
015: t2 = t2.split(':')
016: time1 = millisec(t1)
017: time1 += time_shift
018: if time1 <= 0:
019: time1 = 0
020: time2 = millisec(t2)
021: if time2 <= 0:
022: time2 = 0
023: if time2 != 0 and time2 != 0:
024: t1 = set_time(time1)
025: t2 = set_time(time2)
026: file_out += t1 + ' --> ' + t2 + '\n'
027: k = 1
028: else:
029: k = 0
030: elif k == 1:
031: if '0' <= line <= '9':
032: c += 1
033: file_out += str(c) + '\n'
034: else:
035: file_out += line
036:
037: return file_out
038:
039: def set_time(x):
040: ma = ''
041: d = ''
042: s1 = x % (60000)
043: s2 = s1 // 1000
044: s2 = '0'*(2 - len(str(s2))) + str(s2)
045: s3 = s % 1000
046: s3 = '0'*(3 - len(str(s3))) + str(s3)
047: d += str(s2) + ',' + str(s3)
048:
049: if x // (60000) >= 1:
050: m = x // (60000)
051: m = '0'*(2 - len(str(m))) + str(m)
052: else:
053: m = 0
054: m = '0'*(2 - len(str(m))) + str(m)
055: if x // (60000*3600) >= 1:
056: h = x // (60000*3600)
057: h = '0'*(2 - len(str(h))) + str(h)
058: else:
059: h = 0
060: h = '0'*(2 - len(str(h))) + str(h)
061: ma = h + ':' + m + ':' + d
062: return ma
063:
064: # ------------------------------------------
065: def merge(base_file, merge_file, threshold, file_out):
066: s1=open(base_file, encoding="utf8")
067: s2=open(merge_file, encoding="utf8")
068: ts1,ws1=filetostamp(s1)
069: ts2,ws2=filetostamp(s2)
070: out=''
071: k1 = minusnear(ts1, ts2, threshold, ws1, ws2)
072: # ------------------------------------------
073: def clean(file_in, file_out):
074: s = open(file_in, encoding="utf8")
075: wrout = ""
076: c = 0
077: ka = 1
078: numcount = 0
079: f = 0
080: abchar = '()[]{}<>'
081:
082: for l in s:
083: for i in l:
084: if i in abchar:
085: c += 1
086: if 'a' <= i.lower() <= 'z' or '0' <= i <= '9':
087: numcount += 1
088:
089: if c == 2 or numcount == 0:
090: ka = 0
091:
092: if ka == 1:
093: if '0' <= l <= '9' and len(l) <= 10:
094: f += 1
095: wrout += str('*') + '\n'
096: else:
097: wrout += l
098: numcount = 0
099: c = 0
100: ka = 1
101: wrout = wrout.strip().split('*')
102: m = ''
103: gl = 0
104: for i in range(len(wrout)):
105: if wrout[i].count('\n') == 3:
106: gl += 1
107: m += str(g)
108: m += wrout[i]
109: fl = open(file_out, 'w', encoding="utf8")
110: fl.write(m)
111: return file_out
112: def minusnear(ts1, ts2, threshold, ws1, ws2):
113: outs=[]
114: ch=''
115: count = 1
116: h=''
117: for i in range(len(ts1)):
118: for j in range(len(ts2)):
119: pov1 = ts1[i][0].strip().split(':')
120: pov2 = ts2[j][0].strip().split(':')
121: v1 = millisecfront(ts1[i][0])
122: v2 = millisecfront(ts2[j][0])
123: m1 = millisecback(ts1[i][1])
124: m2 = millisecback(ts2[j][1])
125: if abs(v2 - v1) < threshold and pov1[0] == pov2[0] and pov1[1] == pov2[1]:
126: if v2 > v1 and m1 > m2:
127: ch += ts1[i][0] + '-->' + ts1[i][1]
128: ch += ws1[i] + ws2[j]
129: elif v2 > v1 and m1 < m2:
130: ch += ts1[i][0] + '-->' + ts2[j][1]
131: ch += ws1[i] + ws2[j]
132: elif v2 < v1 and m1 > m2:
133: ch += ts2[j][0] + '-->' + ts1[i][1]
134: ch += ws1[i] + ws2[j]
135: elif v2 < v1 and m1 < m2:
136: ch += ts2[j][0] + '-->' + ts2[j][1]
137: ch += ws1[i] + ws2[j]
138: if ch != '':
139: outs.append(ch)
140: ch=''
141: outs.sort()
142: r = set(outs)
143: for i in r:
144: h += str(count) +'\n'
145: h += i + '\n'
146: count += 1
147: return outs
148: def millisecfront(time):
149: pov = time.strip().split(':')
150: time1 = 0
151: time1 += int(pov[2].replace(',', ''))
152: return time1
153: def millisecback(time):
154: pov=time.strip().split(':')
155: time1 = 0
156: time1 += int(pov[0])*3600
157: time1 += int(pov[1])*60
158: time1 += int(pov[2].replace(',', ''))
159: return time1
160: def filetostamp(file):
161: timestamp=[]
162: wordstamp=[]
163: for line in file:
164: if line.count(':') > 1 :
165: p = line.split('-->')
166: timestamp.append(p)
167: elif line[0] not in '1234567890' and line[-1:] == "\n" and line != '\n':
168: wordstamp.append(line)
169: return timestamp, wordstamp
170:
171: # ------------------------------------------
6431214721 (0.0%)
shift -> 0%
File "", line 73
if len(subtitles) > 0 \
^
IndentationError: unexpected indent
|
clean -> 0%
File "", line 73
if len(subtitles) > 0 \
^
IndentationError: unexpected indent
|
merge -> 0%
File "", line 73
if len(subtitles) > 0 \
^
IndentationError: unexpected indent
|
001: # Subtitle Tools
002: # 6431214721
003:
004: # ------------------------------------------
005: # Shift
006:
007: def shift(file_in, time_shift, file_out):
008:
009: f = open(file_in, 'r')
010:
011: subtitles_lines = read_subtitle_file(file_in)
012:
013:
014: subtitles = []
015:
016:
017: for i in range(len(subtitles_lines)):
018: subtitle = decrease_time(subtitles_lines[i], time_shift)
019:
020:
021: if subtitle['t_start'] == subtitle['t_stop'] and 0 == subtitle['t_start']:
022: continue
023: else:
024: subtitles.append(subtitle)
025:
026: write_subtitle(subtitles, file_out)
027:
028: # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
029: # Merge
030:
031: def merge(base_file, merge_file, threshold, file_out):
032:
033:
034: from operator import itemgetter
035:
036:
037: base_subtitle_lines = read_subtitle_file(base_file)
038: base_subtitles = []
039: for sub in base_subtitle_lines:
040: base_subtitles.append({
041: 't_start': sub['t_start'],
042: 't_stop': sub['t_stop'],
043: 'message': sub['message'],
044: 'mode': 'base'
045: })
046:
047: merge_file_lines = read_subtitle_file(merge_file)
048: merge_subtitles = []
049: for sub in merge_file_lines:
050: merge_subtitles.append({
051: 't_start': sub['t_start'],
052: 't_stop': sub['t_stop'],
053: 'message': sub['message'],
054: 'mode': 'merge'
055: })
056:
057:
058: subtitles = base_subtitles + merge_subtitles
059:
060:
061: merged_subtitles = []
062:
063:
064: subtitles = sorted(subtitles, key=itemgetter('t_start'))
065:
066:
067: is_element_remaining = True
068:
069: while is_element_remaining:
070:
071: subtitle = subtitles.pop(0)
072:
073: if len(subtitles) > 0 \
074: and subtitle['t_start'] - subtitles[0]['t_start'] <= threshold \
075: and subtitle['mode'] == 'base' and subtitles[0]['mode'] == 'merge':
076:
077:
078: next_subtitle = subtitles.pop(0)
079:
080:
081: subtitle['message'] = subtitle['message'] + "\n" + next_subtitle['message']
082:
083:
084: merged_subtitles.append(subtitle)
085:
086:
087: if len(subtitles) == 0:
088: is_element_remaining = False
089:
090: write_subtitle(merged_subtitles, file_out)
091:
092: # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
093: # Clean
094:
095: def clean(file_in, file_out):
096: import re
097:
098:
099: subtitle_lines = read_subtitle_file(file_in)
100:
101:
102: lines = []
103: for subtitle in subtitle_lines:
104: message = subtitle['message'].replace("\n", "|")
105: message = re.sub("[\(\[\<\{].*?[\)\]\>\}]", "", message)
106: message = ''.join([c for c in message if c.isalnum()])
107:
108: if len(message.strip()) != 0:
109: lines.append({
110: 't_start': subtitle['t_start'],
111: 't_stop': subtitle['t_stop'],
112: 'message': message.replace("|", "\n")
113: })
114:
115: write_subtitle(lines, file_out)
6431215321 (75.0%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 45.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA (AAAA)AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
1
00:01:45,100 --> 00:01:48,100
AAAA.
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪. |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA (AAAA)AAA.
(BBBB)BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA (AAAA)AAA.
(BBBB)
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
00:01:55,500 --> 00:02:10,600
$$(AA
AA)
3
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 80.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = , stdout = , close_file = True |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = , stdout = , close_file = True |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
DDD
6
00:01:56,998 --> 00:01:56,999
444
DDD
7
00:01:56,999 --> 00:04:22,100
555
DDD |
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # 6431215321
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: filein = open(file_in,"r", encoding='utf-8')
007: out = ''
008: for line in filein:
009: if ' -->' in line :
010: t1,t2 = line[:-1].split(' -->')
011: t1 = t1.strip()
012: t2 = t2.strip()
013: line = timechang(t1,t2,time_shift)
014: out += line
015: out = out.split("\n\n")
016: for i in range(len(out)):
017: j = out[i].find(":")
018: if out[i][j-2:j+27] == '00:00:00,000 --> 00:00:00,000' :
019: out[i] = ''
020: new = newtext(out)+'\n\n'
021: filein.close()
022: fileout = open(file_out,"w", encoding='utf-8')
023: fileout.write(new)
024: fileout.close()
025:
026: def findtime(newt1):
027: if 0 < newt1/(60*60*1000) < 1 :
028: h1 = 0
029: else : h1 = newt1//(60*60*1000)
030: newt1 -= h1*60*60*1000
031: if 0 < newt1/(60*1000) < 1 :
032: m1 = 0
033: else : m1 = newt1//(60*1000)
034: newt1 -= m1*60*1000
035: if 0 < newt1/(1000) < 1 :
036: s1 = 0
037: else : s1 = newt1//(1000)
038: newt1 -= s1*1000
039: ms1 = newt1
040: if h1 < 0 or m1 < 0 or s1 < 0 or ms1 < 0 :
041: h1 = 0 ; m1 = 0 ; s1 = 0 ; ms1 = 0
042: return '0'*(2-len(str(h1)))+str(h1)+':'+'0'*(2-len(str(m1)))+str(m1)+':'+'0'*(2-len(str(s1)))+str(s1)+','+'0'*(3-len(str(ms1)))+str(ms1)
043:
044: def whattime(t1):
045: h1,m1,s1 = t1.split(":")
046: s1 = "".join(s1.split(","))
047: newt1 = int(h1)*60*60*1000 + int(m1)*60*1000 + int(s1)
048: return newt1
049:
050: def timechang(t1,t2,dt):
051: newt1 = whattime(t1) + dt
052: newt2 = whattime(t2) + dt
053: time1 = findtime(newt1)
054: time2 = findtime(newt2)
055: return time1+' --> '+time2+'\n'
056:
057: def newtext(out):
058: new = []
059: n = 0
060: for e in range(len(out)) :
061: if out[e] not in ' '*10000:
062: n += 1
063: new += [str(n)+str(out[e][len(str(e+1)):])]
064: new = "\n\n".join(new)
065: return new
066: # ------------------------------------------
067: def merge(base_file, merge_file, threshold, file_out):
068: bfile = open(base_file,"r", encoding='utf-8')
069: mfile = open(merge_file,"r", encoding='utf-8')
070: base = ''
071: merge = ''
072: for b in bfile :
073: base += (b)
074: for m in mfile :
075: merge += (m)
076: base = base.strip().split('\n\n')
077: merge = merge.strip().split('\n\n')
078: genbase = []
079: genmerge = []
080: for i in merge :
081: notimetext = i.split("\n")
082: genmerge.append(notimetext)
083: for k in base :
084: notimetext = k.split("\n")
085: genbase.append(notimetext)
086: n = 0
087: gensum = []
088: for i in genbase :
089: for e in genmerge :
090: tmerge = whattime(e[1][0:12:1])
091: if abs(tmerge - whattime(i[1][0:12:1])) <= threshold :
092: if "\n".join(i[2:-1]) in ' ' :
093: gensum.append([i[0],e[0],whattime(i[1][0:12:1]),i[1][12:29],i[-1],"\n".join(e[2:])])
094: else :
095: gensum.append([i[0],e[0],whattime(i[1][0:12:1]),i[1][12:29],"\n".join(i[2:-1])+'\n'+i[-1],"\n".join(e[2:])])
096: allgen = []
097: for i in genbase :
098: n = 0
099: for k in gensum :
100: if i[0] == k[0] :
101: n += 1
102: if n == 0 :
103: if "\n".join(i[2:-1]) in ' ' :
104: allgen.append([whattime(i[1][0:12:1]),i[1][12:29],i[-1]])
105: else :
106: allgen.append([whattime(i[1][0:12:1]),i[1][12:29],"\n".join(i[2:-1])+'\n'+i[-1]])
107: for i in genmerge :
108: n = 0
109: for k in gensum :
110: if i[0] == k[1] :
111: n += 1
112: if n == 0 :
113: if "\n".join(i[2:-1]) in ' ' :
114: allgen.append([whattime(i[1][0:12:1]),i[1][12:29],i[-1]])
115: else :
116: allgen.append([whattime(i[1][0:12:1]),i[1][12:29],"\n".join(i[2:-1])+'\n'+i[-1]])
117: for e in range(len(gensum)) :
118: allgen.append(gensum[e][-4:])
119: allgen.sort()
120: n = 0
121: while n+1 < len(allgen):
122: if allgen[n][0] == allgen[n+1][0] :
123: allgen[n][-1] += '\n' + ('\n'.join(allgen[n+1][3::]))
124: allgen.remove(allgen[n+1])
125: n -= 1
126: n += 1
127: for e in range(len(allgen)) :
128: allgen[e].insert(0,str(e+1))
129: allgen[e][1] = findtime(allgen[e][1])
130: allgen[e][1] = ''.join([allgen[e][1],allgen[e][2]])
131: allgen[e].remove(allgen[e][2])
132: allgen[e] = '\n'.join(allgen[e])
133: allgen = '\n\n'.join(allgen) +'\n\n'
134: bfile.close()
135: mfile.close()
136: fileout = open(file_out,"w", encoding='utf-8')
137: fileout.write(allgen)
138: fileout.close()
139:
140: # ------------------------------------------
141: def clean(file_in, file_out):
142: filein = open(file_in,"r", encoding='utf-8')
143: new = ''
144: for line in filein :
145: new += line
146: new = new.strip().split('\n\n')
147: new = clean1(new)
148: new = clean2(new) + '\n\n'
149: filein.close()
150: fileout = open(file_out,"w", encoding='utf-8')
151: fileout.write(new)
152: fileout.close()
153:
154: def clean1(filein) :
155: out = []
156: for line in filein :
157: while '<' in line and '>' in line :
158: j = line.find('<')
159: k = line.find('>',j)
160: line = line[0:j]+line[k+1:]
161: while '[' in line and ']' in line :
162: j = line.find('[')
163: k = line.find(']',j)
164: line = line[0:j]+line[k+1:]
165: while '{' in line and '}' in line :
166: j = line.find('{')
167: k = line.find('}',j)
168: line = line[0:j]+line[k+1:]
169: out.append(line)
170: return out
171:
172: def clean2(filein) :
173: out = []
174: for e in filein :
175: line = e.split('\n')
176: n = 0
177: for i in line[2] :
178: if i.isalnum() : n += 1
179: if n == 0 : line = ''
180: line = "\n".join(line)
181: out.append(line)
182: out = newtext(out)
183: return out
184: # ------------------------------------------
6431420621 (20.0%)
shift -> 0.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
1
00:03:20,600 --> 00:03:23,600
AAAA AAAA AAA.
2
00:03:31,000 --> 00:03:46,100
AAAA AAAA AAA.
3
01:01:25,600 --> 01:01:43,600
AAAA AAAA AAA.
4
01:02:40,600 --> 01:02:53,600
AAAA AAAA AAA. |
1
00:00:09,600 --> 00:00:12,600
AAAA AAAA AAA.
2
00:00:20,000 --> 00:00:35,100
AAAA AAAA AAA.
3
00:58:14,600 --> 00:58:32,600
AAAA AAAA AAA.
4
00:59:29,600 --> 00:59:42,600
AAAA AAAA AAA. |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
1
00:03:55,600 --> 00:03:58,600
AAAA AAAA AAA.
2
00:04:06,000 --> 00:04:21,100
AAAA AAAA AAA.
3
01:02:00,600 --> 01:02:18,600
AAAA AAAA AAA.
4
01:03:15,600 --> 01:03:28,600
AAAA AAAA AAA. |
1
00:00:00,000 --> 00:00:00,100
AAAA AAAA AAA.
2
00:57:39,600 --> 00:57:57,600
AAAA AAAA AAA.
3
00:58:54,600 --> 00:59:07,600
AAAA AAAA AAA. |
clean -> 60.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA. [AAAA AAA]
[BBBBB]
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA (AAAA)AAA.
(BBBB)
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
{an03} AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
00:01:55,500 --> 00:02:10,600
{an03}$$
3
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0.0%
[30, 'merge_b1.srt', 'merge_m1.srt', 1, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 453, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:01:35,100 --> 00:01:48,100
AAA
2
00:01:45,100 --> 00:01:48,100
111
3
00:01:55,500 --> 00:02:10,600
222
BBB
4
00:59:40,100 --> 01:00:08,100
333
33
5
00:59:50,100 --> 01:00:08,100
CCC
CC
6
01:01:05,100 --> 01:01:18,100
444
44
DDD
DD |
[70, 'merge_b2.srt', 'merge_m2.srt', 500, 'out.srt']
err = FileNotFoundError("No such file or directory: 'out.srt'")
File "z:\HW08\_student.py", line 453, in __test_
close_file, fout_content = f.content(args[-1])
File "z:\HW08\redirect_file.py", line 38, in content
raise FileNotFoundError(f"No such file or directory: '{filename}'")
, stdout = , close_file = |
|
1
00:01:20,499 --> 00:01:22,000
XXX
2
00:01:49,499 --> 00:01:49,500
YYY
3
00:01:50,000 --> 00:01:56,000
111
AAA
4
00:01:55,001 --> 00:01:56,000
222
BBB
CCC
5
00:01:56,997 --> 00:01:56,998
333
6
00:01:56,998 --> 00:01:56,999
444
7
00:01:56,999 --> 00:04:22,100
555
DDD |
001: # Subtitle Tools
002: # Your ID 6431420621
003:
004: # ------------------------------------------
005: def shift(file_in, time_shift, file_out):
006: #gather info from file.srt
007: fin = open(file_in, "r" , encoding="utf-8")
008: info_fin1 = ''
009: for line in fin :
010: info_fin1 += line
011: fin.close()
012: info_fin2 = info_fin1.split('\n\n')
013: info_fin3 = []
014: for i in range(len(info_fin2)) :
015: info_septem1 = []
016: info_septem1 = info_fin2[i].split('\n')
017: info_fin3.append(info_septem1)
018: #shift time
019: for i in range(len(info_fin3)-1) :
020: Time0 = info_fin3[i][1].split(' --> ')
021: Time1_0 = Time0[0].split(',')
022: Time1_1 = Time1_0[0].split(':')
023: millisec1 = int(Time1_0[1])
024: sec1 = int(Time1_1[2])*1000
025: min1 = int(Time1_1[1])*1000*60
026: hour1 = int(Time1_1[0])*1000*60*60
027: Time2_0 = Time0[1].split(',')
028: Time2_1 = Time2_0[0].split(':')
029: millisec2 = int(Time2_0[1])
030: sec2 = int(Time2_1[2])*1000
031: min2 = int(Time2_1[1])*1000*60
032: hour2 = int(Time2_1[0])*1000*60*60
033: TimeS_BS = 0
034: TimeS_BS += hour1+min1+sec1+millisec1
035: TimeF_BS = 0
036: TimeF_BS += hour2+min2+sec2+millisec2
037: TimeS_AS = TimeS_BS - time_shift
038: TimeF_AS = TimeF_BS - time_shift
039: if TimeS_AS < 0 :
040: TimeS_AS = 0
041: if TimeF_AS < 0 :
042: TimeF_AS = 0
043: if TimeS_AS == 0 and TimeF_AS == 0 :
044: pass
045: else :
046: #Time start
047: dt1 = TimeS_AS
048: dh1 = dt1 // (60*60*1000)
049: dt1 -= dh1*60*60*1000
050: dm1 = dt1 // (60*1000)
051: dt1 -= dm1*60*1000
052: ds1 = dt1 // (1000)
053: dt1 -= ds1*1000
054: dms1 = dt1
055: TimeS_AS1 =''
056: dh1 = str(dh1)
057: dm1 = str(dm1)
058: ds1 = str(ds1)
059: dms1 = str(dms1)
060: if len(dh1) == 2 :
061: TimeS_AS1 += dh1+':'
062: elif len(dh1) == 1 :
063: TimeS_AS1 += '0'+dh1+':'
064: if len(dm1) == 2 :
065: TimeS_AS1 += dm1+':'
066: elif len(dm1) == 1 :
067: TimeS_AS1 += '0'+dm1+':'
068: if len(ds1) == 2 :
069: TimeS_AS1 += ds1+','
070: elif len(ds1) == 1 :
071: TimeS_AS1 += '0'+ds1+','
072: if len(dms1) == 3 :
073: TimeS_AS1 += dms1
074: elif len(dms1) == 2 :
075: TimeS_AS1 += '0'+dms1
076: elif len(dms1) == 1 :
077: TimeS_AS1 += '00'+dms1
078: #Time finish
079: dt2 = TimeF_AS
080: dh2 = dt2 // (60*60*1000)
081: dt2 -= dh2*60*60*1000
082: dm2 = dt2 // (60*1000)
083: dt2 -= dm2*60*1000
084: ds2 = dt2 // (1000)
085: dt2 -= ds2*1000
086: dms2 = dt2
087: TimeF_AS1 =''
088: dh2 = str(dh2)
089: dm2 = str(dm2)
090: ds2 = str(ds2)
091: dms2 = str(dms2)
092: if len(dh2) == 2 :
093: TimeF_AS1 += dh2+':'
094: elif len(dh2) == 1 :
095: TimeF_AS1 += '0'+dh2+':'
096: if len(dm2) == 2 :
097: TimeF_AS1 += dm2+':'
098: elif len(dm2) == 1 :
099: TimeF_AS1 += '0'+dm2+':'
100: if len(ds2) == 2 :
101: TimeF_AS1 += ds2+','
102: elif len(ds2) == 1 :
103: TimeF_AS1 += '0'+ds2+','
104: if len(dms2) == 3 :
105: TimeF_AS1 += dms2
106: elif len(dms2) == 2 :
107: TimeF_AS1 += '0'+dms2
108: elif len(dms2) == 1 :
109: TimeF_AS1 += '00'+dms2
110: Timeall = ''
111: Timeall += TimeS_AS1+' --> '+TimeF_AS1
112: info_fin3[i][1] = Timeall
113: #write info to new file.srt
114: pop = info_fin3.pop(-1)
115: c = 1
116: for i in range(len(info_fin3)) :
117: info_fin3[i][0] = c
118: c += 1
119: info_fin4 = []
120: for i in range(len(info_fin3)) :
121: s1 = ''
122: for j in range(len(info_fin3[i])) :
123: if j == len(info_fin3[i])-1 :
124: s1 += str(info_fin3[i][j])
125: else :
126: s1 += str(info_fin3[i][j])+'\n'
127: info_fin4.append(s1)
128: info_fin5 = ''
129: for i in range(len(info_fin4)) :
130: if i == len(info_fin4)-1 :
131: info_fin5 += str(info_fin4[i])
132: else :
133: info_fin5 += str(info_fin4[i])+'\n\n'
134: fout = open(file_out, "w" , encoding="utf-8")
135:
136: fout.write(info_fin5)
137:
138: fout.close()
139: return
140:
141: # ------------------------------------------
142: def merge(base_file, merge_file, threshold, file_out):
143: return
144:
145: # ------------------------------------------
146: def clean(file_in, file_out):
147: #gather info from file.srt
148: fin = open(file_in, "r" , encoding="utf-8")
149: info_fin1 = ''
150: for line in fin :
151: info_fin1 += line
152: fin.close()
153: info_fin2 = info_fin1.split('\n\n')
154: info_fin3 = []
155: for i in range(len(info_fin2)) :
156: info_septem1 = []
157: info_septem1 = info_fin2[i].split('\n')
158: info_fin3.append(info_septem1)
159: pop = info_fin3.pop(-1)
160: #------------------------------------------#
161: for i in range(len(info_fin3)) :
162: if len(info_fin3[i]) == 3 :
163: if '[' in info_fin3[i][2] :
164: if ']' in info_fin3[i][2] :
165: ind1 = info_fin3[i][2].find('[')
166: ind2 = info_fin3[i][2].find(']')
167: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
168: if '[' in Text :
169: if ']' in Text :
170: ind3 = Text.find('[')
171: ind4 = Text.find(']')
172: Text = Text[0:ind3]+Text[ind4+1::]
173: else :
174: Text = info_fin3[i][2]
175: elif '(' in info_fin3[i][2] :
176: if ')' in info_fin3[i][2] :
177: ind1 = info_fin3[i][2].find('(')
178: ind2 = info_fin3[i][2].find(')')
179: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
180: if '(' in Text :
181: if ')' in Text :
182: ind3 = Text.find('(')
183: ind4 = Text.find(')')
184: Text = Text[0:ind3]+Text[ind4+1::]
185: else :
186: Text = info_fin3[i][2]
187: elif '<' in info_fin3[i][2] :
188: if '>' in info_fin3[i][2] :
189: ind1 = info_fin3[i][2].find('<')
190: ind2 = info_fin3[i][2].find('>')
191: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
192: if '<' in Text :
193: if '>' in Text :
194: ind3 = Text.find('<')
195: ind4 = Text.find('>')
196: Text = Text[0:ind3]+Text[ind4+1::]
197: else :
198: Text = info_fin3[i][2]
199: elif '{' in info_fin3[i][2] :
200: if '}' in info_fin3[i][2] :
201: ind1 = info_fin3[i][2].find('{')
202: ind2 = info_fin3[i][2].find('}')
203: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
204: if '{' in Text :
205: if '}' in Text :
206: ind3 = Text.find('{')
207: ind4 = Text.find('}')
208: Text = Text[0:ind3]+Text[ind4+1::]
209: else :
210: Text = info_fin3[i][2]
211: else :
212: Text = info_fin3[i][2]
213: T_test = False
214: for ch in Text :
215: if ch.isalnum() == True :
216: info_fin3[i][2] = Text
217: T_test = True
218: pass
219: if T_test == False :
220: pop2 = info_fin3[i].pop(2)
221: Text = ''
222: elif len(info_fin3[i]) == 4 :
223: #clean 2nd line
224: if '[' in info_fin3[i][3] :
225: if ']' in info_fin3[i][3] :
226: ind1 = info_fin3[i][3].find('[')
227: ind2 = info_fin3[i][3].find(']')
228: Text = info_fin3[i][3][0:ind1]+info_fin3[i][3][ind2+1::]
229: if '[' in Text :
230: if ']' in Text :
231: ind3 = Text.find('[')
232: ind4 = Text.find(']')
233: Text = Text[0:ind3]+Text[ind4+1::]
234: else :
235: Text = info_fin3[i][3]
236: elif '(' in info_fin3[i][3] :
237: if ')' in info_fin3[i][3] :
238: ind1 = info_fin3[i][3].find('(')
239: ind2 = info_fin3[i][3].find(')')
240: Text = info_fin3[i][3][0:ind1]+info_fin3[i][3][ind2+1::]
241: if '(' in Text :
242: if ')' in Text :
243: ind3 = Text.find('(')
244: ind4 = Text.find(')')
245: Text = Text[0:ind3]+Text[ind4+1::]
246: else :
247: Text = info_fin3[i][3]
248: elif '<' in info_fin3[i][3] :
249: if '>' in info_fin3[i][3] :
250: ind1 = info_fin3[i][3].find('<')
251: ind2 = info_fin3[i][3].find('>')
252: Text = info_fin3[i][3][0:ind1]+info_fin3[i][3][ind2+1::]
253: if '<' in Text :
254: if '>' in Text :
255: ind3 = Text.find('<')
256: ind4 = Text.find('>')
257: Text = Text[0:ind3]+Text[ind4+1::]
258: else :
259: Text = info_fin3[i][3]
260: elif '{' in info_fin3[i][3] :
261: if '}' in info_fin3[i][3] :
262: ind1 = info_fin3[i][3].find('{')
263: ind2 = info_fin3[i][3].find('}')
264: Text = info_fin3[i][3][0:ind1]+info_fin3[i][3][ind2+1::]
265: if '{' in Text :
266: if '}' in Text :
267: ind3 = Text.find('{')
268: ind4 = Text.find('}')
269: Text = Text[0:ind3]+Text[ind4+1::]
270: else :
271: Text = info_fin3[i][3]
272: else :
273: Text = info_fin3[i][3]
274: T_test = False
275: for ch in Text :
276: if ch.isalnum() == True :
277: info_fin3[i][3] = Text
278: T_test = True
279: pass
280: if T_test == False :
281: pop2 = info_fin3[i].pop(3)
282: Text = ''
283: #clean 1st line
284: if '[' in info_fin3[i][2] :
285: if ']' in info_fin3[i][2] :
286: ind1 = info_fin3[i][2].find('[')
287: ind2 = info_fin3[i][2].find(']')
288: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
289: if '[' in Text :
290: if ']' in Text :
291: ind3 = Text.find('[')
292: ind4 = Text.find(']')
293: Text = Text[0:ind3]+Text[ind4+1::]
294: else :
295: Text = info_fin3[i][2]
296: elif '(' in info_fin3[i][2] :
297: if ')' in info_fin3[i][2] :
298: ind1 = info_fin3[i][2].find('(')
299: ind2 = info_fin3[i][2].find(')')
300: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
301: if '(' in Text :
302: if ')' in Text :
303: ind3 = Text.find('(')
304: ind4 = Text.find(')')
305: Text = Text[0:ind3]+Text[ind4+1::]
306: else :
307: Text = info_fin3[i][2]
308: elif '<' in info_fin3[i][2] :
309: if '>' in info_fin3[i][2] :
310: ind1 = info_fin3[i][2].find('<')
311: ind2 = info_fin3[i][2].find('>')
312: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
313: if '<' in Text :
314: if '>' in Text :
315: ind3 = Text.find('<')
316: ind4 = Text.find('>')
317: Text = Text[0:ind3]+Text[ind4+1::]
318: else :
319: Text = info_fin3[i][2]
320: elif '{' in info_fin3[i][2] :
321: if '}' in info_fin3[i][2] :
322: ind1 = info_fin3[i][2].find('{')
323: ind2 = info_fin3[i][2].find('}')
324: Text = info_fin3[i][2][0:ind1]+info_fin3[i][2][ind2+1::]
325: if '{' in Text :
326: if '}' in Text :
327: ind3 = Text.find('{')
328: ind4 = Text.find('}')
329: Text = Text[0:ind3]+Text[ind4+1::]
330: else :
331: Text = info_fin3[i][2]
332: else :
333: Text = info_fin3[i][2]
334: T_test = False
335: for ch in Text :
336: if ch.isalnum() == True :
337: info_fin3[i][2] = Text
338: T_test = True
339: pass
340: if T_test == False :
341: pop2 = info_fin3[i].pop(2)
342: Text = ''
343: #2 line combine
344: if len(info_fin3[i]) == 4 :
345: if '[' in info_fin3[i][2] :
346: if ']' in info_fin3[i][3] :
347: ind1 = info_fin3[i][2].find('[')
348: ind2 = info_fin3[i][3].find(']')
349: Text1 = info_fin3[i][2][0:ind1:]
350: Text2 = info_fin3[i][3][ind2+1::]
351: elif '(' in info_fin3[i][2] :
352: if ')' in info_fin3[i][3] :
353: ind1 = info_fin3[i][2].find('(')
354: ind2 = info_fin3[i][3].find(')')
355: Text1 = info_fin3[i][2][0:ind1:]
356: Text2 = info_fin3[i][3][ind2+1::]
357: elif '{' in info_fin3[i][2] :
358: if '}' in info_fin3[i][3] :
359: ind1 = info_fin3[i][2].find('{')
360: ind2 = info_fin3[i][3].find('}')
361: Text1 = info_fin3[i][2][0:ind1:]
362: Text2 = info_fin3[i][3][ind2+1::]
363: elif '<' in info_fin3[i][2] :
364: if '>' in info_fin3[i][3] :
365: ind1 = info_fin3[i][2].find('<')
366: ind2 = info_fin3[i][3].find('>')
367: Text1 = info_fin3[i][2][0:ind1:]
368: Text2 = info_fin3[i][3][ind2+1::]
369: else :
370: Text2 = info_fin3[i][3]
371: Text1 = info_fin3[i][2]
372: T1_test = False
373: T2_test = False
374: for ch in Text2 :
375: if ch.isalnum() == True :
376: info_fin3[i][3] = Text2
377: T2_test = True
378: pass
379: if T2_test == False :
380: pop3 = info_fin3[i].pop(3)
381: for ch in Text1 :
382: if ch.isalnum() == True :
383: info_fin3[i][2] = Text1
384: T1_test = True
385: pass
386: if T1_test == False :
387: pop2 = info_fin3[i].pop(2)
388: info_fin4 = []
389: for i in range(len(info_fin3)) :
390: if len(info_fin3[i]) > 2 :
391: info_fin4.append(info_fin3[i])
392: c = 1
393: for i in range(len(info_fin4)) :
394: info_fin4[i][0] = c
395: c += 1
396: info_fin5 = []
397: for i in range(len(info_fin4)) :
398: s1 = ''
399: for j in range(len(info_fin4[i])) :
400: if j == len(info_fin4[i])-1 :
401: s1 += str(info_fin4[i][j])
402: else :
403: s1 += str(info_fin4[i][j])+'\n'
404: info_fin5.append(s1)
405: info_fin6 = ''
406: for i in range(len(info_fin5)) :
407: if i == len(info_fin5)-1 :
408: info_fin6 += str(info_fin5[i])
409: else :
410: info_fin6 += str(info_fin5[i])+'\n\n'
411: fout = open(file_out, "w" , encoding="utf-8")
412:
413: fout.write(info_fin6)
414:
415: fout.close()
416: return
417:
418: # ------------------------------------------
6431804021 (53.33%)
shift -> 100.0%
[35, 'shift_1.srt', 95500, 'out.srt']
err = , stdout = , close_file = True |
[35, 'shift_1.srt', -95500, 'out.srt']
err = , stdout = , close_file = True |
[30, 'shift_1.srt', -130500, 'out.srt']
err = , stdout = , close_file = True |
clean -> 60.0%
[30, 'clean_1.srt', 'out.srt']
err = , stdout = , close_file = True |
[30, 'clean_2.srt', 'out.srt']
err = , stdout = , close_file = True |
[20, 'clean_3.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
1
00:01:45,100 --> 00:01:48,100
AAAA.
BBB
2
00:01:55,500 --> 00:02:10,600
AAAA AAA.
BBB
3
00:59:50,100 --> 01:00:08,100
♫♫ AAAA AAAA AAAAA
ZZZZ
4
01:01:05,100 --> 01:01:18,100
AAAA AAAA AAA ♪♪.
XXXX |
[20, 'clean_4.srt', 'out.srt']
err = , stdout = , close_file = True |
1
00:01:45,100 --> 00:01:48,100
123
♪
A♪
2
00:01:55,500 --> 00:02:10,600
$$
3
00:59:50,100 --> 01:00:08,100
♫♫ ♫
5
01:03:45,100 --> 01:03:48,100
A♪ |
1
00:01:45,100 --> 00:01:48,100
123
A♪
2
01:03:45,100 --> 01:03:48,100
A♪ |
merge -> 0%
Traceback (most recent call last):
File "", line 181, in __test_merge_
return __test_(merge, cases)
NameError: name 'merge' is not defined
|
001: def shift(file_in, time_shift, file_out):
002: from datetime import datetime, timedelta
003: file1 = open(file_in, 'r', encoding='utf-8')
004: temp = file1.readlines()
005: cnt = 0
006: lineArr = []
007: col = []
008: for i in temp:
009: col.append(i)
010: if i == '\n':
011: lineArr.append(col)
012: col = []
013: cnt += 1
014: continue
015:
016: ans = []
017: time_shift_copy = int(time_shift)
018: time_shift = abs(int(time_shift))
019: hour_change = time_shift // int(1000 * 60 * 60)
020: time_shift -= hour_change * (1000*60 * 60)
021: minute_change = time_shift // int(1000 * 60)
022: time_shift -= minute_change * (1000*60)
023: second_change = time_shift // 1000
024: time_shift -= second_change * 1000
025: millisecond_change = time_shift
026: time_change = "{:02d}:{:02d}:{:02d},{:03d}". format(
027: hour_change, minute_change, second_change, millisecond_change)
028: FMT = '%H:%M:%S,%f'
029: cnt = 1
030: for line in lineArr:
031: a, b = line[1][:-2].split(' --> ')
032: # SKIP
033: if b <= time_change and time_shift_copy < 0:
034: continue
035:
036: if a <= time_change and time_shift_copy < 0:
037: a_ans = '00:00:00,000'
038: else:
039: tdelta = datetime.strptime(
040: a, FMT) + timedelta(milliseconds=time_shift_copy)
041: a_ans = "{:02d}:{:02d}:{:02d},{:03d}". format(
042: tdelta.hour, tdelta.minute, tdelta.second, tdelta.microsecond // 1000)
043:
044: tdelta = datetime.strptime(
045: b, FMT) + timedelta(milliseconds=time_shift_copy)
046: b_ans = "{:02d}:{:02d}:{:02d},{:03d}". format(
047: tdelta.hour, tdelta.minute, tdelta.second, tdelta.microsecond // 1000)
048: line[0] = str(cnt) + '\n'
049: cnt += 1
050: line[1] = a_ans + ' --> ' + b_ans + '\n'
051: ans.append(line)
052: fout = open(file_out, 'w', encoding='utf-8')
053: for i in ans:
054: for j in i:
055: fout.write(j)
056: fout.close()
057:
058: def clean(file_in, file_out):
059: import re
060: file1 = open(file_in, 'r', encoding='utf-8')
061: temp = file1.readlines()
062: cnt = 0
063: lineArr = []
064: col = []
065: for i in temp:
066: col.append(i)
067: if i == '\n':
068: lineArr.append(col)
069: col = []
070: cnt += 1
071: continue
072: cnt = 1
073: ans = []
074: for line in lineArr:
075: x = ''
076: for i in range(2, len(line)):
077: x += line[i]
078: s = ''
079: find_str = ''
080: for c in x:
081: if find_str != '':
082: if c == find_str:
083: find_str = ''
084: continue
085: if c == '<':
086: find_str = '>'
087: continue
088: if c == '{':
089: find_str = '}'
090: continue
091: if c == '(':
092: find_str = ')'
093: continue
094: if c == '[':
095: find_str = ']'
096: continue
097: s += c
098: chk = False
099: if x == '':
100: continue
101: for i in x:
102: if i.isalnum():
103: chk = True
104: break
105: if not chk:
106: continue
107: line[2] = s
108: ans.append(line[:3])
109: fout = open(file_out, 'w', encoding='utf-8')
110: for i in ans:
111: for j in i:
112: fout.write(j)
113: fout.close()