Skip to content

Commit 8ce8ff8

Browse files
committed
now support creating external MKS file instead of copy whole MKV. Fix rudeSubN2L bug.
1 parent 7d025f5 commit 8ce8ff8

File tree

1 file changed

+76
-47
lines changed

1 file changed

+76
-47
lines changed

ASFMKV_dev.py

Lines changed: 76 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,7 +1889,7 @@ def assFontSubset(assfont: dict, fontdir: str, allTTF: bool = False):
18891889
showOutChars = ' '.join([(c if c.isprintable() else 'U+' + hex(ord(c))[2:].rjust(4, '0').upper()) for c in out_of_range])
18901890

18911891
if ignoreLost:
1892-
print('\033[1;31m[WARNING] 已忽略不在字体中的字符 {}\033[0m'.format(showOutChars))
1892+
print('\033[1;31m[WARNING] 已忽略不在字体中的字符: {}\033[0m'.format(showOutChars))
18931893
else:
18941894
print('\033[1;31m[ERROR] 以下字符不在字体\"{1}\"\033[0m\n\"{0}\"\n\033[1;31m[ERROR] 以上字符不在字体\"{1}\"\033[0m'.format(showOutChars, s[3]))
18951895
print('\033[1;31m[ERROR] 已停止子集化,如果您想要强行子集化,请启用ignoreLost\033[0m')
@@ -2181,7 +2181,7 @@ def getSubName(v: str, sub: str) -> tuple[str, str]:
21812181
bVideo = basenameNoEXT(v)
21822182
return sub, bSub[bSub.find(bVideo) + len(bVideo):]
21832183

