-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanime_lang_track_corrector.py
1022 lines (879 loc) · 35.7 KB
/
anime_lang_track_corrector.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
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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import argparse
import os
import platform
import re
import subprocess
from datetime import datetime
import fasttext
import pymkv
from chardet.universaldetector import UniversalDetector
from discord_webhook import DiscordWebhook
from langcodes import *
from pysubparser import parser
from settings import *
# Determine the user's operating system
user_os = platform.system()
# FastText Model Location
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
PRETRAINED_MODEL_PATH = os.path.join(ROOT_DIR, fasttext_model_name)
model = fasttext.load_model(PRETRAINED_MODEL_PATH)
# Subtitle extraction location
subtitle_location = os.path.join(ROOT_DIR, "subs_test")
# The linux location for SE
path_to_subtitle_edit_linux = os.path.join(ROOT_DIR, "se")
# If for some reason the se folder doesn't exist, create it
if not os.path.isdir(path_to_subtitle_edit_linux):
try:
os.mkdir(path_to_subtitle_edit_linux)
if os.path.isdir(path_to_subtitle_edit_linux):
print("\nCreated directory: " + path_to_subtitle_edit_linux)
else:
print("\nFailed to create directory: " + path_to_subtitle_edit_linux)
exit()
except OSError as e:
print("\nFailed to create directory: " + path_to_subtitle_edit_linux)
print("Error: " + str(e))
exit()
se_download_link = "https://github.com/SubtitleEdit/subtitleedit/releases"
# if the user's os is linux and there's no files in the se folder, let them know and exit
if user_os == "Linux":
# if the file count isn't bigger than 1, then there's no files in the se folder
# bigger than one, because github requries a file for the folder to be created
se_file_count = [
name
for name in os.listdir(path_to_subtitle_edit_linux)
if not name.startswith(".")
]
if len(se_file_count) <= 1:
print(f"\nSubtitleEdit not found!")
print(f"Download it at: {se_download_link}")
print(f"Place it in: {path_to_subtitle_edit_linux}")
exit()
# if subtitle_location does not exist, create it
if not os.path.isdir(subtitle_location):
try:
os.mkdir(subtitle_location)
if os.path.isdir(subtitle_location):
print("\nCreated directory: " + subtitle_location)
else:
print("\nFailed to create directory: " + subtitle_location)
exit()
except OSError as e:
print("\nFailed to create directory: " + subtitle_location)
print("Error: " + str(e))
exit()
# The required percentage that must be met when detecting an individual language with FastText
required_lang_match_percentage = 70
# Used to determine the total execution time at the end
startTime = datetime.now()
# Stuff printed at the end
items_changed = []
errors = []
# Signs & Full keyword arrays, add any keywords you want to be searched for
signs_keywords = ["sign", "music", "song"]
full_keywords = ["full", "dialog", "dialogue", "english subs"]
p = argparse.ArgumentParser(
description="A script that corrects undetermined and not applicable subtitle flags within mkv files for anime."
)
p.add_argument(
"-p",
"--path",
help="The path to the anime folder to be scanned recursively.",
required=False,
)
p.add_argument(
"-f", "--file", help="The individual video file to be processed.", required=False
)
p.add_argument(
"-wh",
"--webhook",
help="The optional discord webhook url to be pinged about changes and errors.",
required=False,
)
p.add_argument(
"-lmp",
"--lang-match-percentage",
help="The percentage of the detected file language required for the language to be set.",
required=False,
)
args = p.parse_args()
# parse the arguments
if args.path:
path = args.path
else:
args.path = None
if args.file:
file = args.file
path = os.path.dirname(file)
else:
args.file = None
if args.webhook:
discord_webhook_url = args.webhook
else:
discord_webhook_url = ""
if args.lang_match_percentage:
required_lang_match_percentage = int(args.lang_match_percentage)
# Removes the file if it exists, used for cleaning up after FastText detection
def remove_file(file, silent=False):
if os.path.isfile(file):
try:
os.remove(file)
if not os.path.isfile(file):
if not silent:
print("\n\t\tFile removed:", file)
else:
send_error_message("\t\tFailed to remove file:", file)
except OSError as e:
send_error_message(
"\t\tFailed to remove file:", file, "\n\t\tError:", str(e)
)
else:
print("\t\tFile does not exist before attempting to remove: " + file)
# Detects the encoding of the supplied subtitle file
def detect_subtitle_encoding(output_file_with_path):
try:
with open(output_file_with_path, "rb") as file:
detector = UniversalDetector()
for line in file:
detector.feed(line)
if detector.done:
break
detector.close()
encoding = detector.result["encoding"]
except FileNotFoundError:
send_error_message("File not found: " + output_file_with_path)
print("Defaulting to UTF-8")
encoding = "UTF-8"
return encoding
# Appends, sends, and prints our error message
def send_error_message(message):
errors.append(message)
send_discord_message(message)
print(message)
# Appends, sends, and prints our change message
def send_message(message, add_to_changed=False):
if message:
if add_to_changed:
items_changed.append(message)
send_discord_message(message)
print(message)
# Sends a discord message
def send_discord_message(message):
message = str(message)
if discord_webhook_url:
webhook = DiscordWebhook(
url=discord_webhook_url, content=message, rate_limit_retry=True
)
webhook.execute()
# Prints the information about the given track
def print_track_info(track):
print(f"\n\t\tTrack: {track.track_id}")
print(f"\t\tType: {track._track_type}")
print(f"\t\tName: {track.track_name}")
print(f"\t\tLanguage: {track.language}")
print(f"\t\tCodec: {track.track_codec}")
if track._track_type == "subtitles":
print(f"\t\tForced: {track.forced_track}")
# Determines and sets the file extension
def set_extension(track):
if track.track_codec in ["SubStationAlpha", "AdvancedSubStationAlpha"]:
return "ass"
elif track.track_codec == "SubRip/SRT":
return "srt"
elif track.track_codec == "HDMV PGS":
return "pgs"
elif track.track_codec == "VobSub":
return "sub"
else:
return ""
# Removes hidden files from list, useful for MacOS
def remove_hidden_files(files, root):
for file in files[:]:
if file.startswith(".") and os.path.isfile(os.path.join(root, file)):
files.remove(file)
# execute command with subprocess and reutrn the output
def execute_command(command):
process = None
try:
process = subprocess.Popen(command, stdout=subprocess.PIPE)
while True:
output = process.stdout.readline()
if output == b"" and process.poll() is not None:
break
if output:
sys.stdout.buffer.write(output)
sys.stdout.flush()
except Exception as e:
send_error_message(str(e))
return process
def extract_output_subtitle_file_and_convert(file_name, track, full_path, root):
outputted_file = os.path.join(root, file_name)
call = execute_command(
[
"mkvextract",
"tracks",
full_path,
str(track.track_id) + ":" + outputted_file,
]
)
if os.path.isfile(outputted_file) and call.returncode == 0:
print("\t\tExtraction successful.")
print("\t\tConverting subtitle for detection.")
converted = convert_subtitle_file(outputted_file, os.path.basename(full_path))
if converted is not None and os.path.isfile(converted):
return converted
else:
print("\t\tConversion failed.")
else:
send_error_message("Extraction failed: " + outputted_file + "\n")
def set_track_language(path, track, language_code):
try:
execute_command(
[
"mkvpropedit",
path,
"--edit",
"track:" + str(track.track_id + 1),
"--set",
"language=" + language_code,
]
)
send_message(
f"\t\tFile: {path}\n\t\tTrack: {track.track_id + 1} set to: {language_code}",
True,
)
except Exception as e:
send_error_message(f"{e} File: {path}")
def check_and_set_result_two(
match_result, full_path, track, lang_code, output_file_with_path, root, tracks
):
file = os.path.basename(full_path)
match_result_percent = f"{match_result}%"
if match_result >= required_lang_match_percentage:
full_language_keyword = Language.make(
language=standardize_tag(lang_code)
).display_name()
send_message(f"\n\t\tFile: {file}\n\t\tMatch: {match_result_percent}")
print(f"\t\tSubtitle file detected as {full_language_keyword}")
set_track_language(full_path, track, lang_code)
return 1
else:
send_error_message(
f"\n\t\tFile: {file}\n\t\tMatch: {match_result_percent}\n\t\tSubtitle match below {required_lang_match_percentage}%, no match found.\n"
)
return 0
def check_and_set_result(
match_result,
full_path,
track,
lang_code,
output_file_with_path,
original_subtitle_array,
root,
tracks,
):
file = os.path.basename(full_path)
match_result_percent = f"{match_result}%"
if match_result >= required_lang_match_percentage:
send_message(f"\n\t\tFile: {file}\n\t\tMatch: {match_result_percent}")
print(f"\t\tSubtitle file detected as {lang_code}")
set_track_language(full_path, track, lang_code)
else:
error_message = (
f"\n\t\tFile: {file}\n\t\tMatch: {match_result_percent}\n\t\t"
f"Subtitle match below {required_lang_match_percentage}%, no match found.\n"
)
send_error_message(error_message)
if match_result >= 10:
remove_signs_and_subs(
files, file, original_subtitle_array, tracks, root, track, file
)
lang_codes = [
"eng",
"spa",
"por",
"fra",
"deu",
"ita",
"jpn",
"kor",
"pol",
"rus",
"swe",
"tur",
"vie",
"ara",
"heb",
"cat",
"ces",
"dan",
"ell",
"fin",
"hun",
"ind",
"nor",
"nld",
"ron",
"slk",
"slv",
"srp",
"ukr",
"zho",
]
def detect_subs_via_fasttext(track, extension, root, full_path, tracks):
lang_keyword_search = ""
lang_keyword_search_short = ""
if track.track_name:
for code in lang_codes:
lang_keyword_search = search_track_for_language_keyword(
path, track, code, root, full_path
)
if lang_keyword_search:
break
if not lang_keyword_search:
for code in lang_codes:
lang_keyword_search_short = search_track_for_language_keyword(
path, track, code[:-1], root, full_path
)
if lang_keyword_search_short:
break
if not lang_keyword_search and not lang_keyword_search_short:
print("\n\t\tNo language keyword found in track name.")
print("\t\tFile will be extracted and detection will be attempted.")
print("\t\tExtracting test file to " + subtitle_location)
try:
output_file_with_path = extract_output_subtitle_file_and_convert(
"lang_test." + extension, track, full_path, subtitle_location
)
if output_file_with_path:
subtitle_lines_array = parse_subtitle_lines_into_array(
output_file_with_path
)
match_result = evaluate_subtitle_lines(subtitle_lines_array)
if len(match_result) >= 2 and match_result[1] != 0:
if standardize_tag(track.language) != standardize_tag(
match_result[0]
):
check_and_set_result(
match_result[1],
full_path,
track,
match_result[0],
output_file_with_path,
subtitle_lines_array,
root,
tracks,
)
else:
print("\t\tCorrect language already set.")
except Exception as e:
send_error_message(str(e))
return
else:
return True
def clean_subtitle_lines(lines):
cleaned_lines = []
if lines:
for line in lines:
if isinstance(line, str):
text = line
elif hasattr(line, "text"):
text = line.text
else:
continue
clean_one = re.sub(r"(^[a-z$&+,:;=?@#|'<>.^*()%!-]*;)", "", text)
clean_two = re.sub(r"[0-9$&+,:;=?@#|'<>.^*()%!-]", " ", clean_one)
clean_three = re.sub("(\s{2,})", " ", clean_two).strip() # Excess space
if re.search(r"^(\w\s){3,}", clean_three): # EX: 'D b b l l b''
continue
if len(clean_three) > 4:
cleaned_lines.append(clean_three)
return cleaned_lines
def evaluate_subtitle_lines(subtitles):
cleaned_subtitles = clean_subtitle_lines(subtitles)
results = []
if not cleaned_subtitles:
return "", 0
for subtitle in cleaned_subtitles:
try:
result = model.predict(subtitle)
result = re.sub(r"__label__", "", result[0][0])
print(f'\t\tLanguage Detected: {result} on "{subtitle}"\t')
results.append(result)
except Exception as e:
send_error_message(
"Error determining result of subtitle:",
str(subtitle),
"\n\t\tError:",
str(e),
)
language_counts = {result: results.count(result) for result in results}
highest_lang_result = max(language_counts, key=language_counts.get)
highest_lang_result_percent = (
language_counts[highest_lang_result] / len(cleaned_subtitles)
) * 100
return highest_lang_result, highest_lang_result_percent
def parse_subtitle_lines_into_array(input_file):
extension = os.path.splitext(input_file)[1].strip(".")
subtitles = parser.parse(
input_file,
subtitle_type=extension,
encoding=detect_subtitle_encoding(input_file),
)
return list(subtitles)
def convert_subtitle_file(subtitle_file, source_file):
if subtitle_file.endswith(".srt"):
return subtitle_file
processing_options = [
"srt",
"/RemoveFormatting",
"/MergeSameTexts",
"/overwrite",
]
if user_os == "Windows":
call = [
"SubtitleEdit",
"/convert",
"\\\\?\\{}".format(subtitle_file),
]
call.extend(processing_options)
# call = 'SubtitleEdit /convert "\\\\?\\{}" srt /RemoveFormatting /MergeSameTexts /overwrite'.format(
# subtitle_file
# )
elif user_os == "Linux":
call = [
"xvfb-run",
"-a",
"mono",
os.path.join(path_to_subtitle_edit_linux, "SubtitleEdit.exe"),
"/convert",
subtitle_file,
]
call.extend(processing_options)
try:
result = execute_command(call)
converted_file = os.path.splitext(subtitle_file)[0] + ".srt"
if os.path.isfile(converted_file) and result.returncode == 0:
print("\t\tConversion successful.")
return converted_file
else:
send_error_message(
f"Conversion failed on: {subtitle_file} from {source_file}"
)
except Exception as e:
print("Subprocess error:", e)
def find_files_by_release_group(release_group, files):
return [
file for file in files if re.search(release_group, file, flags=re.IGNORECASE)
]
def get_mkv_tracks(full_path):
mkv = pymkv.MKVFile(full_path)
tracks = mkv.get_track()
return tracks
def remove_all_tracks_but_subtitles(tracks):
clean = [track for track in tracks if track._track_type == "subtitles"]
return clean
def print_similar_releases(comparision_releases):
if comparision_releases:
for release in comparision_releases:
print("\t\t" + release)
else:
print("\t\tNo comparision releases found.")
def check_tracks(tracks, comparision_full_path, original_files_results, root, track):
send_message("\t\tChecking internal subtitle tracks for a comparision.")
# The number of pgs subs that can be used for comparision, per file.
# Since OCR'ing can take a long time, and end up in an endless loop
# of OCR'ing all the PGS subs in a 24 episode season.
pgs_limit = 2
pgs_count = 0
for comparision_track in tracks:
if comparision_track._track_type == "subtitles":
print_track_info(comparision_track)
if comparision_track.track_codec == "HDMV PGS":
pgs_count += 1
if pgs_count > pgs_limit:
print("\n\t\tSkipping PGS, limit reached.")
continue
extension = set_extension(comparision_track)
output_file_with_path = extract_output_subtitle_file_and_convert(
"lang_comparison" + "." + extension,
comparision_track,
comparision_full_path,
subtitle_location,
)
if output_file_with_path is not None:
comparision_subtitle_lines_array = parse_subtitle_lines_into_array(
output_file_with_path
)
comparision_subtitle_lines_array = clean_subtitle_lines(
comparision_subtitle_lines_array
)
duplicates_removed = 0
removed = []
for result in comparision_subtitle_lines_array[:]:
if result in original_files_results:
original_files_results.remove(result)
print("\t\tDuplicate removed from original: " + result)
removed.append(result)
duplicates_removed += 1
if duplicates_removed > 1:
print("\t\t-- Comparision Attempt --")
print(
"\t\tEnough duplicates found between original and comparision."
)
print("\t\tRetesting original with duplicates removed.")
match_result = evaluate_subtitle_lines(original_files_results)
if len(match_result) >= 2 and match_result[1] != 0:
if standardize_tag(match_result[0]) != standardize_tag(
track.language
):
set_result = check_and_set_result_two(
match_result[1],
comparision_full_path,
track,
match_result[0],
output_file_with_path,
root,
tracks,
)
print("\t\t-- Comparision Attempt --")
if set_result == 1:
return True
else:
print("\t\tNot enough duplicates found in track.")
send_message("\t\tLanguage could not be determined through internal tracks.")
send_message("\t\tChecking externally...")
return False
def remove_signs_and_subs(
files, original_file, original_files_results, tracks, root, track, file
):
original_files_results = clean_subtitle_lines(original_files_results)
tracks.remove(track)
if not check_tracks(
tracks, os.path.join(root, file), original_files_results, root, track
):
original_file_releaser = re.search(r"-(?:.(?!-))+$", original_file)
original_file_releaser = re.sub(
r"([-\.])(mkv)", "", original_file_releaser.group()
)
original_file_releaser = re.sub(r"-", "", original_file_releaser).lower()
if original_file_releaser:
comparision_releases = find_files_by_release_group(
original_file_releaser, files
)
if comparision_releases:
send_discord_message(
"\n\t\t- Checking Similar Releases to ["
+ original_file_releaser
+ "] -"
)
comparision_releases.remove(original_file)
# limit comparision releases to 3
if len(comparision_releases) > 3:
print("\t\tLimiting comparision releases to 3.")
comparision_releases = comparision_releases[:3]
print("\n")
print_similar_releases(comparision_releases)
# The number of pgs subs that can be used for comparision, per file.
# Since OCR'ing can take a long time, and end up in an endless loop
# of OCR'ing all the PGS subs in a 24 episode season.
pgs_limit = 4
try:
pgs_count = 0
for f in reversed(comparision_releases):
print("\n\t\tFile: " + f)
comparision_full_path = os.path.join(root, f)
tracks = get_mkv_tracks(comparision_full_path)
tracks = remove_all_tracks_but_subtitles(tracks)
print("\n\t\t--- Tracks [" + str(len(tracks)) + "] ---")
for comparision_track in tracks:
if comparision_track._track_type == "subtitles":
print_track_info(comparision_track)
if comparision_track.track_codec == "HDMV PGS":
pgs_count += 1
if pgs_count > pgs_limit:
print("\n\t\tSkipping PGS, limit reached.")
continue
extension = set_extension(comparision_track)
output_file_with_path = (
extract_output_subtitle_file_and_convert(
"lang_comparison" + "." + extension,
comparision_track,
comparision_full_path,
subtitle_location,
)
)
if output_file_with_path:
comparision_subtitle_lines_array = (
parse_subtitle_lines_into_array(
output_file_with_path
)
)
comparision_subtitle_lines_array = (
clean_subtitle_lines(
comparision_subtitle_lines_array
)
)
duplicates_removed = 0
removed = []
for result in comparision_subtitle_lines_array[:]:
if result in original_files_results:
original_files_results.remove(result)
print(
"\t\tDuplicate removed from original: "
+ result
)
removed.append(result)
duplicates_removed += 1
if duplicates_removed > 1:
print("\t\t-- Comparison Attempt --")
print(
"\t\tEnough duplicates found between original and comparison."
)
print(
"\t\tRetesting original with duplicates removed."
)
match_result = evaluate_subtitle_lines(
original_files_results
)
if (
len(match_result) >= 2
and match_result[1] != 0
):
if standardize_tag(
track.language
) != standardize_tag(match_result[0]):
set_result = check_and_set_result_two(
match_result[1],
full_path,
track,
match_result[0],
output_file_with_path,
root,
tracks,
)
print("\t\t-- Comparison Attempt --")
if set_result == 1:
return
else:
print(
"\t\tNot enough duplicates found in track."
)
except Exception as e:
send_error_message(str(e))
return
else:
send_message(
"\t\tNo similar release found for: " + file + " at " + root
)
else:
send_message("\t\tSuccessfully set through internal subs")
def clean_and_sort(files, root, dirs):
remove_hidden_files(files, root)
if len(ignored_folder_names) != 0:
dirs[:] = [d for d in dirs if d not in ignored_folder_names]
dirs.sort()
files.sort()
for file in files[:]:
fileIsTrailer = str(re.search("trailer", str(file), re.IGNORECASE))
fileEndsWithMKV = file.endswith(".mkv")
if not fileEndsWithMKV or fileIsTrailer != "None":
files.remove(file)
def search_track_for_language_keyword(path, track, lang_code, root, full_path):
if not track.track_name:
return False
full_language_keyword = Language.make(
language=standardize_tag(lang_code)
).display_name()
track_name = str(track.track_name)
if re.search(
full_language_keyword,
track_name,
re.IGNORECASE,
) or re.search(rf"\b{lang_code}\b", track_name, re.IGNORECASE):
if standardize_tag(track.language) != standardize_tag(lang_code):
send_message(
"\t\tFile: "
+ full_path
+ "\n\t\t\t"
+ full_language_keyword
+ " keyword found in track name."
)
set_track_language(full_path, track, lang_code)
else:
print(
"\t\tFile: "
+ full_path
+ "\n\t\t\t"
+ full_language_keyword
+ " keyword found in track name."
)
print("\n\t\tCorrect language already set.")
return True
return False
# The execution start of the program
if discord_webhook_url != "":
send_message("")
send_message("\n[START]-------------------------------------------[START]")
send_message("Start Time: " + str(datetime.now()))
send_message("Script: anime_lang_track_corrector.py")
send_message("Path: " + path)
def check_for_sign_keywords(track_name, track):
for sign in signs_keywords:
if re.search(sign, track_name, re.IGNORECASE):
return True
return False
def start(files, root, dirs):
clean_subtitle_location() # clean out the subs_test folder
for file in files:
full_path = os.path.join(root, file)
file_without_extension = os.path.splitext(full_path)[0]
if os.path.isfile(full_path):
print(f"\n\tPath: {root}")
print(f"\tFile: {file}")
try:
if file.endswith(".mkv"):
tracks = get_mkv_tracks(full_path)
track_counts = count_tracks(tracks)
print(f"\n\t\t--- Tracks [{len(tracks)}] ---")
handle_tracks(tracks, track_counts, root, full_path)
except Exception as e:
send_error_message(f"\tError with file: {file} ERROR: {str(e)}")
else:
send_error_message(
f"\n\tNot a valid file (do you have mkvtoolnix installed?): {full_path}\n"
)
clean_subtitle_location() # clean out the subs_test folder
def count_tracks(tracks):
track_counts = {
"jpn_audio": 0,
"eng_audio": 0,
"jpn_subtitle": 0,
"eng_subtitle": 0,
"unknown_audio": 0,
"unknown_subtitle": 0,
}
for track in tracks:
if track._track_type == "audio":
if track.language in ["jpn", "jp"]:
track_counts["jpn_audio"] += 1
elif track.language in ["eng", "en"]:
track_counts["eng_audio"] += 1
else:
track_counts["unknown_audio"] += 1
elif track._track_type == "subtitles":
if track.language in ["jpn", "jp"]:
track_counts["jpn_subtitle"] += 1
elif track.language in ["eng", "en"]:
track_counts["eng_subtitle"] += 1
else:
track_counts["unknown_subtitle"] += 1
return track_counts
# Removes all files from subtitle_location
def clean_subtitle_location():
if os.path.isdir(subtitle_location):
files = os.listdir(subtitle_location)
if files:
for file in files:
remove_file(os.path.join(subtitle_location, file), silent=True)
def handle_tracks(tracks, track_counts, root, full_path):
jpn_audio_count = track_counts["jpn_audio"]
eng_audio_count = track_counts["eng_audio"]
jpn_subtitle_count = track_counts["jpn_subtitle"]
eng_subtitle_count = track_counts["eng_subtitle"]
unknown_audio_count = track_counts["unknown_audio"]
unknown_subtitle_count = track_counts["unknown_subtitle"]
total_tracks = sum(track_counts.values())
failed_elimination_text = (
"Language could not be determined through process of elimination."
)
for track in tracks:
clean_subtitle_location() # clean out the subs_test folder
print_track_info(track)
if track._track_type != "subtitles":
continue
extension = set_extension(track)
if track.language in track_languages_to_check:
done = False
print("\n\t\tChecking track...")
if str(track.track_name) != "None":
sign = check_for_sign_keywords(track.track_name, track)
if sign != "None":
print("\t\tTrack name contains a Signs keyword.")
if total_tracks > 0 and total_tracks % 2 == 0:
if unknown_audio_count == 0 and unknown_subtitle_count == 1:
if (
total_tracks
- (
jpn_audio_count
+ eng_audio_count
+ jpn_subtitle_count
+ eng_subtitle_count
)
== unknown_subtitle_count
):
send_message(
"\tTrack determined to be English through process of elimination."
)
set_track_language(full_path, track, "eng")
done = True
elif eng_audio_count == 0:
print("\t\tNo recognized name track.")
if jpn_subtitle_count == 0 and jpn_audio_count == 1:
if eng_subtitle_count == 0 and eng_audio_count == 0:
if unknown_audio_count == 0 and unknown_subtitle_count == 1:
if (
total_tracks
- (jpn_audio_count + jpn_subtitle_count)
== unknown_subtitle_count
):
send_message(
"\tTrack determined to be English through process of elimination."
)
set_track_language(full_path, track, "eng")
done = True
else:
print(
f"\t\tTrack name is empty, TRACK: {str(track.track_id)} on {full_path}"
)
errors.append(
f"Track name is empty, TRACK: {str(track.track_id)} on {full_path}"
)
if not done:
print(f"\t\t{failed_elimination_text}")
detect_subs_via_fasttext(track, extension, root, full_path, tracks)
else:
print("\n\t\tNo matching track found.\n")
if __name__ == "__main__":
if args.path and args.file:
send_error_message("\n\tCannot use both --path and --file at the same time.\n")
elif args.path:
if os.path.isdir(path):
os.chdir(path)
for root, dirs, files in os.walk(path):
clean_and_sort(files, root, dirs)
print("\nCurrent Path: ", root + "\nDirectories: ", dirs)
print("Files: ", files)
start(files, root, dirs)
else:
send_error_message("\n\tNot a valid path: " + path + "\n")
elif args.file: