-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlyrics-extractor.js
More file actions
112 lines (96 loc) · 4.31 KB
/
lyrics-extractor.js
File metadata and controls
112 lines (96 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// FLAC歌词提取辅助函数
// 使用music-metadata-browser库提取FLAC文件中的Vorbis注释歌词
async function extractLyricsFromAudioFile(file) {
try {
// 检查库是否加载
if (!window.parseMusicMetadata && typeof musicMetadataBrowser !== 'undefined') {
window.parseMusicMetadata = musicMetadataBrowser.parseBlob;
}
if (!window.parseMusicMetadata) {
console.warn('music-metadata-browser未加载');
return null;
}
const metadata = await window.parseMusicMetadata(file);
let lyricsText = '';
console.log('解析元数据:', {
format: metadata.format?.container,
hasCommon: !!metadata.common,
hasNative: !!metadata.native
});
// 方法1: 从common.lyrics获取(标准位置)
if (metadata.common?.lyrics) {
if (Array.isArray(metadata.common.lyrics)) {
lyricsText = metadata.common.lyrics
.map(lyric => typeof lyric === 'string' ? lyric : (lyric.text || ''))
.filter(l => l.trim())
.join('\n');
} else if (typeof metadata.common.lyrics === 'string') {
lyricsText = metadata.common.lyrics;
}
}
// 方法2: 从native标签中查找LYRICS字段(FLAC Vorbis注释)
if (!lyricsText && metadata.native) {
for (const tag of metadata.native) {
// FLAC Vorbis注释格式: id可能是 "vorbis/LYRICS" 或 "LYRICS"
const tagId = tag.id?.toUpperCase() || '';
if (tagId.includes('LYRICS') || tagId === 'LYRICS') {
if (tag.value) {
if (Array.isArray(tag.value)) {
lyricsText = tag.value.join('\n');
} else if (typeof tag.value === 'string') {
lyricsText = tag.value;
} else if (tag.value.text) {
lyricsText = tag.value.text;
}
if (lyricsText) break;
}
}
}
}
// 方法3: 从comment字段查找(某些FLAC文件可能将歌词放在comment中)
if (!lyricsText && metadata.common?.comment) {
const comments = Array.isArray(metadata.common.comment)
? metadata.common.comment
: [metadata.common.comment];
for (const comment of comments) {
const commentText = typeof comment === 'string' ? comment : (comment.text || '');
// 如果comment很长(可能是歌词)
if (commentText.length > 100 && commentText.includes('\n')) {
lyricsText = commentText;
break;
}
}
}
// 方法4: 查找所有native标签,寻找包含歌词的字段
if (!lyricsText && metadata.native) {
for (const tag of metadata.native) {
const tagId = (tag.id || '').toUpperCase();
const tagValue = tag.value;
// 查找各种可能的歌词字段名
const lyricsKeywords = ['LYRICS', 'LYRIC', 'UNSYNCEDLYRICS', 'UNSYNCED LYRICS'];
const isLyricsField = lyricsKeywords.some(keyword => tagId.includes(keyword));
if (isLyricsField && tagValue) {
if (typeof tagValue === 'string') {
lyricsText = tagValue;
} else if (Array.isArray(tagValue)) {
lyricsText = tagValue.map(v => typeof v === 'string' ? v : (v.text || '')).join('\n');
} else if (tagValue.text) {
lyricsText = tagValue.text;
}
if (lyricsText) break;
}
}
}
if (lyricsText && lyricsText.trim()) {
return lyricsText.trim();
}
return null;
} catch (error) {
console.error('提取歌词失败:', error);
return null;
}
}
// 导出函数
if (typeof module !== 'undefined' && module.exports) {
module.exports = extractLyricsFromAudioFile;
}