|
1 | | -export const findIndex = (text) => { |
2 | | - const lines = text.split('\n'); |
3 | | - const result = []; |
4 | | - |
5 | | - for (const line of lines) { |
6 | | - const parts = line.split(','); |
7 | | - if (parts.length >= 3) { // 至少要有3部分:id,格式,语言代码 |
8 | | - const id = parts[0]; |
9 | | - const langCode = parts[2].toLowerCase(); |
10 | | - const langName = parts[3] ? parts[3].toLowerCase() : ''; |
11 | | - |
12 | | - // 检查简体中文:代码为chi且名称包含"简体"或"simplified" |
13 | | - const isSimplifiedChinese = langCode === 'chi' && ( |
14 | | - langName.includes('简体') || |
15 | | - langName.includes('simplified') || |
16 | | - langName === '简体' // 只有"简体"的情况 |
17 | | - ); |
18 | | - |
19 | | - // 检查英语:代码为eng或名称包含"english" |
20 | | - const isEnglish = langCode === 'eng' || |
21 | | - langName.includes('english') || |
22 | | - langName === 'sdh' || // eng,SDH |
23 | | - langName === ''; // 只有eng代码的情况 |
24 | | - |
25 | | - if (isSimplifiedChinese || isEnglish) { |
26 | | - result.push(parseInt(id)); |
27 | | - } |
28 | | - } |
| 1 | +export const findIndex = (info) => { |
| 2 | + const subTitles = info.streams.filter((stream) => stream.codec_type === 'subtitle').map(stream => ({ |
| 3 | + index: stream.index, |
| 4 | + code: stream.tags.language.toLowerCase(), |
| 5 | + name: stream.tags.title ? stream.tags.title.toLowerCase() : '' |
| 6 | + })); |
| 7 | + |
| 8 | + const chsIdx = findChiSub(subTitles); |
| 9 | + const engIdx = findEngSub(subTitles); |
| 10 | + |
| 11 | + if (chsIdx === undefined) { |
| 12 | + console.log('没有找到简体中文字幕'); |
| 13 | + } else { |
| 14 | + console.log('找到简体中文字幕,索引为:', chsIdx); |
| 15 | + } |
| 16 | + |
| 17 | + if (engIdx === undefined) { |
| 18 | + console.log('没有找到英语中文字幕'); |
| 19 | + } else { |
| 20 | + console.log('找到英语字幕,索引为:', engIdx); |
| 21 | + } |
| 22 | + |
| 23 | + if (chsIdx === undefined || engIdx === undefined) { |
| 24 | + throw new Error('字幕查找失败,中断执行'); |
| 25 | + } |
| 26 | + |
| 27 | + return [chsIdx, engIdx]; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * 目前看到的数据可能有: |
| 32 | + * chi,简体 |
| 33 | + * chi,Simplified Chinese |
| 34 | + * 查找策略是:先找 'chi', 如果数量大于1,则进一步找 "简体" |
| 35 | + */ |
| 36 | +const findChiSub = (subTitles) => { |
| 37 | + const chineseSubtitles = subTitles.filter(subTitle => subTitle.code === 'chi'); |
| 38 | + |
| 39 | + if (chineseSubtitles.length === 0) { |
| 40 | + return undefined; |
29 | 41 | } |
30 | 42 |
|
31 | | - // 按中文在前,英文在后排序 |
32 | | - return result.sort((a, b) => { |
33 | | - const lineA = lines.find(l => l.startsWith(a + ',')); |
34 | | - const lineB = lines.find(l => l.startsWith(b + ',')); |
35 | | - const isChineseA = lineA.includes('chi'); |
36 | | - const isChineseB = lineB.includes('chi'); |
37 | | - |
38 | | - if (isChineseA && !isChineseB) return -1; |
39 | | - if (!isChineseA && isChineseB) return 1; |
40 | | - return a - b; |
41 | | - }); |
| 43 | + if (chineseSubtitles.length === 1) { |
| 44 | + return chineseSubtitles[0].index; |
| 45 | + } |
| 46 | + |
| 47 | + // If multiple Chinese subtitles, look for Simplified Chinese |
| 48 | + const simplifiedChinese = chineseSubtitles.find(subTitle => |
| 49 | + subTitle.name.includes('简体') || |
| 50 | + subTitle.name.includes('simplified') |
| 51 | + ); |
| 52 | + |
| 53 | + return simplifiedChinese?.index; |
42 | 54 | } |
| 55 | + |
| 56 | +/** |
| 57 | + * 目前看到的数据可能有: |
| 58 | + * 9,subrip,eng |
| 59 | + * 10,subrip,eng,SDH |
| 60 | + * 23,subrip,eng,English[CC] |
| 61 | + * 如果同时有SDH和非SDH版本,选非SDH版本。 |
| 62 | + */ |
| 63 | +const findEngSub = (subTitles) => { |
| 64 | + const englishSubs = subTitles.filter(sub => sub.code === 'eng'); |
| 65 | + |
| 66 | + if (englishSubs.length === 0) return undefined; |
| 67 | + if (englishSubs.length === 1) return englishSubs[0].index; |
| 68 | + |
| 69 | + // Filter out SDH subtitles if there are multiple English options |
| 70 | + const nonSDHSubs = englishSubs.filter(sub => |
| 71 | + !sub.name.includes('sdh') |
| 72 | + ); |
| 73 | + |
| 74 | + // Return first non-SDH sub if available, otherwise first English sub |
| 75 | + return (nonSDHSubs[0] || englishSubs[0])?.index; |
| 76 | +}; |
0 commit comments