-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubfixr.py
More file actions
922 lines (751 loc) · 33.8 KB
/
subfixr.py
File metadata and controls
922 lines (751 loc) · 33.8 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
import argparse
import glob
import json
import os
import re
import shutil
import subprocess
import tempfile
TEXT_ENCODINGS = ['utf-8-sig', 'utf-8', 'cp1252', 'latin-1', 'iso-8859-1']
TIME_ROW = re.compile(r'^\d{2}:\d{2}:\d{2},\d{3}\s*-->\s*\d{2}:\d{2}:\d{2},\d{3}$')
BLOCK_NO = re.compile(r'^\d+$')
TIME_BITS = re.compile(r'(\d{2}):(\d{2}):(\d{2}),(\d{3})')
SRT_GLOB = '*.[sS][rR][tT]'
MKS_GLOB = '*.[mM][kK][sS]'
SUBRIP_CODECS = {'S_TEXT/UTF8', 'S_TEXT/ASCII'}
LANG_MAP = {
'en': 'eng', 'es': 'spa', 'id': 'ind', 'ko': 'kor', 'ms': 'may',
'pt-br': 'pt-br', 'pt': 'por', 'th': 'tha', 'vi': 'vie', 'zh': 'zho',
'fr': 'fre', 'de': 'ger', 'it': 'ita', 'ja': 'jpn', 'ru': 'rus',
'ar': 'ara', 'hi': 'hin', 'tr': 'tur', 'pl': 'pol', 'nl': 'dut',
'sv': 'swe', 'da': 'dan', 'no': 'nor', 'fi': 'fin', 'cs': 'cze',
'hu': 'hun', 'ro': 'rum', 'el': 'gre', 'he': 'heb', 'uk': 'ukr',
'bg': 'bul', 'hr': 'hrv', 'sk': 'slo', 'sl': 'slv', 'sr': 'srp',
'mk': 'mac', 'sq': 'alb', 'et': 'est', 'lv': 'lav', 'lt': 'lit',
'is': 'ice', 'ga': 'gle', 'cy': 'wel', 'mt': 'mlt', 'eu': 'baq',
'ca': 'cat', 'gl': 'glg', 'oc': 'oci', 'sc': 'srd', 'co': 'cos',
'br': 'bre', 'gd': 'gla', 'fo': 'fao', 'rm': 'roh', 'wa': 'wln',
'lb': 'ltz', 'af': 'afr', 'sw': 'swa', 'zu': 'zul', 'xh': 'xho',
'tn': 'tsn', 'st': 'sot', 'ss': 'ssw', 've': 'ven', 'ts': 'tso',
'nr': 'nbl', 'nso': 'nso'
}
def run_text_command(args, timeout, encoding='ascii', errors='backslashreplace'):
return subprocess.run(
args,
capture_output=True,
text=True,
encoding=encoding,
errors=errors,
timeout=timeout,
)
def ms_from_timecode(raw):
hit = TIME_BITS.match(raw)
if not hit:
raise ValueError(f"Invalid time format: {raw}")
hh, mm, ss, ms = (int(part) for part in hit.groups())
return hh * 3600000 + mm * 60000 + ss * 1000 + ms
def timecode_from_ms(total_ms):
if total_ms < 0:
total_ms = 0
hh, rem = divmod(total_ms, 3600000)
mm, rem = divmod(rem, 60000)
ss, ms = divmod(rem, 1000)
return f"{hh:02d}:{mm:02d}:{ss:02d},{ms:03d}"
def adjust_time_row(row, fps_scale=1.0, delta=0):
hit = re.match(r'(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})$', row)
if not hit:
return row
start, end = hit.groups()
moved_start = timecode_from_ms(round(ms_from_timecode(start) * fps_scale) + delta)
moved_end = timecode_from_ms(round(ms_from_timecode(end) * fps_scale) + delta)
return f"{moved_start} --> {moved_end}"
def parse_shift(raw):
raw = raw.strip().lower()
if raw.endswith('min'):
return round(float(raw[:-3]) * 60000)
if raw.endswith('ms'):
return round(float(raw[:-2]))
if raw.endswith('s'):
return round(float(raw[:-1]) * 1000)
try:
return round(float(raw) * 1000)
except ValueError as exc:
raise ValueError(f"Invalid time format: {raw}. Use something like 1s, 1ms, 1min, or plain seconds") from exc
def clean_direction(raw):
raw = raw.lower().strip()
if raw in ('for', 'forward', 'fwd', 'f'):
return 'forward'
if raw in ('back', 'backward', 'bwd', 'b'):
return 'backward'
raise ValueError(f"Invalid direction: {raw}. Use forward/for or backward/back")
def parse_fps_value(raw):
raw = raw.strip().lower()
if not raw:
raise ValueError("FPS value cannot be empty")
if '/' in raw:
left, right = [part.strip() for part in raw.split('/', 1)]
try:
numerator = float(left)
denominator = float(right)
except ValueError as exc:
raise ValueError(f"Invalid FPS value: {raw}") from exc
if denominator == 0:
raise ValueError(f"Invalid FPS value: {raw}. Denominator cannot be zero")
value = numerator / denominator
else:
try:
value = float(raw)
except ValueError as exc:
raise ValueError(f"Invalid FPS value: {raw}") from exc
if value <= 0:
raise ValueError(f"Invalid FPS value: {raw}. FPS must be greater than zero")
return value
def parse_fps_change(raw):
if isinstance(raw, (list, tuple)):
pieces = [str(part).strip() for part in raw if str(part).strip()]
if len(pieces) == 2:
source_label, target_label = pieces
elif len(pieces) == 1:
raw = pieces[0]
else:
raise ValueError("Invalid --fps value: use source:target like 25:23.976 or pass two values like --fps 25 23.976")
if isinstance(raw, str):
raw = raw.strip()
pieces = re.split(r'\s*(?:->|:|to)\s*', raw, maxsplit=1)
if len(pieces) == 2 and pieces[0] and pieces[1]:
source_label = pieces[0].strip()
target_label = pieces[1].strip()
else:
try:
parse_fps_value(raw)
except ValueError:
raise ValueError("Invalid --fps value: use source:target like 25:23.976 or pass two values like --fps 25 23.976") from None
raise ValueError(f"Invalid --fps value: a single FPS like {raw} is ambiguous. Use source:target like 25:{raw} or pass two values like --fps 25 {raw}")
elif not isinstance(raw, (list, tuple)):
raise ValueError("Invalid --fps value: use source:target like 25:23.976 or pass two values like --fps 25 23.976")
source = parse_fps_value(source_label)
target = parse_fps_value(target_label)
if abs(source - target) < 1e-12:
raise ValueError("Invalid --fps value: source and target FPS are the same")
return {
'source': source,
'target': target,
'scale': source / target,
'source_label': source_label,
'target_label': target_label,
}
def load_text(path, limit=None):
last_error = None
for enc in TEXT_ENCODINGS:
try:
with open(path, 'r', encoding=enc, newline='') as fh:
text = fh.read() if limit is None else fh.read(limit)
newline = '\n'
if '\r\n' in text:
newline = '\r\n'
elif '\r' in text:
newline = '\r'
return text, newline
except UnicodeDecodeError as exc:
last_error = exc
raise ValueError(f"Could not decode file with common encodings: {', '.join(TEXT_ENCODINGS)}") from last_error
def read_blocks(text):
text = text.replace('\r\n', '\n').replace('\r', '\n').strip()
if not text:
return []
chunks = re.split(r'\n\s*\n', text)
blocks = []
for chunk in chunks:
lines = chunk.split('\n')
if len(lines) < 2:
continue
maybe_no = lines[0].strip()
when = lines[1].strip()
if not BLOCK_NO.match(maybe_no):
continue
if not TIME_ROW.match(when):
continue
blocks.append({
'number': int(maybe_no),
'timestamp': lines[1],
'text': lines[2:],
})
return blocks
def join_blocks(blocks):
out = []
idx = 1
for block in blocks:
out.append(str(idx))
out.append(block['timestamp'])
out.extend(block['text'])
out.append('')
idx += 1
return '\n'.join(out)
def parse_block_selector(raw):
picks = set()
pieces = [piece.strip() for piece in raw.split(',') if piece.strip()]
if not pieces:
raise ValueError("Invalid --delete-lines value: use subtitle numbers or ranges like 1-8 or 1,3,5-7")
for piece in pieces:
if '-' not in piece:
if not piece.isdigit():
raise ValueError(f"Invalid --delete-lines entry: {piece}")
number = int(piece)
if number < 1:
raise ValueError(f"Invalid --delete-lines entry: {piece}. Subtitle numbers must start from 1")
picks.add(number)
continue
left, right = [part.strip() for part in piece.split('-', 1)]
if not left.isdigit() or not right.isdigit():
raise ValueError(f"Invalid --delete-lines range: {piece}")
start = int(left)
end = int(right)
if start < 1 or end < 1:
raise ValueError(f"Invalid --delete-lines range: {piece}. Subtitle numbers must start from 1")
if start > end:
raise ValueError(f"Invalid --delete-lines range: {piece}. Start must be less than or equal to end")
picks.update(range(start, end + 1))
return picks
def drop_blocks_by_number(text, spec):
doomed = parse_block_selector(spec)
blocks = read_blocks(text)
keep = [block for block in blocks if block['number'] not in doomed]
return join_blocks(keep), len(blocks) - len(keep)
def drop_blocks_by_text(text, needle):
blocks = read_blocks(text)
keep = []
hit_count = 0
needle = needle.lower()
for block in blocks:
blob = ' '.join(block['text']).lower()
if needle in blob:
hit_count += 1
continue
keep.append(block)
return join_blocks(keep), hit_count
def render_fixed_subtitle(src, shift_value, direction, remove_text=None, delete_lines=None, fps_change=None):
delta = 0
if shift_value:
if not direction:
raise ValueError("Direction is required when shift value is provided")
delta = parse_shift(shift_value)
if direction == 'backward':
delta = -delta
fps_scale = fps_change['scale'] if fps_change else 1.0
text, line_ending = load_text(src)
removed_by_number = 0
removed_by_text = 0
if delete_lines:
text, removed_by_number = drop_blocks_by_number(text, delete_lines)
if remove_text:
text, removed_by_text = drop_blocks_by_text(text, remove_text)
out = []
timed_rows = 0
for row in text.splitlines():
if TIME_ROW.match(row):
if fps_change or delta:
out.append(adjust_time_row(row, fps_scale, delta))
timed_rows += 1
else:
out.append(row)
else:
out.append(row)
return line_ending.join(out), {
'delta': delta,
'fps_change': fps_change,
'removed_by_number': removed_by_number,
'removed_by_text': removed_by_text,
'timed_rows': timed_rows,
}
def write_text(path, text):
folder = os.path.dirname(path)
if folder:
os.makedirs(folder, exist_ok=True)
with open(path, 'w', encoding='utf-8', newline='') as fh:
fh.write(text)
def fix_one(src, dst, shift_value, direction, remove_text=None, delete_lines=None, fps_change=None):
rendered, stats = render_fixed_subtitle(src, shift_value, direction, remove_text, delete_lines, fps_change)
if delete_lines:
print(f" Removed {stats['removed_by_number']} subtitle block(s) from '{delete_lines}'")
if remove_text:
print(f" Removed {stats['removed_by_text']} subtitle block(s) containing '{remove_text}'")
write_text(dst, rendered)
print(f"[+] Processed: {os.path.basename(src)}")
if stats['fps_change']:
change = stats['fps_change']
print(f" FPS: {change['source_label']} -> {change['target_label']} ({stats['timed_rows']} timestamps retimed)")
if stats['delta']:
print(f" Shift: {direction} by {shift_value} ({stats['timed_rows']} timestamps shifted)")
print(f" Output: {dst}")
return stats
def lang_as_iso639_2(code):
code = code.replace('_', '-').lower()
if code in LANG_MAP:
return LANG_MAP[code]
if '-' not in code:
return LANG_MAP.get(code, code)
base, region = code.split('-', 1)
return f"{LANG_MAP.get(base, base)}-{region}"
def tweak_lang_in_stem(stem):
folder = os.path.dirname(stem)
name = os.path.basename(stem)
bits = name.rsplit('.', 1)
if len(bits) == 2:
base, maybe_lang = bits
swapped = lang_as_iso639_2(maybe_lang)
if swapped != maybe_lang:
name = f"{base}.{swapped}"
return os.path.join(folder, name) if folder else name
def tweak_lang_in_filename(path):
folder = os.path.dirname(path)
filename = os.path.basename(path)
stem, ext = os.path.splitext(filename)
renamed = os.path.basename(tweak_lang_in_stem(stem)) + ext
return os.path.join(folder, renamed) if folder else renamed
def have_mkv_tools():
for cmd in ('mkvextract', 'mkvmerge'):
try:
result = run_text_command([cmd, '--version'], timeout=5)
except (FileNotFoundError, subprocess.TimeoutExpired, OSError):
return False
if result.returncode != 0:
return False
return True
def inspect_mks(mks_file):
result = run_text_command(
['mkvmerge', '--identify', '--identification-format', 'json', mks_file],
timeout=30,
encoding='utf-8',
errors='replace',
)
if result.returncode != 0:
msg = result.stderr.strip() if result.stderr else 'Unknown error'
raise RuntimeError(f"mkvmerge identify failed: {msg}")
try:
return json.loads(result.stdout)
except json.JSONDecodeError as exc:
raise RuntimeError(f"Could not parse mkvmerge output for {mks_file}") from exc
def track_lang_code(track):
lang = track.get('language_ietf') or track.get('language') or 'unknown'
return lang_as_iso639_2(lang) if lang != 'unknown' else lang
def track_label(track):
label = track.get('language_ietf') or track.get('language') or 'unknown'
if track.get('track_name'):
return f"{label} | {track['track_name']}"
return label
def bool_flag(value):
return '1' if value else '0'
def pull_tracks_from_mks(mks_file, temp_dir, strict=False):
if not have_mkv_tools():
raise RuntimeError("mkvextract/mkvmerge not found. Please install MKVToolNix")
try:
info = inspect_mks(mks_file)
title = info.get('container', {}).get('properties', {}).get('title')
tracks = []
editable_count = 0
for raw_track in info.get('tracks', []):
if raw_track.get('type') != 'subtitles':
continue
props = raw_track.get('properties', {})
track = {
'id': raw_track.get('id'),
'number': props.get('number'),
'codec': raw_track.get('codec'),
'codec_id': props.get('codec_id'),
'language': props.get('language'),
'language_ietf': props.get('language_ietf'),
'track_name': props.get('track_name'),
'default_track': props.get('default_track', False),
'forced_track': props.get('forced_track', False),
'enabled_track': props.get('enabled_track', True),
'flag_hearing_impaired': props.get('flag_hearing_impaired', False),
'flag_original': props.get('flag_original', False),
}
if track['codec_id'] not in SUBRIP_CODECS:
tracks.append(track)
continue
lang_code = track_lang_code(track)
base = os.path.splitext(os.path.basename(mks_file))[0]
out_file = os.path.join(temp_dir, f"{base}_track{track['id']}.{lang_code}.srt")
result = run_text_command(['mkvextract', 'tracks', mks_file, f"{track['id']}:{out_file}"], timeout=60)
if result.returncode != 0 or not os.path.exists(out_file):
msg = result.stderr.strip() if result.stderr else result.stdout.strip()
if strict:
raise RuntimeError(f"Failed to extract track {track['id']} ({lang_code}): {msg or 'Unknown error'}")
if msg:
print(f" Warning: Failed to extract track {track['id']} ({lang_code}): {msg}")
tracks.append(track)
continue
try:
text, _ = load_text(out_file)
except ValueError:
text = ''
if text and text.strip():
track['extracted_path'] = out_file
editable_count += 1
tracks.append(track)
if not tracks:
raise ValueError("No subtitle tracks found in MKS file.")
if editable_count == 0:
raise ValueError("No supported SubRip/SRT subtitle tracks found in MKS file.")
return {
'source': mks_file,
'title': title,
'tracks': tracks,
}
except subprocess.TimeoutExpired:
raise RuntimeError("mkvextract operation timed out")
except FileNotFoundError:
raise RuntimeError("mkvextract or mkvmerge not found. Please install MKVToolNix from https://mkvtoolnix.download/")
except Exception as exc:
raise RuntimeError(f"Error extracting subtitles from MKS: {exc}")
def make_temp_workspace(temp_root):
return tempfile.mkdtemp(prefix='mks_', dir=temp_root)
def resolve_mks_output_path(src, out_path, out_is_dir, folder_mode):
if out_path:
if out_is_dir:
return os.path.join(out_path, os.path.basename(src))
if folder_mode:
base_out, ext = os.path.splitext(out_path)
if not ext:
ext = '.mks'
stem = os.path.splitext(os.path.basename(src))[0]
return f"{base_out}_{stem}{ext}"
base_out, ext = os.path.splitext(out_path)
return out_path if ext else f"{base_out}.mks"
base, _ = os.path.splitext(src)
return f"{base}_synced.mks"
def mks_track_args(track):
args = ['--sub-charset', '0:utf-8']
language = track.get('language_ietf') or track.get('language')
if language:
args.extend(['--language', f'0:{language}'])
if track.get('track_name') is not None:
args.extend(['--track-name', f"0:{track['track_name']}"])
args.extend(['--default-track-flag', f"0:{bool_flag(track.get('default_track', False))}"])
args.extend(['--forced-display-flag', f"0:{bool_flag(track.get('forced_track', False))}"])
args.extend(['--track-enabled-flag', f"0:{bool_flag(track.get('enabled_track', True))}"])
args.extend(['--hearing-impaired-flag', f"0:{bool_flag(track.get('flag_hearing_impaired', False))}"])
args.extend(['--original-flag', f"0:{bool_flag(track.get('flag_original', False))}"])
return args
def remux_mks(job, dst):
folder = os.path.dirname(dst)
if folder:
os.makedirs(folder, exist_ok=True)
cmd = ['mkvmerge', '-o', dst]
if job.get('title'):
cmd.extend(['--title', job['title']])
passthrough_ids = [str(track['id']) for track in job['tracks'] if not track.get('processed_path')]
if passthrough_ids:
cmd.extend(['--subtitle-tracks', ','.join(passthrough_ids), job['source']])
else:
cmd.extend(['--no-subtitles', job['source']])
track_refs = {}
next_input_id = 1
for track in job['tracks']:
if track.get('processed_path'):
cmd.extend(mks_track_args(track))
cmd.append(track['processed_path'])
track_refs[track['id']] = f'{next_input_id}:0'
next_input_id += 1
else:
track_refs[track['id']] = f"0:{track['id']}"
cmd.extend(['--track-order', ','.join(track_refs[track['id']] for track in job['tracks'])])
result = run_text_command(cmd, timeout=120, encoding='utf-8', errors='replace')
if result.returncode != 0 or not os.path.exists(dst):
msg = result.stderr.strip() if result.stderr else result.stdout.strip()
raise RuntimeError(f"mkvmerge failed: {msg or 'Unknown error'}")
def process_mks_file(job, dst, shift_value, direction, remove_text=None, delete_lines=None, fps_change=None):
print(f"[+] Rebuilding: {os.path.basename(job['source'])}")
passthrough = 0
for track in job['tracks']:
extracted = track.get('extracted_path')
if not extracted:
passthrough += 1
continue
edited_path = os.path.join(os.path.dirname(extracted), f"edited_track{track['id']}.srt")
rendered, stats = render_fixed_subtitle(extracted, shift_value, direction, remove_text, delete_lines, fps_change)
write_text(edited_path, rendered)
track['processed_path'] = edited_path
print(f" Track {track['id']} ({track_label(track)})")
if delete_lines:
print(f" Removed {stats['removed_by_number']} subtitle block(s) from '{delete_lines}'")
if remove_text:
print(f" Removed {stats['removed_by_text']} subtitle block(s) containing '{remove_text}'")
if stats['fps_change']:
change = stats['fps_change']
print(f" FPS: {change['source_label']} -> {change['target_label']} ({stats['timed_rows']} timestamps retimed)")
if stats['delta']:
print(f" Shift: {direction} by {shift_value} ({stats['timed_rows']} timestamps shifted)")
if passthrough:
print(f" Preserved {passthrough} original subtitle track(s) without edits")
remux_mks(job, dst)
print(f"[+] Processed: {os.path.basename(job['source'])}")
print(f" Output: {dst}")
def scan_inputs(input_path, recursive=False, include_mks=False):
srt_files = []
mks_files = []
if os.path.isfile(input_path):
lower = input_path.lower()
if lower.endswith('.srt'):
return [input_path], []
if include_mks and lower.endswith('.mks'):
return [], [input_path]
raise ValueError(f"Input file is not a supported format (.srt or .mks): {input_path}")
if not os.path.isdir(input_path):
raise FileNotFoundError(f"Input path not found: {input_path}")
if recursive:
srt_files = glob.glob(os.path.join(input_path, '**', SRT_GLOB), recursive=True)
if include_mks:
mks_files = glob.glob(os.path.join(input_path, '**', MKS_GLOB), recursive=True)
else:
srt_files = glob.glob(os.path.join(input_path, SRT_GLOB))
if include_mks:
mks_files = glob.glob(os.path.join(input_path, MKS_GLOB))
if not srt_files and not mks_files:
raise ValueError(f"No subtitle files (.srt or .mks) found in: {input_path}")
srt_files.sort()
mks_files.sort()
return srt_files, mks_files
def process_path(input_path, output_path, shift_value, direction, recursive=False, overwrite=False, remove_text=None, delete_lines=None, mks_output=False, fps_change=None):
srt_files, mks_files = scan_inputs(input_path, recursive, include_mks=True)
folder_mode = os.path.isdir(input_path)
out_path = os.path.normpath(output_path) if output_path else None
looks_like_dir = False
if output_path:
seps = [os.sep]
if os.altsep:
seps.append(os.altsep)
looks_like_dir = any(output_path.endswith(sep) for sep in seps)
temp_dir = tempfile.mkdtemp(prefix='subfixr_') if mks_files else None
temp_home = {}
if mks_files and not mks_output:
print(f"[+] Extracting subtitles from {len(mks_files)} MKS file(s)...")
for mks_file in mks_files:
work_dir = make_temp_workspace(temp_dir)
try:
job = pull_tracks_from_mks(mks_file, work_dir)
except Exception as exc:
print(f" Error processing {os.path.basename(mks_file)}: {exc}")
continue
home = os.path.dirname(os.path.abspath(mks_file)) or '.'
extracted = 0
skipped_tracks = 0
for track in job['tracks']:
extracted_file = track.get('extracted_path')
if not extracted_file:
skipped_tracks += 1
continue
temp_home[extracted_file] = home
srt_files.append(extracted_file)
extracted += 1
print(f" Extracted {extracted} track(s) from {os.path.basename(mks_file)}")
if skipped_tracks:
print(f" Skipped {skipped_tracks} subtitle track(s) that are not editable as SRT")
print()
out_is_dir = False
if out_path:
out_is_dir = folder_mode or looks_like_dir or os.path.isdir(out_path)
if out_is_dir and os.path.exists(out_path) and os.path.isfile(out_path):
raise ValueError(f"Output path exists as a file, but a folder is required: {out_path}")
if out_is_dir and not os.path.exists(out_path):
os.makedirs(out_path, exist_ok=True)
print(f"[+] Created output folder: {out_path}\n")
if mks_output:
print(f"[+] Found {len(srt_files)} SRT file(s)")
print(f"[+] Found {len(mks_files)} MKS file(s)")
else:
print(f"[+] Found {len(srt_files)} subtitle file(s)")
if fps_change:
print(f"[+] FPS: {fps_change['source_label']} -> {fps_change['target_label']}")
if shift_value:
print(f"[+] Shift: {direction} by {shift_value}")
if delete_lines:
print(f"[+] Delete subtitle blocks: {delete_lines}")
if remove_text:
print(f"[+] Remove lines containing: {remove_text}")
print()
done = 0
skipped = 0
try:
for src in srt_files:
from_temp = False
if temp_dir:
try:
shared = os.path.commonpath([os.path.abspath(temp_dir), os.path.abspath(src)])
from_temp = os.path.abspath(shared) == os.path.abspath(temp_dir)
except (ValueError, OSError):
from_temp = False
if folder_mode or from_temp:
if out_path:
if out_is_dir:
if from_temp:
stem = os.path.splitext(os.path.basename(src))[0]
stem = tweak_lang_in_stem(stem)
dst = os.path.join(out_path, f"{os.path.basename(stem)}.srt")
else:
dst = os.path.join(out_path, tweak_lang_in_filename(os.path.basename(src)))
else:
if from_temp:
stem = os.path.splitext(os.path.basename(src))[0]
stem = tweak_lang_in_stem(stem)
base_out, ext = os.path.splitext(out_path)
if not ext:
ext = '.srt'
if '_track' in stem:
dst = f"{base_out}_track{stem.split('_track', 1)[1]}{ext}"
else:
dst = f"{base_out}_{os.path.basename(stem)}{ext}"
else:
dst = out_path
else:
if from_temp:
home = temp_home.get(src, '.')
stem = os.path.splitext(os.path.basename(src))[0]
stem = tweak_lang_in_stem(stem)
if '_track' in stem:
base_name, tail = stem.split('_track', 1)
dst = os.path.join(home, f"{base_name}_track{tail}.srt")
else:
dst = os.path.join(home, f"{stem}_synced.srt")
else:
base, ext = os.path.splitext(src)
dst = f"{tweak_lang_in_stem(base)}_synced{ext}"
else:
if out_path:
if out_is_dir:
dst = os.path.join(out_path, tweak_lang_in_filename(os.path.basename(src)))
else:
dst = out_path
else:
base, ext = os.path.splitext(src)
dst = f"{tweak_lang_in_stem(base)}_synced{ext}"
if os.path.exists(dst) and not overwrite:
print(f"[-] Skipped: {os.path.basename(src)} (output exists, use --overwrite)")
skipped += 1
continue
try:
fix_one(src, dst, shift_value, direction, remove_text, delete_lines, fps_change)
done += 1
except Exception as exc:
print(f"[-] Error processing {os.path.basename(src)}: {exc}")
skipped += 1
print()
if mks_output:
for src in mks_files:
dst = resolve_mks_output_path(src, out_path, out_is_dir, folder_mode)
if os.path.exists(dst) and not overwrite:
print(f"[-] Skipped: {os.path.basename(src)} (output exists, use --overwrite)")
skipped += 1
print()
continue
if os.path.exists(dst) and overwrite:
try:
os.remove(dst)
except OSError as exc:
print(f"[-] Error processing {os.path.basename(src)}: Could not replace existing output: {exc}")
skipped += 1
print()
continue
work_dir = make_temp_workspace(temp_dir)
try:
job = pull_tracks_from_mks(src, work_dir, strict=True)
process_mks_file(job, dst, shift_value, direction, remove_text, delete_lines, fps_change)
done += 1
except Exception as exc:
print(f"[-] Error processing {os.path.basename(src)}: {exc}")
skipped += 1
print()
finally:
if temp_dir and os.path.exists(temp_dir):
shutil.rmtree(temp_dir, ignore_errors=True)
print(f"[+] Summary: {done} processed, {skipped} skipped")
def main():
parser = argparse.ArgumentParser(
prog='SubFixr',
description='Shift subtitle timing, convert subtitle FPS, remove specific subtitle blocks, or drop blocks that match some text',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python subfixr.py file.srt -s 1s -d forward
Shift subtitles forward by 1 second.
python subfixr.py file.srt --fps 25:23.976
python subfixr.py file.srt --fps 25 23.976
Retime subtitles from 25 FPS to 23.976 FPS.
python subfixr.py folder/ -s 500ms -d backward
Shift all subtitle files in folder/ backward by 500 milliseconds.
python subfixr.py folder/ -s 1min -d forward -o output_folder/ -r
Shift all subtitles forward by 1 minute, save to output_folder/,
and include subfolders.
python subfixr.py temp/ -s 2.5s -d backward --overwrite
Shift subtitles backward by 2.5 seconds and overwrite originals.
python subfixr.py output/ -s 1s -d forward -o output_folder/
Shift subtitles forward by 1 second and save to output_folder/.
python subfixr.py file.srt --delete-lines 1-8
Remove subtitle blocks 1 through 8.
python subfixr.py file.srt --delete-lines 1,3,10-15 -s 750ms -d backward
Remove selected blocks, then shift remaining subtitles backward by 750 ms.
python subfixr.py file.srt --fps 24000/1001:25 -s 300ms -d forward
python subfixr.py file.srt --fps 24000/1001 25 -s 300ms -d forward
Retime from 24000/1001 FPS to 25 FPS, then shift forward by 300 ms.
python subfixr.py file.srt --remove "viki"
Remove subtitle blocks containing "viki".
python subfixr.py folder/ --remove "viki.com" -s 1s -d forward
Remove matching blocks, then shift all subtitles forward by 1 second.
python subfixr.py file.mks -s 1s -d forward -o output_folder/
Extract, shift, and save subtitles from file.mks to output_folder/.
python subfixr.py file.mks --remove "viki" --fps 25:23.976 --mks-output
Remove matching blocks, retime FPS, and rebuild as an MKS file.
python subfixr.py file.srt --delete-lines 1-5 --remove "ads" --fps 25:23.976 -s 1s -d forward
Apply all operations: delete blocks, remove text, retime FPS, then shift.
When multiple options are combined, they are applied in this order:
1. --delete-lines
2. --remove
3. --fps
4. timestamp shift (--shift with --direction)
"""
)
parser.add_argument('input', help='Subtitle file (.srt or .mks) or a folder with subtitle files')
parser.add_argument('-o', '--output', default=None, help='Where to write the result. Defaults to input_synced.srt/.mks or the source folder')
parser.add_argument('-s', '--shift', default=None, help='How much to shift timestamps, for example 1s, 500ms, 1min, or plain seconds like 1.5')
parser.add_argument('--fps', nargs='+', default=None, help='Retiming FPS as source:target or source target, for example --fps 25:23.976 or --fps 25 23.976')
parser.add_argument('-d', '--direction', default='forward', help='Which way to shift: forward/for or backward/back')
parser.add_argument('--delete-lines', default=None, help='Subtitle block numbers or ranges to remove, for example 1-8 or 1,3,5-7')
parser.add_argument('--remove', default=None, help='Remove subtitle blocks that contain this text (case-insensitive)')
parser.add_argument('-r', '--recursive', action='store_true', help='Scan subfolders too when the input is a folder')
parser.add_argument('--overwrite', action='store_true', help='Replace output files if they already exist')
parser.add_argument('--mks-output', action='store_true', help='When the input is .mks, rebuild a new .mks instead of writing extracted .srt tracks')
args = parser.parse_args()
if not args.shift and not args.remove and not args.delete_lines and not args.fps:
parser.error('Nothing to do. Pass at least one of --shift, --fps, --remove, or --delete-lines')
direction = None
if args.shift:
try:
direction = clean_direction(args.direction)
except ValueError as exc:
parser.error(str(exc))
fps_change = None
if args.fps:
try:
fps_change = parse_fps_change(args.fps)
except ValueError as exc:
parser.error(str(exc))
if args.delete_lines:
try:
parse_block_selector(args.delete_lines)
except ValueError as exc:
parser.error(str(exc))
process_path(
args.input,
args.output,
args.shift,
direction,
recursive=args.recursive,
overwrite=args.overwrite,
remove_text=args.remove,
delete_lines=args.delete_lines,
mks_output=args.mks_output,
fps_change=fps_change,
)
if __name__ == '__main__':
main()