mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-26 17:42:05 +00:00
support chorus parsing
This commit is contained in:
86
src/plugins/synced-lyrics/parsers/lrc.test.ts
Normal file
86
src/plugins/synced-lyrics/parsers/lrc.test.ts
Normal file
@ -0,0 +1,86 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
import { LRC } from './lrc';
|
||||
|
||||
test('empty string', () => {
|
||||
const lrc = LRC.parse('');
|
||||
expect(lrc).toStrictEqual({ lines: [], tags: [] });
|
||||
});
|
||||
|
||||
test('chorus', () => {
|
||||
const lrc = LRC.parse(`\
|
||||
[00:12.00]Line 1 lyrics
|
||||
[00:17.20]Line 2 lyrics
|
||||
[00:21.10][00:45.10]Repeating lyrics (e.g. chorus)
|
||||
[mm:ss.xx]Last lyrics line\
|
||||
`);
|
||||
|
||||
expect(lrc).toStrictEqual({
|
||||
lines: [
|
||||
{ duration: 12000, text: '', time: '0:0:0', timeInMs: 0 },
|
||||
{
|
||||
duration: 5020,
|
||||
text: 'Line 1 lyrics',
|
||||
time: '00:12:00',
|
||||
timeInMs: 12000,
|
||||
},
|
||||
{
|
||||
duration: 3990,
|
||||
text: 'Line 2 lyrics',
|
||||
time: '00:17:20',
|
||||
timeInMs: 17020,
|
||||
},
|
||||
{
|
||||
duration: 24000,
|
||||
text: 'Repeating lyrics (e.g. chorus)',
|
||||
time: '00:21:10',
|
||||
timeInMs: 21010,
|
||||
},
|
||||
{
|
||||
duration: Infinity,
|
||||
text: 'Repeating lyrics (e.g. chorus)',
|
||||
time: '00:45:10',
|
||||
timeInMs: 45010,
|
||||
},
|
||||
],
|
||||
tags: [],
|
||||
});
|
||||
});
|
||||
|
||||
test('attributes', () => {
|
||||
const lrc = LRC.parse(
|
||||
`[ar:Chubby Checker oppure Beatles, The]
|
||||
[al:Hits Of The 60's - Vol. 2 – Oldies]
|
||||
[ti:Let's Twist Again]
|
||||
[au:Written by Kal Mann / Dave Appell, 1961]
|
||||
[length: 2:23]
|
||||
|
||||
[00:12.00]Naku Penda Piya-Naku Taka Piya-Mpenziwe
|
||||
[00:15.30]Some more lyrics ...`,
|
||||
);
|
||||
|
||||
expect(lrc).toStrictEqual({
|
||||
lines: [
|
||||
{ duration: 12000, text: '', time: '0:0:0', timeInMs: 0 },
|
||||
{
|
||||
duration: 3030,
|
||||
text: 'Naku Penda Piya-Naku Taka Piya-Mpenziwe',
|
||||
time: '00:12:00',
|
||||
timeInMs: 12000,
|
||||
},
|
||||
{
|
||||
duration: Infinity,
|
||||
text: 'Some more lyrics ...',
|
||||
time: '00:15:30',
|
||||
timeInMs: 15030,
|
||||
},
|
||||
],
|
||||
tags: [
|
||||
{ tag: 'ar', value: 'Chubby Checker oppure Beatles, The' },
|
||||
{ tag: 'al', value: "Hits Of The 60's - Vol. 2 – Oldies" },
|
||||
{ tag: 'ti', value: "Let's Twist Again" },
|
||||
{ tag: 'au', value: 'Written by Kal Mann / Dave Appell, 1961' },
|
||||
{ tag: 'length', value: '2:23' },
|
||||
],
|
||||
});
|
||||
});
|
||||
@ -17,7 +17,7 @@ interface LRC {
|
||||
|
||||
const tagRegex = /^\[(?<tag>\w+):\s*(?<value>.+?)\s*\]$/;
|
||||
// prettier-ignore
|
||||
const lyricRegex = /^\[(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)\](?<text>.+)$/;
|
||||
const timestampRegex = /^\[(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)\]/m;
|
||||
|
||||
export const LRC = {
|
||||
parse: (text: string): LRC => {
|
||||
@ -27,13 +27,29 @@ export const LRC = {
|
||||
};
|
||||
|
||||
let offset = 0;
|
||||
let previousLine: LRCLine | null = null;
|
||||
|
||||
for (const line of text.split('\n')) {
|
||||
if (!line.trim().startsWith('[')) continue;
|
||||
for (let line of text.split('\n')) {
|
||||
line = line.trim();
|
||||
if (!line.startsWith('[')) continue;
|
||||
|
||||
const lyric = line.match(lyricRegex)?.groups;
|
||||
if (!lyric) {
|
||||
const timestamps = [];
|
||||
let match: Record<string, string> | undefined;
|
||||
while ((match = line.match(timestampRegex)?.groups)) {
|
||||
const { minutes, seconds, milliseconds } = match;
|
||||
const timeInMs =
|
||||
parseInt(minutes) * 60 * 1000 +
|
||||
parseInt(seconds) * 1000 +
|
||||
parseInt(milliseconds);
|
||||
|
||||
timestamps.push({
|
||||
time: `${minutes}:${seconds}:${milliseconds}`,
|
||||
timeInMs,
|
||||
});
|
||||
|
||||
line = line.replace(timestampRegex, '');
|
||||
}
|
||||
|
||||
if (!timestamps.length) {
|
||||
const tag = line.match(tagRegex)?.groups;
|
||||
if (tag) {
|
||||
if (tag.tag === 'offset') {
|
||||
@ -49,29 +65,28 @@ export const LRC = {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { minutes, seconds, milliseconds, text } = lyric;
|
||||
const timeInMs =
|
||||
parseInt(minutes) * 60 * 1000 +
|
||||
parseInt(seconds) * 1000 +
|
||||
parseInt(milliseconds);
|
||||
const text = line.trim();
|
||||
|
||||
const currentLine: LRCLine = {
|
||||
time: `${minutes}:${seconds}:${milliseconds}`,
|
||||
for (const { time, timeInMs } of timestamps) {
|
||||
lrc.lines.push({
|
||||
time,
|
||||
timeInMs,
|
||||
text: text.trim(),
|
||||
text: text,
|
||||
duration: Infinity,
|
||||
};
|
||||
|
||||
if (previousLine) {
|
||||
previousLine.duration = timeInMs - previousLine.timeInMs;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
previousLine = currentLine;
|
||||
lrc.lines.push(currentLine);
|
||||
}
|
||||
lrc.lines.sort(({ timeInMs: timeA }, { timeInMs: timeB }) => timeA - timeB);
|
||||
for (let i = 0; i < lrc.lines.length; i++) {
|
||||
const current = lrc.lines[i];
|
||||
const next = lrc.lines[i + 1];
|
||||
|
||||
for (const line of lrc.lines) {
|
||||
line.timeInMs += offset;
|
||||
current.timeInMs += offset;
|
||||
|
||||
if (next) {
|
||||
current.duration = next.timeInMs - current.timeInMs;
|
||||
}
|
||||
}
|
||||
|
||||
const first = lrc.lines.at(0);
|
||||
|
||||
Reference in New Issue
Block a user