-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimecodeglobal.py
More file actions
92 lines (56 loc) · 2.58 KB
/
timecodeglobal.py
File metadata and controls
92 lines (56 loc) · 2.58 KB
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
def getHourMinuteSecondFrames(timecode: str):
timecodeNoFrames = timecode[:8]
timecodeFrames = timecode[9:]
timecodeSplit = timecodeNoFrames.split(':')
hour = int(timecodeSplit[0])
minute = int(timecodeSplit[1])
second = int(timecodeSplit[2])
return hour, minute, second, timecodeFrames
def updateTimecode(timecodeIn: tuple, startTimecode: tuple):
second = timecodeIn[2] + startTimecode[2]
minute = timecodeIn[1] + startTimecode[1]
if second > 59:
minute += 1
second -= 60
hour = timecodeIn[0] + startTimecode[0]
if minute > 59:
hour +=1
minute -= 60
ret_str = str(hour).zfill(2) + ':' + str(minute).zfill(2) + ':' + str(second).zfill(2) + ',' + timecodeIn[3]
return ret_str
def globalizeTimecode(filename: str, start_timecode: str):
start_timecode = start_timecode.split(':')
try:
start_hour = int(start_timecode[0])
start_minute = int(start_timecode[1])
start_second = int(start_timecode[2])
if (start_minute < 0 or start_minute > 59) or (start_second < 0 or start_second > 59):
raise ValueError
startTimecode = start_hour, start_minute, start_second
with open(filename, 'r') as file:
lines = file.readlines()
with open('globalTC_' + filename, 'w') as newfile:
for line in lines:
if len(line) > 2:
if line[0].isdigit() and line[1].isdigit() and line[2] == ':':
linesplit = line.split(' ')
timecodeIn = linesplit[0]
sign = linesplit[1]
timecodeOut = linesplit[2]
timecodeIn = updateTimecode(getHourMinuteSecondFrames(timecodeIn), startTimecode)
timecodeOut = updateTimecode(getHourMinuteSecondFrames(timecodeOut), startTimecode)
line = timecodeIn + ' ' + sign + ' ' + timecodeOut
newfile.write(line)
print(f'Timecodes in {filename} successfuly updated to "globalTC_{filename}"')
except IndexError:
print('Invalid timecode')
except ValueError:
print('hh:mm:ss must be two digits positive integers (00-59)')
except FileNotFoundError:
print('File not found')
def main():
filename = input('Filename >')
start_timecode = input('start timecode hh:mm:ss >')
globalizeTimecode(filename, start_timecode)
if __name__ == '__main__':
main()