mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-27 18:03:44 +00:00
support eLRC
This commit is contained in:
@ -17,28 +17,32 @@ test('chorus', () => {
|
|||||||
|
|
||||||
expect(lrc).toStrictEqual({
|
expect(lrc).toStrictEqual({
|
||||||
lines: [
|
lines: [
|
||||||
{ duration: 12000, text: '', time: '0:0:0', timeInMs: 0 },
|
{ duration: 12000, text: '', words: [], time: '00:00:00', timeInMs: 0 },
|
||||||
{
|
{
|
||||||
duration: 5020,
|
duration: 5020,
|
||||||
text: 'Line 1 lyrics',
|
text: 'Line 1 lyrics',
|
||||||
|
words: [],
|
||||||
time: '00:12:00',
|
time: '00:12:00',
|
||||||
timeInMs: 12000,
|
timeInMs: 12000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
duration: 3990,
|
duration: 3990,
|
||||||
text: 'Line 2 lyrics',
|
text: 'Line 2 lyrics',
|
||||||
|
words: [],
|
||||||
time: '00:17:20',
|
time: '00:17:20',
|
||||||
timeInMs: 17020,
|
timeInMs: 17020,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
duration: 24000,
|
duration: 24000,
|
||||||
text: 'Repeating lyrics (e.g. chorus)',
|
text: 'Repeating lyrics (e.g. chorus)',
|
||||||
|
words: [],
|
||||||
time: '00:21:10',
|
time: '00:21:10',
|
||||||
timeInMs: 21010,
|
timeInMs: 21010,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
duration: Infinity,
|
duration: Infinity,
|
||||||
text: 'Repeating lyrics (e.g. chorus)',
|
text: 'Repeating lyrics (e.g. chorus)',
|
||||||
|
words: [],
|
||||||
time: '00:45:10',
|
time: '00:45:10',
|
||||||
timeInMs: 45010,
|
timeInMs: 45010,
|
||||||
},
|
},
|
||||||
@ -61,16 +65,18 @@ test('attributes', () => {
|
|||||||
|
|
||||||
expect(lrc).toStrictEqual({
|
expect(lrc).toStrictEqual({
|
||||||
lines: [
|
lines: [
|
||||||
{ duration: 12000, text: '', time: '0:0:0', timeInMs: 0 },
|
{ duration: 12000, text: '', words: [], time: '00:00:00', timeInMs: 0 },
|
||||||
{
|
{
|
||||||
duration: 3030,
|
duration: 3030,
|
||||||
text: 'Naku Penda Piya-Naku Taka Piya-Mpenziwe',
|
text: 'Naku Penda Piya-Naku Taka Piya-Mpenziwe',
|
||||||
|
words: [],
|
||||||
time: '00:12:00',
|
time: '00:12:00',
|
||||||
timeInMs: 12000,
|
timeInMs: 12000,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
duration: Infinity,
|
duration: Infinity,
|
||||||
text: 'Some more lyrics ...',
|
text: 'Some more lyrics ...',
|
||||||
|
words: [],
|
||||||
time: '00:15:30',
|
time: '00:15:30',
|
||||||
timeInMs: 15030,
|
timeInMs: 15030,
|
||||||
},
|
},
|
||||||
@ -84,3 +90,55 @@ test('attributes', () => {
|
|||||||
],
|
],
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('karaoke', () => {
|
||||||
|
const lrc = LRC.parse(
|
||||||
|
'[00:00.00] <00:00.04> When <00:00.16> the <00:00.82> truth <00:01.29> is <00:01.63> found <00:03.09> to <00:03.37> be <00:05.92> lies',
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(lrc).toStrictEqual({
|
||||||
|
lines: [
|
||||||
|
{
|
||||||
|
duration: Infinity,
|
||||||
|
text: 'When the truth is found to be lies',
|
||||||
|
time: '00:00:00',
|
||||||
|
timeInMs: 0,
|
||||||
|
words: [
|
||||||
|
{
|
||||||
|
timeInMs: 4,
|
||||||
|
word: 'When',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 16,
|
||||||
|
word: 'the',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 82,
|
||||||
|
word: 'truth',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 1029,
|
||||||
|
word: 'is',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 1063,
|
||||||
|
word: 'found',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 3009,
|
||||||
|
word: 'to',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 3037,
|
||||||
|
word: 'be',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
timeInMs: 5092,
|
||||||
|
word: 'lies',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
tags: [],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@ -8,6 +8,7 @@ interface LRCLine {
|
|||||||
timeInMs: number;
|
timeInMs: number;
|
||||||
duration: number;
|
duration: number;
|
||||||
text: string;
|
text: string;
|
||||||
|
words: { timeInMs: number; word: string }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface LRC {
|
interface LRC {
|
||||||
@ -19,6 +20,9 @@ const tagRegex = /^\[(?<tag>\w+):\s*(?<value>.+?)\s*\]$/;
|
|||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
const timestampRegex = /^\[(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)\]/m;
|
const timestampRegex = /^\[(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)\]/m;
|
||||||
|
|
||||||
|
// prettier-ignore
|
||||||
|
const wordRegex = /<(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)> *(?<word>\w+)/g;
|
||||||
|
|
||||||
export const LRC = {
|
export const LRC = {
|
||||||
parse: (text: string): LRC => {
|
parse: (text: string): LRC => {
|
||||||
const lrc: LRC = {
|
const lrc: LRC = {
|
||||||
@ -65,13 +69,27 @@ export const LRC = {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const text = line.trim();
|
let text = line.trim();
|
||||||
|
const words = Array.from(text.matchAll(wordRegex), ({ groups }) => {
|
||||||
|
const { minutes, seconds, milliseconds, word } = groups!;
|
||||||
|
const timeInMs =
|
||||||
|
parseInt(minutes) * 60 * 1000 +
|
||||||
|
parseInt(seconds) * 1000 +
|
||||||
|
parseInt(milliseconds);
|
||||||
|
|
||||||
|
return { timeInMs, word };
|
||||||
|
});
|
||||||
|
|
||||||
|
if (words.length) {
|
||||||
|
text = words.map(({ word }) => word).join(' ');
|
||||||
|
}
|
||||||
|
|
||||||
for (const { time, timeInMs } of timestamps) {
|
for (const { time, timeInMs } of timestamps) {
|
||||||
lrc.lines.push({
|
lrc.lines.push({
|
||||||
time,
|
time,
|
||||||
timeInMs,
|
timeInMs,
|
||||||
text: text,
|
text,
|
||||||
|
words,
|
||||||
duration: Infinity,
|
duration: Infinity,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -92,10 +110,11 @@ export const LRC = {
|
|||||||
const first = lrc.lines.at(0);
|
const first = lrc.lines.at(0);
|
||||||
if (first && first.timeInMs > 300) {
|
if (first && first.timeInMs > 300) {
|
||||||
lrc.lines.unshift({
|
lrc.lines.unshift({
|
||||||
time: '0:0:0',
|
time: '00:00:00',
|
||||||
timeInMs: 0,
|
timeInMs: 0,
|
||||||
duration: first.timeInMs,
|
duration: first.timeInMs,
|
||||||
text: '',
|
text: '',
|
||||||
|
words: [],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user