-
Notifications
You must be signed in to change notification settings - Fork 0
/
titulky.py
354 lines (268 loc) · 9.63 KB
/
titulky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env python3
"""Titulky SubRip editor.
Command line program for editing .srt files. Display file informations, shift or
fit subtitles.
Note: We internally process binary data to work around the various text encodings / BOMs.
"""
import argparse
import datetime
import math
import os
import sys
from typing import Generator, Iterable, List, NamedTuple, Tuple
from argparse import Namespace
DIGITS = set(b'0123456789')
ARROW = b' --> '
__version__ = '1.0.2'
def parse_timestamp(text: str) -> float:
"""Parse normal timestamp.
Example:
>>> parse_timestamp('1:23:45.6789')
5025.6789
"""
parts = text.split(':')
if len(parts) > 3:
raise ValueError(f'Invalid time string {text!r}')
timestamp = 0.
sign = math.copysign(1, float(parts[0]))
for i, part in enumerate(reversed(parts)):
timestamp += abs(float(part)) * (60 ** i)
return sign * timestamp
def parse_timestamp_srt(text: bytes) -> float:
"""Parse SubRip timestamp from bytes.
Example:
>>> parse_timestamp_srt(b'12:34:56,789')
45296.789
"""
text = text.decode().strip()
dt = datetime.datetime.strptime(text, '%H:%M:%S,%f')
return datetime.timedelta(
hours=dt.hour,
minutes=dt.minute,
seconds=dt.second,
microseconds=dt.microsecond,
).total_seconds()
def format_timestamp_srt(timestamp: float) -> bytes:
"""Format SubRip timestamp to bytes.
Example:
>>> format_timestamp_srt(45296.789)
b'12:34:56,789'
"""
hours, remainder = divmod(timestamp, 3600)
minutes, seconds = divmod(remainder, 60)
ret = b'%02d:%02d:%06.3f' % (int(hours), int(minutes), seconds)
return ret.replace(b'.', b',')
class Subtitle(NamedTuple):
"""Single SubRip subtitle entry."""
nr: int
start: float
end: float
content: list
def parse_subtitle(lines: List[bytes]) -> Subtitle:
"""Parse single subtitle from bytes lines.
Args:
lines: Bytes lines to parse.
Returns:
Parsed Subtitle instance.
Example:
>>> lines = [b'42', b'01:23:45,500 --> 01:34:54,000', b'Hello,', b'world!', b'']
... parse_subtitle(lines)
Subtitle(nr=42, start=5025.5, end=5694.0, content=[b'Hello,', b'world!', b''])
"""
if len(lines) < 3 or ARROW not in lines[1]:
raise ValueError('Can not parse Subtitle from invalid lines', lines)
start, end = map(parse_timestamp_srt, lines[1].split(ARROW))
return Subtitle(
nr = int(lines[0]),
start=start,
end=end,
content=lines[2:],
)
def parse_subtitles(lines: List[bytes]) -> Generator[Subtitle, None, None]:
"""Parse all subtitles from bytes lines.
Args:
lines: Bytes lines to parse.
Yields:
All parsed subtitles.
"""
lines = [ line.strip() for line in lines if line.strip() ]
start = -1
for end, line in enumerate(lines):
if DIGITS.issuperset(line):
if start != -1:
yield parse_subtitle(lines[start:end])
start = end
if start != -1:
yield parse_subtitle(lines[start:])
def format_subtitle(subtitle: Subtitle) -> List[bytes]:
"""Format subtitle to bytes lines.
Args:
subtitle: Subtitle to format.
Returns:
Bytes lines of text (caller can decide which newline character).
"""
lines = [
str(subtitle.nr).encode(),
ARROW.join([
format_timestamp_srt(subtitle.start),
format_timestamp_srt(subtitle.end),
]),
]
lines.extend(subtitle.content)
lines.append(b'') # Empty line SubRip subtitle termination
return lines
def format_subtitles(subtitles: Iterable[Subtitle], newline: bytes = b'\n') -> bytes:
"""Format multiple subtitles to bytes.
Args:
subtitles: Subtitles to format.
Kwargs:
newline: Newline character to use.
Returns:
Formatted SubRip subtitles.
"""
return newline.join(
newline.join(format_subtitle(sub))
for sub in subtitles
)
def ceil(x, ndigits: int = 0) -> float:
"""Ceil up number at ndigits."""
shift = 10 ** ndigits
return math.ceil(x * shift) / shift
def floor(x, ndigits: int = 0) -> float:
"""Floor down number at ndigits."""
shift = 10 ** ndigits
return math.floor(x * shift) / shift
def tranform_subtitles(
subtitles: Iterable[Subtitle],
scale: float = 1.,
offset: float = 0.,
) -> Generator[Subtitle, None, None]:
"""Transform start and end times of subtitles. f(t) = scale * t + offset.
Args:
subtitles: Subtitles to transform.
Kwargs:
scale: Scale factor.
offset: Offset factor.
Yields:
Transformed subtitles.
"""
for sub in subtitles:
yield sub._replace(
start=ceil(scale * sub.start + offset, ndigits=3),
end=floor(scale * sub.end + offset, ndigits=3),
)
def cli() -> Namespace:
"""Parse command line interface."""
parser = argparse.ArgumentParser(prog='Titulky', description='SubRip editor')
parser.add_argument('infile', help='input file')
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.set_defaults(mode='info')
subparsers = parser.add_subparsers()
infoParser = subparsers.add_parser('info', help='show subtitle file infos')
infoParser.set_defaults(mode='info')
shiftParser = subparsers.add_parser('shift', help='shift all subtitles by some amount')
shiftParser.add_argument('amount', help='time shift amount')
shiftParser.add_argument('-o', '--outfile', default=sys.stdout, help='output file. stdout by default.')
shiftParser.set_defaults(mode='shift')
fitParser = subparsers.add_parser('fit', help='fit subtitles to start / end time')
fitParser.add_argument('first', help='start time of first subtitle')
fitParser.add_argument('last', help='start time of last subtitle')
fitParser.add_argument('-o', '--outfile', default=sys.stdout, help='output file. stdout by default')
fitParser.set_defaults(mode='fit')
return parser.parse_args()
def analyze_srt_data(srtData: bytes) -> Tuple[bytes, bytes]:
"""Analyze raw binary SubRip data for BOM prefix and newline style.
Args:
srtData: Bytes to an
Returns:
BOM and newline bytes.
Note: This only works for SubRip files since we are looking for digit
character of the first subtitle in the data stream..
"""
bom = b''
maxBom = 4
for char in srtData[:maxBom]:
if char in DIGITS:
break
bom += bytes([char])
for newline in [b'\r\n', b'\n', b'\r']:
if newline in srtData:
return bom, newline
ValueError('Could not determine newline style!')
def yes_or_no(question: str) -> bool:
"""Yes or no question for the user.
Args:
question: Question to ask the user.
Returns:
Yes or no.
Raises:
RuntimeError: If not possible to parse answer from user.
"""
answer = input(question + ' [y/n]')
key = answer.lower()
valid = {
'y': True, 'ye': True, 'yes': True, 'n': False, 'no': False, '1': True,
'0': False,
}
if key in valid:
return valid[key]
answer = input(f'I do not understand {answer!r}. Please answer with [{",".join(valid)}]')
key = answer.lower()
if key in valid:
return valid[key]
raise RuntimeError(f'Sorry but I still cannot understand you. What did you mean with {answer!r}?')
def main():
args = cli()
with open(args.infile, 'rb') as f:
raw = f.read()
bom, newline = analyze_srt_data(raw)
data = raw.lstrip(bom)
lines = data.split(newline)
subtitles = list(parse_subtitles(lines))
if not subtitles:
print(f'No subtitles in {args.infile!r}')
sys.exit(1)
first = min(sub.start for sub in subtitles)
last = max(sub.start for sub in subtitles)
if args.mode == 'info':
print(f'Found {len(subtitles)} subtitles in {args.infile!r}')
print(f'{len(raw)} bytes')
if bom:
print(f'BOM: {bom}')
else:
print(f'BOM: {bom} (no BOM)')
print(f'newline: {newline}')
print('Subtitles:\n')
print(format_subtitles(subtitles[:3]).decode())
print('...\n')
print(format_subtitles(subtitles[-3:]).decode())
sys.exit(0)
if args.mode == 'shift':
amount = parse_timestamp(args.amount)
if first + amount < 0:
raise ValueError(f'amount {amount} would lead to negative start time')
print(f'Shifting subtitles by {amount} seconds')
newSubtitles = tranform_subtitles(subtitles, scale=1., offset=amount)
elif args.mode == 'fit':
if (last - first) == 0:
raise RuntimeError('Subtitles are not spread out enough for re-scaling')
firstDesired = parse_timestamp(args.first)
lastDesired = parse_timestamp(args.last)
print(f'Re-fitting subtitles to [{args.first}, {args.last}]')
scale = (lastDesired - firstDesired) / (last - first)
offset = firstDesired - scale * first
newSubtitles = tranform_subtitles(subtitles, scale, offset)
out = bom + format_subtitles(newSubtitles, newline)
if args.outfile is sys.stdout:
sys.stdout.buffer.write(out)
sys.stdout.flush()
else:
if os.path.exists(args.outfile):
question = f'File {args.outfile} does already exists! Do you want to overwrite it?'
if not yes_or_no(question):
sys.exit(0)
with open(args.outfile, 'wb') as f:
f.write(out)
print(f'Wrote to {args.outfile!r}')
if __name__ == '__main__':
main()