2184-
def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list = [], fontpaths: list = [], forceSubTrack: str = '?') -> int:
2184+
def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list = [], fontpaths: list = [], forceSubTrack: str = '?', makeMKS: bool = False) -> int:
21852185
"""
21862186
ffASFMKV,将媒体文件、字幕、字体封装到一个MKV文件,需要ffmpeg、ffprobe命令行支持
21872187
@@ -2203,46 +2203,55 @@ def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list =
22032203
elif not path.exists(file) or not path.isfile(file):
22042204
return 4
22052205
if outfile is None: outfile = ''
2206+
2207+
outExt = '.mkv'
2208+
if makeMKS:
2209+
outExt = '.mks'
2210+
22062211
if outfile == '' or not path.exists(path.dirname(outfile)) or path.dirname(outfile) == path.dirname(file):
2207-
outfile = '.muxed'.join([path.splitext(file)[0], '.mkv'])
2208-
outfile = path.splitext(outfile)[0] + '.mkv'
2212+
outfile = '.muxed'.join([path.splitext(file)[0], outExt])
2213+
outfile = path.splitext(outfile)[0] + outExt
22092214
if path.exists(outfile):
22102215
checkloop = 1
22112216
while path.exists('#{0}'.format(checkloop).join(path.splitext(outfile))):
22122217
checkloop += 1
2213-
outfile = '#{0}'.format(checkloop).join([path.splitext(outfile)[0], '.mkv'])
2218+
outfile = '#{0}'.format(checkloop).join([path.splitext(outfile)[0], outExt])
22142219

2215-
rawIdx = json.loads(os.popen('ffprobe -print_format json -show_streams -hide_banner -v 0 -i \"{}\"'.format(file), mode='r').read())
2216-
firstIdx = len(rawIdx['streams'])
22172220
ffargs = []
22182221
metaList = []
22192222
mapList = []
22202223
copyList = []
2221-
2222-
for f in rawIdx['streams']:
2223-
if f['codec_type'] == 'video':
2224-
if '-map 0:v' not in mapList:
2225-
mapList.append('-map 0:v')
2226-
copyList.append('-c:v copy')
2227-
elif f['codec_type'] == 'audio':
2228-
if '-map 0:a' not in mapList:
2229-
mapList.append('-map 0:a')
2230-
copyList.append('-c:a copy')
2231-
elif f['codec_type'] == 'attachment':
2232-
if not rmAttach:
2233-
if '-map 0:t' not in mapList:
2234-
mapList.append('-map 0:t')
2235-
copyList.append('-c:t copy')
2236-
else:
2237-
firstIdx -= 1
2238-
elif f['codec_type'] == 'subtitle':
2239-
if rmAssIn:
2240-
firstIdx -= 1
2241-
elif '-map 0:s' not in mapList:
2242-
mapList.append('-map 0:s')
2243-
if not rmAssIn and f['disposition']['default'] == 1:
2244-
copyList.append('-disposition:{} 0'.format(f['index']))
2245-
ffargs.append('-i \"{}\"'.format(file))
2224+
firstIdx = 0
2225+
2226+
if not makeMKS:
2227+
rawIdx = json.loads(os.popen('ffprobe -print_format json -show_streams -hide_banner -v 0 -i \"{}\"'.format(file), mode='r').read())
2228+
2229+
firstIdx = len(rawIdx['streams'])
2230+
2231+
for f in rawIdx['streams']:
2232+
if f['codec_type'] == 'video':
2233+
if '-map 0:v' not in mapList:
2234+
mapList.append('-map 0:v')
2235+
copyList.append('-c:v copy')
2236+
elif f['codec_type'] == 'audio':
2237+
if '-map 0:a' not in mapList:
2238+
mapList.append('-map 0:a')
2239+
copyList.append('-c:a copy')
2240+
elif f['codec_type'] == 'attachment':
2241+
if not rmAttach:
2242+
if '-map 0:t' not in mapList:
2243+
mapList.append('-map 0:t')
2244+
copyList.append('-c:t copy')
2245+
else:
2246+
firstIdx -= 1
2247+
elif f['codec_type'] == 'subtitle':
2248+
if rmAssIn and not makeMKS:
2249+
firstIdx -= 1
2250+
elif '-map 0:s' not in mapList:
2251+
mapList.append('-map 0:s')
2252+
if not rmAssIn and f['disposition']['default'] == 1:
2253+
copyList.append('-disposition:{} 0'.format(f['index']))
2254+
ffargs.append('-i \"{}\"'.format(file))
22462255

22472256
fn = path.splitext(path.basename(file))[0]
22482257
metaList.append('-metadata:g title=\"{}\"'.format(fn))
@@ -2263,14 +2272,14 @@ def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list =
22632272

22642273
if len(asslangs) > 0 and path.splitext(s)[1][1:].lower() not in ['idx']:
22652274
if assnote.lower() in asslangs:
2266-
metadata.append('language=\"{}\"'.format(asslangs[assnote]))
2275+
metadata.append('language=\"{}\"'.format(asslangs[assnote.lower()]))
22672276

22682277
if len(metadata) > 0:
22692278
for m in metadata:
22702279
metaList.append('-metadata:s:{1} {0}'.format(m, firstIdx))
22712280

22722281
ffargs.append('-i \"{}\"'.format(s))
2273-
mapList.append('-map {}'.format(i + 1))
2282+
mapList.append('-map {}'.format(i + 1 if not makeMKS else i))
22742283

22752284
if not defaultSet and (assnote.lower() == forceSubTrack.lower() or forceSubTrack == '?'):
22762285
copyList.append('-disposition:{} default'.format(firstIdx))
@@ -2290,6 +2299,9 @@ def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list =
22902299
extMime[path.splitext(s)[1][1:].lower()], firstIdx))
22912300
ffargs.append('-attach \"{}\"'.format(s))
22922301
firstIdx += 1
2302+
2303+
if makeMKS: metaList.append('-f matroska')
2304+
22932305
ffmr = os.system('ffmpeg {0} {3} {4} -c:s copy {2} \"{1}\"'.format(' '.join(ffargs), outfile, ' '.join(metaList), ' '.join(mapList), ' '.join(copyList)))
22942306

22952307
if ffmr >= 1:
@@ -2320,7 +2332,7 @@ def ffASFMKV(file: str, outfile: str = '', asslangs: list = [], asspaths: list =
23202332
return ffmr
23212333

23222334

2323-
def ASFMKV(file: str, outfile: str = '', asslangs: dict = {}, asspaths: list = [], fontpaths: list = [], forceSubTrack: str = '?') -> int:
2335+
def ASFMKV(file: str, outfile: str = '', asslangs: dict = {}, asspaths: list = [], fontpaths: list = [], forceSubTrack: str = '?', makeMKS: bool = False) -> int:
23242336
"""
23252337
ASFMKV,将媒体文件、字幕、字体封装到一个MKV文件,需要mkvmerge命令行支持
23262338
@@ -2344,18 +2356,27 @@ def ASFMKV(file: str, outfile: str = '', asslangs: dict = {}, asspaths: list = [
23442356
elif not path.exists(file) or not path.isfile(file):
23452357
return 4
23462358
if outfile is None: outfile = ''
2359+
2360+
outExt = '.mkv'
2361+
if makeMKS:
2362+
outExt = '.mks'
2363+
23472364
if outfile == '' or not path.exists(path.dirname(outfile)) or path.dirname(outfile) == path.dirname(file):
2348-
outfile = '.muxed'.join([path.splitext(file)[0], '.mkv'])
2349-
outfile = path.splitext(outfile)[0] + '.mkv'
2365+
outfile = '.muxed'.join([path.splitext(file)[0], outExt])
2366+
outfile = path.splitext(outfile)[0] + outExt
23502367
if path.exists(outfile):
23512368
checkloop = 1
23522369
while path.exists('#{0}'.format(checkloop).join(path.splitext(outfile))):
23532370
checkloop += 1
2354-
outfile = '#{0}'.format(checkloop).join([path.splitext(outfile)[0], '.mkv'])
2371+
outfile = '#{0}'.format(checkloop).join([path.splitext(outfile)[0], outExt])
2372+
23552373
mkvargs = []
2356-
if rmAssIn: mkvargs.append('-S')
2357-
if rmAttach: mkvargs.append('-M')
2358-
mkvargs.extend(['(', file, ')'])
2374+
2375+
if not makeMKS:
2376+
if rmAssIn: mkvargs.append('-S')
2377+
if rmAttach: mkvargs.append('-M')
2378+
mkvargs.extend(['(', file, ')'])
2379+
23592380
fn = path.splitext(path.basename(file))[0]
23602381
if len(asspaths) > 0:
23612382
defaultSet = False
@@ -2825,7 +2846,7 @@ def namePosition(files: list):
28252846

28262847

28272848
def main(font_info: list, asspath: list, outdir: list = ['', '', ''], mux: bool = False, vpath: str = '',
2828-
asslangs: dict = {}, FFmuxer: int = 0, fontline: int = -1, forceSubTrack: str = '?'):
2849+
asslangs: dict = {}, FFmuxer: int = 0, fontline: int = -1, forceSubTrack: str = '?', makeMKS: bool = False):
28292850
"""
28302851
主函数,负责调用各函数走完完整的处理流程
28312852
@@ -2934,10 +2955,10 @@ def main(font_info: list, asspath: list, outdir: list = ['', '', ''], mux: bool
29342955
outdir[2] = path.dirname(outdir[2])
29352956
if FFmuxer == 1:
29362957
mkvr = ffASFMKV(vpath, path.join(outdir[2], path.splitext(path.basename(vpath))[0] + '.mkv'),
2937-
asslangs=asslangs, asspaths=newasspath, fontpaths=list(set([f[0] for f in newfont_name.values()])), forceSubTrack=forceSubTrack)
2958+
asslangs=asslangs, asspaths=newasspath, fontpaths=list(set([f[0] for f in newfont_name.values()])), forceSubTrack=forceSubTrack, makeMKS=makeMKS)
29382959
elif FFmuxer == 0:
29392960
mkvr = ASFMKV(vpath, path.join(outdir[2], path.splitext(path.basename(vpath))[0] + '.mkv'),
2940-
asslangs=asslangs, asspaths=newasspath, fontpaths=list(set([f[0] for f in newfont_name.values()])), forceSubTrack=forceSubTrack)
2961+
asslangs=asslangs, asspaths=newasspath, fontpaths=list(set([f[0] for f in newfont_name.values()])), forceSubTrack=forceSubTrack, makeMKS=makeMKS)
29412962
else:
29422963
mkvr = 0
29432964
if not notfont:
@@ -3449,10 +3470,10 @@ def showFocusSub(i: str):
34493470
elif rudeSubN2L:
34503471
for k in subName2Lang.keys():
34513472
if k.lstrip('.') in i:
3452-
lang = subName2Lang[i]
3473+
lang = subName2Lang[k]
34533474
autoGet = True
34543475
break
3455-
else:
3476+
if not autoGet:
34563477
if no_mkvm and len(translationLang) > 0:
34573478
searchLang = '仅本地语言搜索'
34583479
elif no_mkvm:
@@ -3819,13 +3840,21 @@ def cFontSubset(font_info):
38193840
sublangs = None
38203841
sublangs = {}
38213842
forceSubTrack = '?'
3843+
makeMKSq = False
38223844
muxer = 0
38233845
cls()
38243846
if domux:
38253847
print('您需要为字幕轨道添加语言信息吗?')
38263848
if os.system('choice') == 1:
38273849
sublangs = getSubsLangsV2(media_ass)
3850+
3851+
cls()
38283852
forceSubTrack = getForceSub(media_ass)
3853+
3854+
cls()
3855+
print('您要封装为外置MKS文件吗?')
3856+
if os.system('choice') == 1:
3857+
makeMKSq = True
38293858
# print(media_ass)
38303859
if work == 21 or (work == 2 and no_mkvm):
38313860
muxer = 1
@@ -3844,7 +3873,7 @@ def cFontSubset(font_info):
38443873
outdir=[assout_cache, fontout_cache, mkvout_cache],
38453874
vpath=k,
38463875
asslangs=sublangs,
3847-
FFmuxer = muxer, forceSubTrack=forceSubTrack)
3876+
FFmuxer = muxer, forceSubTrack = forceSubTrack, makeMKS = makeMKSq)
38483877
if mkvr != -2:
38493878
showMessageSubset(newasspaths, newfont_name)
38503879
else:

0 commit comments

Comments
 (0)