mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 03:56:38 +08:00
支持 ASS 字幕原样渲染与字幕显示设置
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
export type SubtitleChineseConverter = (text: string) => string
|
||||
|
||||
function commaIndexAtField(line: string, fieldCount: number): number {
|
||||
let index = -1
|
||||
for (let field = 0; field < fieldCount; field += 1) {
|
||||
index = line.indexOf(',', index + 1)
|
||||
if (index < 0) return -1
|
||||
}
|
||||
return index
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert only the Text column of ASS/SSA Dialogue lines. ASS styling tags,
|
||||
* drawings, timing and positioning metadata are bytes-for-bytes preserved so
|
||||
* the event can still be rendered by libass after a Chinese conversion.
|
||||
*/
|
||||
export function convertASSContent(
|
||||
content: string,
|
||||
convert: SubtitleChineseConverter,
|
||||
): string {
|
||||
return content
|
||||
.replace(/^\uFEFF/, '')
|
||||
.split(/\r?\n/)
|
||||
.map((line) => {
|
||||
const lower = line.toLowerCase()
|
||||
if (!lower.startsWith('dialogue:')) return line
|
||||
const textIndex = commaIndexAtField(line, 9)
|
||||
if (textIndex < 0) return line
|
||||
return `${line.slice(0, textIndex + 1)}${convert(line.slice(textIndex + 1))}`
|
||||
})
|
||||
.join('\n')
|
||||
}
|
||||
Reference in New Issue
Block a user