mirror of
https://github.com/nicetry247/offlineacademy.git
synced 2026-07-26 11:58:44 +00:00
fix: embed language segments like -english-onehack.us.srt too
This commit is contained in:
+65
-8
@@ -69,16 +69,12 @@ export function getSubtitleFormat(fileName: string): SubtitleFormat | null {
|
|||||||
return ext === 'vtt' || ext === 'srt' ? ext : null
|
return ext === 'vtt' || ext === 'srt' ? ext : null
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseSubtitleForVideo(
|
function tryExactBasename(
|
||||||
videoBaseName: string,
|
videoBaseName: string,
|
||||||
subtitleFileName: string,
|
subtitleBaseName: string,
|
||||||
src: string
|
src: string,
|
||||||
|
format: SubtitleFormat
|
||||||
): SubtitleTrackInput | null {
|
): SubtitleTrackInput | null {
|
||||||
const format = getSubtitleFormat(subtitleFileName)
|
|
||||||
if (!format) return null
|
|
||||||
|
|
||||||
const subtitleBaseName = basename(subtitleFileName, extname(subtitleFileName))
|
|
||||||
|
|
||||||
if (subtitleBaseName === videoBaseName) {
|
if (subtitleBaseName === videoBaseName) {
|
||||||
return {
|
return {
|
||||||
src,
|
src,
|
||||||
@@ -88,7 +84,15 @@ export function parseSubtitleForVideo(
|
|||||||
isDefault: true,
|
isDefault: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function tryDottedLangSuffix(
|
||||||
|
videoBaseName: string,
|
||||||
|
subtitleBaseName: string,
|
||||||
|
src: string,
|
||||||
|
format: SubtitleFormat
|
||||||
|
): SubtitleTrackInput | null {
|
||||||
const languagePrefix = `${videoBaseName}.`
|
const languagePrefix = `${videoBaseName}.`
|
||||||
if (!subtitleBaseName.startsWith(languagePrefix)) return null
|
if (!subtitleBaseName.startsWith(languagePrefix)) return null
|
||||||
|
|
||||||
@@ -105,6 +109,59 @@ export function parseSubtitleForVideo(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tryEmbeddedLangSegment(
|
||||||
|
videoBaseName: string,
|
||||||
|
subtitleBaseName: string,
|
||||||
|
src: string,
|
||||||
|
format: SubtitleFormat
|
||||||
|
): SubtitleTrackInput | null {
|
||||||
|
const subtitleParts = subtitleBaseName.split('-')
|
||||||
|
const videoParts = videoBaseName.split('-')
|
||||||
|
if (subtitleParts.length < videoParts.length + 1) return null
|
||||||
|
|
||||||
|
for (let index = 1; index < subtitleParts.length - 1; index += 1) {
|
||||||
|
const candidateParts = [...subtitleParts]
|
||||||
|
candidateParts.splice(index, 1)
|
||||||
|
if (candidateParts.join('-') !== videoBaseName) continue
|
||||||
|
|
||||||
|
const rawLanguage = subtitleParts[index]
|
||||||
|
if (!rawLanguage || rawLanguage.includes('/')) continue
|
||||||
|
|
||||||
|
const lang = normalizeSubtitleLang(rawLanguage)
|
||||||
|
return {
|
||||||
|
src,
|
||||||
|
lang,
|
||||||
|
label: languageCodeToLabel(lang),
|
||||||
|
format,
|
||||||
|
isDefault: lang === 'en',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseSubtitleForVideo(
|
||||||
|
videoBaseName: string,
|
||||||
|
subtitleFileName: string,
|
||||||
|
src: string
|
||||||
|
): SubtitleTrackInput | null {
|
||||||
|
const format = getSubtitleFormat(subtitleFileName)
|
||||||
|
if (!format) return null
|
||||||
|
|
||||||
|
const subtitleBaseName = basename(subtitleFileName, extname(subtitleFileName))
|
||||||
|
|
||||||
|
const direct = tryExactBasename(videoBaseName, subtitleBaseName, src, format)
|
||||||
|
if (direct) return direct
|
||||||
|
|
||||||
|
const dotted = tryDottedLangSuffix(videoBaseName, subtitleBaseName, src, format)
|
||||||
|
if (dotted) return dotted
|
||||||
|
|
||||||
|
const embedded = tryEmbeddedLangSegment(videoBaseName, subtitleBaseName, src, format)
|
||||||
|
if (embedded) return embedded
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
export function buildSubtitleTrackMap(
|
export function buildSubtitleTrackMap(
|
||||||
videoFileNames: string[],
|
videoFileNames: string[],
|
||||||
subtitleFileNames: string[],
|
subtitleFileNames: string[],
|
||||||
|
|||||||
Reference in New Issue
Block a user