Repository navigation
Expand file tree
/
Copy pathshift_subs.py
More file actions
132 lines (103 loc) · 3.03 KB
/
Copy pathshift_subs.py
File metadata and controls
132 lines (103 loc) · 3.03 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import re
from functools import partial
from mtlogger import logger
from mtfs import read_text_file, write_text_file
from mtprompt import Prompt
from _constants import (
ASS_SUBTITLE_EXTS,
ASS_TIME_PATTERN,
SRT_TIME_PATTERN,
VTT_EXT,
VTT_TIME_PATTERN,
)
def main():
subs_file = Prompt.file('Enter the path to the subtitles file')
shift = Prompt.int('Enter milliseconds to shift (+/-)')
shift_subtitles(subs_file, shift)
def parse_time(
time_str: str,
):
parts = time_str.replace(',', '.').split(':')
hours = int(parts[0])
minutes = int(parts[1])
seconds = float(parts[2])
total_ms = int((hours * 3600 + minutes * 60 + seconds) * 1000)
return total_ms
def get_time_units(
ms: int,
):
ms = max(0, ms)
hours, remainder = divmod(ms, 3600000)
minutes, remainder = divmod(remainder, 60000)
seconds, milliseconds = divmod(remainder, 1000)
return hours, minutes, seconds, milliseconds
def ms_to_srt_time(
ms: int,
):
hours, minutes, seconds, milliseconds = get_time_units(ms)
return f'{hours:02d}:{minutes:02d}:{seconds:02d},{milliseconds:03d}'
def ms_to_vtt_time(
ms: int,
):
hours, minutes, seconds, milliseconds = get_time_units(ms)
return f'{hours:02d}:{minutes:02d}:{seconds:02d}.{milliseconds:03d}'
def ms_to_ass_time(
ms: int,
):
rounded_ms = round(ms / 10) * 10
hours, minutes, seconds, milliseconds = get_time_units(rounded_ms)
return f'{hours:01d}:{minutes:02d}:{seconds:02d}.{milliseconds // 10:02d}'
def replace_srt_time(
match: re.Match,
ms_to_shift: int,
):
start_time = parse_time(match.group(1))
end_time = parse_time(match.group(2))
return f'{ms_to_srt_time(start_time + ms_to_shift)} --> {ms_to_srt_time(end_time + ms_to_shift)}'
def replace_vtt_time(
match: re.Match,
ms_to_shift: int,
):
start_time = parse_time(match.group(1))
end_time = parse_time(match.group(2))
return f'{ms_to_vtt_time(start_time + ms_to_shift)} --> {ms_to_vtt_time(end_time + ms_to_shift)}'
def replace_ass_time(
match: re.Match,
ms_to_shift: int,
):
start_time = parse_time(match.group(2))
end_time = parse_time(match.group(3))
return f'{match.group(1)}{ms_to_ass_time(start_time + ms_to_shift)},{ms_to_ass_time(end_time + ms_to_shift)}'
def shift_subtitles(
subs_path: str,
ms_to_shift: int,
):
content = read_text_file(subs_path)
ext = os.path.splitext(subs_path)[1].lower()
if ext in ASS_SUBTITLE_EXTS:
new_content = re.sub(
ASS_TIME_PATTERN,
partial(replace_ass_time, ms_to_shift=ms_to_shift),
content,
)
elif ext == VTT_EXT:
new_content = re.sub(
VTT_TIME_PATTERN,
partial(replace_vtt_time, ms_to_shift=ms_to_shift),
content,
)
else:
new_content = re.sub(
SRT_TIME_PATTERN,
partial(replace_srt_time, ms_to_shift=ms_to_shift),
content,
)
write_text_file(subs_path, new_content)
logger.success(f'Subtitles shifted by {ms_to_shift}ms', prefix_newline=True)
if __name__ == '__main__':
try:
main()
except Exception as ex:
logger.unhandled_error(ex)
Prompt.enter_to_exit()