12
12
from fontTools import ttLib , subset
13
13
from chardet .universaldetector import UniversalDetector
14
14
from chardet import detect
15
- import os , sys , re , winreg , zlib , json , copy , traceback , shutil , configparser , time , locale , codecs
15
+ import os , sys , re , winreg , zlib , json , copy , traceback , shutil , configparser , time , locale , codecs , argparse
16
16
from os import path
17
17
from colorama import init
18
18
from datetime import datetime
@@ -1143,6 +1143,9 @@ def outputSameLength(s: str) -> str:
1143
1143
return output + '' .join ([' ' for _ in range (0 , 60 - length )])
1144
1144
1145
1145
1146
+ # 当前字体缓存版本,小于该版本的字体缓存会被删除重建
1147
+ currentFontCacheVer = 2
1148
+
1146
1149
# font_info 列表结构
1147
1150
# [ font_name, font_n_lower, font_family, warning_font, font_all ]
1148
1151
# font_name 词典结构
@@ -1228,6 +1231,11 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
1228
1231
jsonFile = open (fontCacheFile , mode = 'rb' )
1229
1232
fontCache [dirCRCKey ] = json .loads (zlib .decompress (jsonFile .read ()).decode ('utf-8' ))
1230
1233
jsonFile .close ()
1234
+
1235
+ if fontCache [dirCRCKey ].get ('?version' , 0 ) < currentFontCacheVer :
1236
+ print ('\n \033 [1;31m[WARNING] 已移除旧版本字体缓存文件 \" {}\" \033 [0m' .format (dirCRCKey ))
1237
+ os .remove (fontCacheFile )
1238
+ fontCache .pop (dirCRCKey )
1231
1239
except :
1232
1240
print ('\n \033 [1;31m[ERROR] 缓存文件读取失败 \" {}\" \033 [0m' .format (dirCRCKey ))
1233
1241
os .remove (fontCacheFile )
@@ -1298,13 +1306,13 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
1298
1306
if len (fontCRCKey ) == 0 :
1299
1307
fontCRCKey = hex (zlib .crc32 ((s .lower () + str (path .getsize (s )) + str (path .getctime (s ))).encode ('utf-8' )))[2 :].upper ().rjust (8 , '0' )
1300
1308
if not fontCacheN .get (dirCRCKey , False ):
1301
- fontCacheN [dirCRCKey ] = {fontCRCKey : {}}
1309
+ fontCacheN [dirCRCKey ] = {fontCRCKey : {}, '?version' : currentFontCacheVer }
1302
1310
else :
1303
1311
fontCacheN [dirCRCKey ].update ({fontCRCKey : {}})
1304
1312
1305
1313
# 处理字体信息
1306
1314
for ti , t in enumerate (tc ):
1307
-
1315
+
1308
1316
tencodings = []
1309
1317
1310
1318
if readFontCache is None :
@@ -1438,7 +1446,15 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
1438
1446
else :
1439
1447
font_family [fN ].setdefault ((isItalic , isBold ), [namestr ])
1440
1448
1441
- if readFontCache is None : t .close ()
1449
+ if len (dictFamilyN ) >= 1 :
1450
+ for k , v in dictFamilyN .items ():
1451
+ if k == 'other' : continue
1452
+ if k not in dictNameStr :
1453
+ namestr = list (dictNameStr .values ())[- 1 ][0 ]
1454
+ if namestr .strip (' ' ):
1455
+ font_family [v ].setdefault ((isItalic , isBold ), [namestr ])
1456
+
1457
+ if readFontCache is None and ti == len (tc ) - 1 : t .close ()
1442
1458
1443
1459
f_all_item = (dictNameStr , isItalic , isBold , dictFstyle , dictFamilyN )
1444
1460
if s in f_all :
@@ -1455,7 +1471,12 @@ def fontProgress(fl: list, font_info: list = [{}, {}, {}, {}, {}], overwrite: bo
1455
1471
keys = list (font_family .keys ())
1456
1472
for k in keys :
1457
1473
if not len (font_family [k ]) > 1 :
1458
- font_family .pop (k )
1474
+ keepFamily = False
1475
+ for v in font_family [k ].values ():
1476
+ if v != k :
1477
+ keepFamily = True
1478
+ if not keepFamily :
1479
+ font_family .pop (k )
1459
1480
del keys
1460
1481
1461
1482
dupfont_cache = dupfont
@@ -1525,6 +1546,7 @@ def checkAssFont(fontlist: dict, font_info: list, fn_lines: list = [], onlycheck
1525
1546
ignoreLostFonts = noRequestFont
1526
1547
1527
1548
keys = list (fontlist .keys ())
1549
+
1528
1550
for s in keys :
1529
1551
isbold = s [2 ]
1530
1552
isitalic = s [1 ]
@@ -3687,31 +3709,31 @@ def cFontSubset(font_info):
3687
3709
elif work in [1 , 2 , 20 , 21 ]:
3688
3710
cls ()
3689
3711
if work == 1 :
3690
- print ('''子集化字体
3691
- 搜索子目录(视频): \033 [1;33m{0 }\033 [0m
3692
- 搜索子目录(字幕): \033 [1;33m{1 }\033 [0m
3693
- 严格字幕匹配: \033 [1;33m{2 }\033 [0m
3694
- 字幕文件输出文件夹: \033 [1;33m{3 }\033 [0m
3695
- 字体文件输出文件夹: \033 [1;33m{4 }\033 [0m
3696
- 广兼容性子集化: \033 [1;33m{5 }\033 [0m
3697
- 忽略字体所缺字: \033 [1;33m{8 }\033 [0m
3698
- 子集化失败中断: \033 [1;33m{6 }\033 [0m
3699
- 使用工作目录字体:\033 [1;33m{7 }\033 [0m
3700
- ''' . format ( v_subdir , s_subdir , matchStrict , assout , fontout , char_compatible , errorStop , fontload , ignoreLost ) )
3712
+ print (f '''子集化字体
3713
+ 搜索子目录(视频): \033 [1;33m{ v_subdir } \033 [0m
3714
+ 搜索子目录(字幕): \033 [1;33m{ s_subdir } \033 [0m
3715
+ 严格字幕匹配: \033 [1;33m{ matchStrict } \033 [0m
3716
+ 字幕文件输出文件夹: \033 [1;33m{ assout } \033 [0m
3717
+ 字体文件输出文件夹: \033 [1;33m{ fontout } \033 [0m
3718
+ 广兼容性子集化: \033 [1;33m{ char_compatible } \033 [0m
3719
+ 忽略字体所缺字: \033 [1;33m{ ignoreLost } \033 [0m
3720
+ 子集化失败中断: \033 [1;33m{ errorStop } \033 [0m
3721
+ 使用工作目录字体:\033 [1;33m{ fontload } \033 [0m
3722
+ ''' )
3701
3723
else :
3702
- print ('''子集化字体并封装
3703
- 搜索子目录(视频): \033 [1;33m{4 }\033 [0m
3704
- 搜索子目录(字幕): \033 [1;33m{5 }\033 [0m
3705
- 移除内挂字幕: \033 [1;33m{0 }\033 [0m
3706
- 移除原有附件: \033 [1;33m{1 }\033 [0m
3707
- 不封装字体: \033 [1;33m{2 }\033 [0m
3708
- 严格字幕匹配: \033 [1;33m{6 }\033 [0m
3709
- 媒体文件输出文件夹: \033 [1;33m{3 }\033 [0m
3710
- 广兼容性子集化: \033 [1;33m{8 }\033 [0m
3711
- 忽略字体所缺字: \033 [1;33m{10 }\033 [0m
3712
- 子集化失败中断: \033 [1;33m{7 }\033 [0m
3713
- 使用工作目录字体:\033 [1;33m{9 }\033 [0m
3714
- ''' . format ( rmAssIn , rmAttach , notfont , mkvout , v_subdir , s_subdir , matchStrict , errorStop , char_compatible , fontload , ignoreLost ) )
3724
+ print (f '''子集化字体并封装
3725
+ 搜索子目录(视频): \033 [1;33m{ v_subdir } \033 [0m
3726
+ 搜索子目录(字幕): \033 [1;33m{ s_subdir } \033 [0m
3727
+ 移除内挂字幕: \033 [1;33m{ rmAssIn } \033 [0m
3728
+ 移除原有附件: \033 [1;33m{ rmAttach } \033 [0m
3729
+ 不封装字体: \033 [1;33m{ notfont } \033 [0m
3730
+ 严格字幕匹配: \033 [1;33m{ matchStrict } \033 [0m
3731
+ 媒体文件输出文件夹: \033 [1;33m{ mkvout } \033 [0m
3732
+ 广兼容性子集化: \033 [1;33m{ char_compatible } \033 [0m
3733
+ 忽略字体所缺字: \033 [1;33m{ ignoreLost } \033 [0m
3734
+ 子集化失败中断: \033 [1;33m{ errorStop } \033 [0m
3735
+ 使用工作目录字体:\033 [1;33m{ fontload } \033 [0m
3736
+ ''' )
3715
3737
cpath = ''
3716
3738
directout = False
3717
3739
subonly = False
@@ -3722,9 +3744,9 @@ def cFontSubset(font_info):
3722
3744
if cpath == '' :
3723
3745
print ('没有输入,回到上级菜单' )
3724
3746
elif not path .isabs (cpath ):
3725
- print ('\033 [1;31m[ERROR] 输入的必须是绝对路径!: \" {0 }\" \033 [0m' . format ( cpath ) )
3747
+ print (f '\033 [1;31m[ERROR] 输入的必须是绝对路径!: \" { cpath } \" \033 [0m' )
3726
3748
elif not path .exists (cpath ):
3727
- print ('\033 [1;31m[ERROR] 找不到路径: \" {0 }\" \033 [0m' . format ( cpath ) )
3749
+ print (f '\033 [1;31m[ERROR] 找不到路径: \" { cpath } \" \033 [0m' )
3728
3750
elif path .isfile (cpath ):
3729
3751
testext = path .splitext (cpath )[1 ][1 :].lower ()
3730
3752
if testext in extlist :
@@ -3733,9 +3755,9 @@ def cFontSubset(font_info):
3733
3755
directout = False
3734
3756
subonly = True
3735
3757
else :
3736
- print ('\033 [1;31m[ERROR] 扩展名不正确: \" {0 }\" \033 [0m' . format ( cpath ) )
3758
+ print (f '\033 [1;31m[ERROR] 扩展名不正确: \" { cpath } \" \033 [0m' )
3737
3759
elif not path .isdir (cpath ):
3738
- print ('\033 [1;31m[ERROR] 输入的应该是目录或媒体文件!: \" {0 }\" \033 [0m' . format ( cpath ) )
3760
+ print (f '\033 [1;31m[ERROR] 输入的应该是目录或媒体文件!: \" { cpath } \" \033 [0m' )
3739
3761
else :
3740
3762
directout = False
3741
3763
# print(directout)
@@ -3857,7 +3879,7 @@ def cFontSubset(font_info):
3857
3879
3858
3880
def cLicense ():
3859
3881
cls ()
3860
- print ('''AddSubFontMKV Python Remake Preview 23
3882
+ print ('''AddSubFontMKV Python Remake Preview 24
3861
3883
3862
3884
Apache-2.0 License
3863
3885
https://www.apache.org/licenses/
@@ -4105,11 +4127,17 @@ def cTextCodingTranscode():
4105
4127
print ('输出文件夹: \033 [33;1m\" {}\" \033 [0m' .format (outPath ))
4106
4128
os .system ('pause' )
4107
4129
4130
+
4131
+ def commandInput ():
4132
+ '''计划中的命令行输入支持'''
4133
+ pass
4134
+
4135
+
4108
4136
no_mkvm = False
4109
4137
no_cmdc = False
4110
4138
mkvmv = ''
4111
4139
ffmv = ''
4112
- font_info = [{}, {}, {}, {}]
4140
+ font_info = [{}, {}, {}, {}, {} ]
4113
4141
4114
4142
def checkFF ():
4115
4143
'''检查ffmpeg和ffprobe可用性'''
@@ -4123,7 +4151,7 @@ def checkFF():
4123
4151
def loadMain (reload : bool = False ):
4124
4152
global extlist , no_mkvm , no_cmdc , dupfont , mkvmv , font_info , fontin , langlist , ffmv , insteadFF
4125
4153
# 初始化字体列表 和 mkvmerge 相关参数
4126
- os .system ('title ASFMKV Python Remake Pre23 ^| (c) 2022-2024 yyfll ^| Apache-2.0' )
4154
+ os .system ('title ASFMKV Python Remake Pre24 ^| (c) 2022-2024 yyfll ^| Apache-2.0' )
4127
4155
if not reload :
4128
4156
if not o_fontload :
4129
4157
font_list = getFontFileList (fontin )
@@ -4170,7 +4198,7 @@ def loadMain(reload: bool = False):
4170
4198
if not len (ffmv ) > 0 :
4171
4199
ffMessage = '\n [F] 检查并启用FFmpeg'
4172
4200
ffSelect = 'F'
4173
- print ('''ASFMKV Python Remake Pre23 | (c) 2022-2024 yyfll{0}{5}
4201
+ print ('''ASFMKV Python Remake Pre24 | (c) 2022-2024 yyfll{0}{5}
4174
4202
字体名称数: [\033 [1;33m{2}\033 [0m]({4})
4175
4203
请选择功能:
4176
4204
[A] 列出字幕所用字体
@@ -4234,16 +4262,16 @@ def loadMain(reload: bool = False):
4234
4262
appdata = os .getenv ('APPDATA' )
4235
4263
fontCacheDir = path .join (appdata , 'ASFMKVpy' )
4236
4264
if not path .isdir (fontCacheDir ): fontCacheDir = None
4237
- print ('''字体信息重载
4238
- 缓存目录: \" \033 [1;33m{}\033 [0m\"
4265
+ print (f '''字体信息重载
4266
+ 缓存目录: \" \033 [1;33m{ fontCacheDir } \033 [0m\"
4239
4267
4240
4268
[0] 回到主菜单
4241
4269
[1] 更新缓存
4242
4270
[2] 仅覆盖当前使用的缓存文件并重载
4243
4271
[3] 删除所有缓存文件并重载
4244
4272
[4] 新增自定义字体目录并重载
4245
4273
4246
- 请选择:''' . format ( fontCacheDir ) )
4274
+ 请选择:''' )
4247
4275
work2 = os .system ('choice /C 01234' )
4248
4276
4249
4277
def reloadFont (usingCache : bool = True ):
0 commit comments