新增 Any Listen 歌词标签数据嵌入、下载、读取播放(#2485)
This commit is contained in:
@@ -29,8 +29,57 @@ export const getMusicFilePic = async(filePath: string) => {
|
||||
return `data:${picture.format};base64,${Buffer.from(picture.data).toString('base64')}`
|
||||
}
|
||||
|
||||
export const parseLyric = (lrc: string): LX.Music.LyricInfo => {
|
||||
const verifyAwlrc = (lrc: string) => {
|
||||
return /(?:^|\s*)\[\d+:\d+(?:\.\d+)]<\d+,\d+>.+$/m.test(lrc)
|
||||
}
|
||||
const verifylrc = (lrc: string) => {
|
||||
return /(?:^|\s*)\[\d+:\d+(?:\.\d+)].+$/m.test(lrc)
|
||||
}
|
||||
const lrcTags = {
|
||||
awlrc: {
|
||||
name: 'lxlyric',
|
||||
verify: verifyAwlrc,
|
||||
},
|
||||
lrc: {
|
||||
name: 'lyric',
|
||||
verify: verifylrc,
|
||||
},
|
||||
tlrc: {
|
||||
name: 'tlyric',
|
||||
verify: verifylrc,
|
||||
},
|
||||
rlrc: {
|
||||
name: 'rlyric',
|
||||
verify: verifylrc,
|
||||
},
|
||||
rxp: /(?:^|\n\s*)\[(?:awlrc):([^\]]+)]/i,
|
||||
rxp2: /^(lrc|awlrc|tlrc|rlrc):([^,]+)$/i,
|
||||
} as const
|
||||
const parse = (content: string) => {
|
||||
const lyricInfo: Partial<LX.Music.LyricInfo> = {}
|
||||
const lrcs = content.trim().split(',')
|
||||
for (const lrc of lrcs) {
|
||||
const result = lrcTags.rxp2.exec(lrc.trim())
|
||||
if (!result) continue
|
||||
const target = lrcTags[result[1].toLowerCase() as 'tlrc' | 'rlrc' | 'lrc' | 'awlrc']
|
||||
if (!target) continue
|
||||
const data = Buffer.from(result[2], 'base64').toString('utf-8').trim()
|
||||
if (target.verify(data)) lyricInfo[target.name] = data
|
||||
}
|
||||
return lyricInfo
|
||||
}
|
||||
let parsedInfo: Partial<LX.Music.LyricInfo> = {}
|
||||
let lyric = lrc.replace(lrcTags.rxp, (_: string, p1: string) => {
|
||||
parsedInfo = parse(p1)
|
||||
return ''
|
||||
}).trim()
|
||||
return { lyric, ...parsedInfo }
|
||||
}
|
||||
|
||||
|
||||
export const getMusicFileLyric = async(filePath: string): Promise<LX.Music.LyricInfo | null> => {
|
||||
const lyric = await getLocalMusicFileLyric(filePath)
|
||||
if (!lyric) return null
|
||||
return lyric
|
||||
return parseLyric(lyric.lyric)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user