49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
export interface LrcLine {
|
|
time: number
|
|
text: string
|
|
}
|
|
|
|
export interface LrcData {
|
|
title?: string
|
|
artist?: string
|
|
lines: LrcLine[]
|
|
}
|
|
|
|
export function parseLrc(content: string): LrcData {
|
|
const result: LrcData = { lines: [] }
|
|
const lines = content.split('\n')
|
|
|
|
for (const line of lines) {
|
|
const trimmed = line.trim()
|
|
if (!trimmed) continue
|
|
|
|
// 元信息
|
|
const metaTitle = trimmed.match(/^\[ti:(.*?)\]$/i)
|
|
if (metaTitle) { result.title = metaTitle[1].trim(); continue }
|
|
const metaArtist = trimmed.match(/^\[ar:(.*?)\]$/i)
|
|
if (metaArtist) { result.artist = metaArtist[1].trim(); continue }
|
|
|
|
// 时间标签
|
|
const timeTags: number[] = []
|
|
let text = trimmed
|
|
const tagRegex = /\[(\d{2}):(\d{2})([.:])(\d{2,3})\]/g
|
|
let m
|
|
while ((m = tagRegex.exec(trimmed)) !== null) {
|
|
const min = parseInt(m[1])
|
|
const sec = parseInt(m[2])
|
|
const ms = parseInt(m[4].padEnd(3, '0'))
|
|
timeTags.push(min * 60 + sec + ms / 1000)
|
|
}
|
|
|
|
if (timeTags.length === 0) continue
|
|
|
|
text = trimmed.replace(/\[\d{2}:\d{2}[.:]\d{2,3}\]/g, '').trim()
|
|
for (const t of timeTags) {
|
|
result.lines.push({ time: t, text })
|
|
}
|
|
}
|
|
|
|
result.lines.sort((a, b) => a.time - b.time)
|
|
return result
|
|
}
|