-
Notifications
You must be signed in to change notification settings - Fork 18
/
adjust_timestamp.py
123 lines (84 loc) · 2.25 KB
/
adjust_timestamp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import re
import numpy as np
def add_time(t, OFFSET):
# Convert to ints
t = [int(i) for i in t]
# Add milliseconds if present
ms = OFFSET % 1
ms *= 1000
ms = int(np.round(ms))
if ms > 0:
t[3] += ms
if t[3] >= 1000:
t[2] += 1
t[3] -= 1000
# Add seconds
s = int(np.floor(OFFSET))
if s > 0:
t[2] += s
if t[2] >= 60:
# Add to minutes
t[1] += t[2] // 60
t[2] = t[2] % 60
# Add to hours
if t[1] >= 60:
t[0] += t[1] // 60
t[1] = t[1] % 60
string = '{0:02d}:{1:02d}:{2:02d}.{3:03d}'.format(t[0], t[1], t[2], t[3])
# print(string)
return string
def sub_time(t, OFFSET):
# Convert to ints
t = [int(i) for i in t]
# Add milliseconds if present
ms = OFFSET % -1
ms *= 1000
ms = int(np.round(ms))
if ms < 0:
t[3] += ms
if t[3] < 0:
t[2] -= 1
t[3] += 1000
# Add seconds
s = int(np.ceil(OFFSET))
if s < 0:
t[2] += s
if t[2] < 0:
# Add to minutes
t[1] += t[2] // 60
t[2] = (t[2] % 60)
# Add to hours
if t[1] < 0:
t[0] += t[1] // 60
t[1] = (t[1] % 60)
string = '{0:02d}:{1:02d}:{2:02d}.{3:03d}'.format(t[0], t[1], t[2], t[3])
# print(string)
return string
def adjust_time(line, f, n, OFFSET):
pattern = '[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}'
pattern_number = '[0-9]{2,3}'
result = re.match(pattern, line)
if result:
times = re.findall(pattern_number, line)
if OFFSET > 0:
t1 = add_time(times[0:4], OFFSET)
t2 = add_time(times[4:], OFFSET)
else:
t1 = sub_time(times[0:4], OFFSET)
t2 = sub_time(times[4:], OFFSET)
append = '{} --> {}\n'.format(t1, t2)
else:
append = line
n.write(append)
# Specify the target of comversion
target = "./IR2/IR2_intro.vtt"
# Specify offset in seconds
OFFSET = -4.7
f = open(target)
save_target = target[0:-4] + '_new.vtt'
n = open(save_target, "x")
for line in f:
adjust_time(line, f, n, OFFSET)
f.close()
n.close
# print(f.readline())