mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-25 16:11:20 -08:00
fix(multiple-subtitles): repair old ASS conversion line breaks [PKG-MULTI-SUB-11]
This commit is contained in:
1 parent
8ae2762a23
commit
318de37e8a
22 files changed
+3268
-27
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import type { Converters } from './types'
|
||||
|
||||
export default function convertAss(text: string, converter: Converters['assToVtt']): string {
|
||||
const converted = converter(text)
|
||||
if (!/^WEBVTT \d+ \d+:/.test(converted))
|
||||
return converted
|
||||
|
||||
// Core5.1.2 main collapsed template line breaks; compare its complete output
|
||||
// before repairing so custom converters and valid headers remain untouched.
|
||||
const cues = []
|
||||
for (const line of text.split(/\r?\n/)) {
|
||||
const match = line.match(/Dialogue:\s\d,(\d+:\d{2}:\d{2}\.\d{2}),(\d+:\d{2}:\d{2}\.\d{2}),(?:[^,]*,){6}([\s\S]*)$/i)
|
||||
if (!match)
|
||||
continue
|
||||
const [, start = '', end = '', content = ''] = match
|
||||
cues.push({
|
||||
start: `${start.replace(/^(\d):/, '0$1:')}0`,
|
||||
end: `${end.replace(/^(\d):/, '0$1:')}0`,
|
||||
text: content.replace(/\{[\s\S]*?\}/g, '').replace(/\\N/g, '\n').trim().split(/\r?\n/).map(line => line.trim()).join('\n'),
|
||||
})
|
||||
}
|
||||
const collapsed = `WEBVTT ${cues.map((cue, index) => `${index + 1} ${cue.start} --> ${cue.end} ${cue.text}`).join('\n\n')}`
|
||||
if (converted !== collapsed)
|
||||
return converted
|
||||
return `WEBVTT\n\n${cues.map((cue, index) => `${index + 1}\n${cue.start} --> ${cue.end}\n${cue.text}`).join('\n\n')}`
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { Converters, Lifetime, TrackOption } from './types'
|
||||
import convertAss from './ass-conversion'
|
||||
|
||||
export default async function loadVtt(option: TrackOption, { getExt, srtToVtt, assToVtt }: Converters, lifetime: Lifetime): Promise<string | void> {
|
||||
if (lifetime.closed)
|
||||
@@ -21,7 +22,7 @@ export default async function loadVtt(option: TrackOption, { getExt, srtToVtt, a
|
||||
const text = new TextDecoder(option.encoding || 'utf-8').decode(buffer!)
|
||||
switch (option.type || getExt(option.url!)) {
|
||||
case 'srt': return srtToVtt(text)
|
||||
case 'ass': return assToVtt(text)
|
||||
case 'ass': return convertAss(text, assToVtt)
|
||||
case 'vtt': return text
|
||||
default: return ''
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user