-
Notifications
You must be signed in to change notification settings - Fork 1
/
cleanup_drcs.py
191 lines (169 loc) · 7.49 KB
/
cleanup_drcs.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
#!/usr/bin/env python3
#
# Wilhelm/ JPTV.club
#
# History:
# 1.5 (18-Jul-2024) - Updated look up table
# 1.4 (10-Jan-2023) - Converted filter to command, because Windows is weird.
# 1.3 (02-Aug-2022) - Cleanups
# 1.2 (14-Sep-2021) - Merged workarounds for Windows
# 1.1 (12-Sep-2021) - Initial version
#
import re
import sys
import os
import argparse
__author__ = 'Wilhelm/ JPTV.club'
__version__ = '1.5'
# /* look up table -- automatically generated -- */
mappings = {
'063C95566807D5E7B51AB706426BEDF9': '[スマホ]',
'06CB56043B9C4006BCFBE07CC831FEAF': '[スピーカ]',
'0895A30733D262AB1F93A708DD58A18D': '濵',
'0EB2FFE3A6BB50A846711DA465E40278': '[スマホアラート]',
'12A2C7156DA32FC972B5A451BB87B813': '⦅',
'12AECDEA283E4D07F88B9F2B740E4F86': '⚟',
'170193C22E22A88904C34F3EC7129DDB': '[PC]',
'1A0D5448F0C7AD3592867EF1822625D0': '⎚',
'1A563501AFFBF7F5BAEC350A108D5505': '⚟',
'1ACF61D411002097CD9E60E60A835A8F': '[プレーヤー]',
'1E689794776CB5582A1863B4DEE9FEAC': '[ケータイ]',
'1EC68789194EA34C7A8A0B89F9550B1D': '[マイク]',
'21574A6257F78156F74EAE5385997277': '⎚',
'281FC5F4077EA6427747561BC6C60EFA': '葛',
'2A063EDC4770B3403F060B38166A0D4D': '⦅',
'2EFC3F9CB8EA7F1AF59B0E3919EBAAA9': '[TV]',
'32DA82DE0FA624C06429973546962AF8': '[スピーカ]',
'33D4C5243A45503D43FBB858A728664D': '[ケータイ]',
'35E1950772932B5E0265782CD3D3D260': '☎',
'36C49DFC3BDD10E40ED69A6D3C342875': '逢',
'37DDA7E42A71CCE52E92AF10433DCD93': '『',
'37F6ECF37A0A3EF8DFF083CCC8754F81': '♬',
'38566B372F4C5A1AEAD4EFA20DECD079': '⦆',
'3D1BD145819ECE4A33807CE3A4B1EFAA': '[スマホ]',
'44CBC7DC0AF84F6AB857F00A9BC2560A': '⎚',
'4747AF09D9BA2F184CA5D1985910EFE7': '[イアホン]',
'4901D207BFC017051FDCF6629492BF24': '⤴︎',
'4975836AA32BC7BE4376CA1B2FFE226A': 'ì',
'4AD56504A7066270BE7CAD51C0B05E17': '彅',
'4C84BD1AF930BE3E0FDC14944B01867D': '祇',
'516A7B4EB9DE2841903301997E881E9D': '[スピーカ]',
'52B83336967BAEDFD10FB3C0EB88C205': '珉',
'54479AA90145B4713134B78D4FB98AA5': '⦅',
'54809FFDE5588F40F754CBF69A28B404': '弴',
'563E1633D226C10EF4EC80638997E4A9': '『',
'583134B86E7D90960F64C5B863196978': '➡',
'5BB8B7731D9473EBD7C842334DFA24F2': '⦆',
'5C31E7978A711D0CA0469B294CB47CA6': '[スピーカ]',
'5C8022286D3BC941C12E9BBC475255DD': '鷗',
'62985AEEBAEC69314F03FF9D3080ADA2': '鷗',
'6481AA074865672B97996C8989BEC66F': '[ラジオ]',
'650C4061157ECBA687E8934164D83A5F': '樋',
'668EFE227046308AE8E72C016F2EA3D7': '[テープ]',
'68A5D3A27B73B1E679216A6A6437B4DB': '↳',
'68FC649B4A57A6103A25DC678FCEC9F4': '[ケータイ]',
'6D5AA3FF99A144BD5138562787F58590': '[ケータイ]',
'6E1D2C6D71AE08CE7F4CF64F23D97DF4': '[TV]',
'6F03A512FAAD6B4DC4B66DA4818CF6EB': '↴',
'6F15F7D4ADD814229942D280A8ACAE40': '☎',
'6FD7DBAC9AD5D605768457F173A1CBD1': '[TV]',
'7160F7419CBA7ACDACD23CBEB4834DBE': '⦅',
'7491E4734BF93C5064B077318BC82EC1': '[スマホ]',
'74D535CA9F47D57FD78234F7019A525E': '⚟',
'7542BC0875D546542D2435DAA99821BB': '[ケータイ]',
'762CBC6B6F0F7132973C0F6CEB4141C6': '☎',
'7778FA83E2DD213935B75951A07FCCAD': '𣝣',
'7BFEE4DD2D8C5478F86A169EA60AA03D': '[ケータイ]',
'7CAF343B9E56DC14B775080F944E21FD': '[スピーカ]',
'804A5BCDCBF1BA977C92D3D58A1CDFE1': '[スピーカ]',
'83558EE0307101A7D50EC0904F0CEA52': '』',
'86AED3FE53AD8F629253795C87452FAB': '[ケータイ]',
'8B6444BE18F269AC615643B26F9E3041': '『',
'8E5B873AC8E1BF84246B281B3548C2FF': '↴',
'90FA5A71875E1E0D52A623465A7043AB': '﨑',
'9140C12A79C679589C6E4546924D7E07': 'è',
'929CA2E2768BEDD4A812916925F1266B': '[TV]',
'94FB7BE756372DB6B62E3E0A119083D5': '⚞',
'953993322785AB4152AC0D3747D7ACF5': '『',
'97CDEDD3618D1510CC3904D4652F5790': '[スマホ]',
'9D15C0395A4738936AF34308ACF2D032': '⦅',
'9EE59C7D2C202E0214836A0138F59E24': '』',
'9FFA7E00CFC7E807A161ADA460B8060C': '⦆',
'A191DCAE8572D65EBB1411F17311076A': '『',
'A1ACA3019ACEBE43380AC4F1033B2A8D': '﨑',
'A368B4CE2212EF80E2BF3D68559F5151': '⎚',
'A58DC0E1271B03A5981B57A83271AFA7': '⦆',
'A78D9B65F46654601CE0145622164B47': '↴',
'A97B575907C06F39C8AC0ED26C49E328': '[スマホ]',
'AD63C1574C1F4B556B157A0D5FD855FF': '[スマホ]',
'B1177C2EA37A69B67747287050F28F41': '樋',
'B1ABBE58BAC2342AE82DF6C19FB2D192': '遡',
'B3E0BE16A1818CD8D4CC5E1D20B27CBC': '[TV]',
'B84B5D9232B091B461BAA1C07950E76B': '﨑',
'C01D2BAFCE469DA1ABBB612FDB16C1E3': '元',
'C06A278CFFC82DDC67709E5E9577F148': '↱',
'C08E27670940B3A3DC8122586E144B44': '⎚',
'C35C7E6816E10BE8304F2D876426C9E7': '[ケータイ]',
'C42450812C184AEE2AE01CC5E39AE957': '[ラジオ]',
'C7A45CD980247B7971BF223F1A71607F': '[PC]',
'CCB96A769F9CD55EBDF67B5C03495C0E': '﨑',
'CEF8C8FE116047F9B3214D0DD145EA32': '[CD]',
'D4F0B6247BBB9102758D1F4A06506C9F': '薩',
'DE7F3B9A7048C3A1D5EFE8DEE33953FD': '』',
'E214599903C94C532684BDF54B62DF61': '⦆',
'E3587F9C5CF08D369D50A459F1D23723': '[TV]',
'E67210B0DA0161D36B79E8C9BE6A9D0C': '⦆',
'E702912587801D73D58CDB30E48DEBED': '』',
'E8029D0C2C333D626F025E2CA80A9B02': '[スマホ]',
'E806D1481CFA721DA5F60413531F39BD': '[ケータイ]',
'F30294B4B8B6C05E9418B1188CE72742': '[イアホン]',
'F500B2C97A8D61A7440472C5E397E631': '彅',
'FBD48A799B4F6802745508A76590E3BC': '[ケータイ]',
'FE091DA3F60998F83773AEDED2D327DA': '[ケータイ]',
'FE720D2A491D8A4441151C49CD8AB4F6': '[PC]',
}
# /* end look up table */
unknowns = set() # create a "unique list"
def mycallback(match):
myhash = (match.group(0)).upper()[3:-1]
if myhash in mappings:
replacement = mappings[myhash]
if replacement[:2] == 'U+':
return chr(int(replacement.lstrip("U+").zfill(8), 16))
return replacement # assume an already prepared string here
print("Unknown DRCS hash: 0x", myhash, sep='', file=sys.stderr)
unknowns.add(myhash)
return '〓'
def main():
ap = argparse.ArgumentParser(
description=('Replaces known hashed ARIB DRCS symbols in subtitles files with human-readable equivalents.'),
epilog='Report bugs, request features, or provide suggestions via https://github.com/TurtleWilly/arib-drcs-symbols/issues',
)
ap.add_argument('-V', '--version', action='version', help="show version number and exit", version='%(prog)s {}'.format(__version__), )
ap.add_argument('input', type=str, help='input filename/path (*.ass, *.srt, etc.)')
ap.add_argument('-o', '--output', metavar='FILENAME', type=str, help='output filename/path, if omitted it will be generated automatically')
user_input = ap.parse_args()
filename_in = os.path.realpath(os.path.expanduser(user_input.input))
if user_input.output:
filename_out = os.path.realpath(os.path.expanduser(user_input.output))
else:
(root, extension) = os.path.splitext(filename_in)
filename_out = root + '_fixed' + extension
cached = re.compile('\[外:([0-9A-Fa-f]{32})\]')
try:
with open(filename_in, 'rt', encoding='utf8', newline=None) as fi, open(filename_out, 'xt', encoding='utf8', newline='\n') as fo:
while True:
line = fi.readline()
if not line: # end of file?
break
fo.write(cached.sub(mycallback, line.strip()) + '\n')
if '[ARIBEXT:' in line or '・' in line:
print("Warning: potential ARIB STD B24 extended character in line: <", line, ">", sep='', file=sys.stderr)
except FileExistsError as e:
print('Output file "{}" already exists.'.format(filename_out), file=sys.stderr)
except OSError as e:
print('Operation failed: {}'.format(e.strerror), file=sys.stderr)
if unknowns:
print("All unknown DRCS hashes: ", unknowns, file=sys.stderr)
if __name__ == "__main__":
main()