npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

lyric-kit

v0.7.0

Published

A lightweight, zero-dependency toolkit for parsing, serializing, and processing lyrics across multiple formats (LRC, TTML, QRC, YRC, KRC, LyS, SRT, ASS).

Readme

lyric-kit

用于解析、序列化及同步多格式歌词(LRC、TTML、QRC、KRC、YRC、LyS、SRT、ASS)的高性能 TypeScript 工具库。

npm version npm downloads License TypeScript Zero Dependency

English | 简体中文

安装

pnpm add lyric-kit
# 或
npm install lyric-kit

快速上手

解析歌词

parseLyric 支持传入纯歌词文本字符串,或包含翻译/罗马音的配置对象。

LRC 支持整秒标签 [mm:ss] 与 1~3 位小数标签,也可混用;正文、翻译、罗马音及增强 LRC 逐字标签使用相同规则。KRC 的分秒行头同样支持省略小数,原生整数毫秒行头不变。SRT 兼容整秒时间,跳过时间无效或结束早于开始的字幕块。ASS 可根据 Dialogue 行自动识别,无需完整文件头。TTML 支持 h、m、s、ms 时间单位。

TTML 的逐字、注音、背景人声及对唱规则以 applemusic-like-lyrics 为参照;行时间会涵盖逐字及背景人声的范围。外部翻译与罗马音优先匹配相同歌手 ID 和声部,再在 300ms 内对齐时间;不同的明确歌手 ID 不互相匹配,未标记歌手的辅助文件仍可按时间对齐。

import { parseLyric } from "lyric-kit";

// 1. 传入纯文本(自动检测格式)
const { lines, metadata } = parseLyric(`
[ti:歌曲名]
[ar:歌手名]
[00:01.00]第一行歌词
[00:03.00]第二行歌词
`);

// 2. 传入主歌词、翻译与罗马音(自动时间戳对齐)
const result = parseLyric({
  content: "[00:01.00]Hello\n[00:03.00]Goodbye",
  translation: "[00:01.00]你好\n[00:03.00]再见",
  romaji: "[00:01.00]haro\n[00:03.00]gubbai",
});

序列化导出

支持使用独立序列化函数或统一入口 serializeLyric:

import { toEnhancedLRC, toLRC, toSRT, toTTML } from "lyric-kit";

// 导出为标准逐行 LRC 格式
const lrc = toLRC(lines);

// 逐字时间戳增强型 LRC 格式
const elrc = toEnhancedLRC(lines);

// Apple Music TTML XML 格式(支持传入 lines 或包含 metadata 的 result)
const ttml = toTTML(result);

// SubRip 字幕格式
const srt = toSRT(lines);

// 也可通过 serializeLyric 统一入口导出
import { serializeLyric } from "lyric-kit";
const output = serializeLyric(lines, "ttml");

默认导出格式保持兼容。如需再次解析导出结果,可显式传入 roundTrip: true:

const elrc = serializeLyric(lines, "elrc", { roundTrip: true });
const srt = toSRT(lines, { roundTrip: true });
const restored = parseLyric(srt, { multiLineMode: "bilingual" });

该选项也支持 toLRC 和 toEnhancedLRC。LRC 导出使用毫秒精度,保留辅助行的和声标记,并在只有罗马音时使用 // 占据翻译位置;增强 LRC 还会写入有效的词尾时间标记。SRT 使用“原文、翻译、罗马音”顺序,读取时须使用 bilingual 模式。未开启时,原有输出顺序与厘秒时间格式不变。

serializeLyric 的第三参数使用 SerializeOptions,解析配置放在 parse 中,例如 serializeLyric(content, "elrc", { roundTrip: true, parse: { applyOffset: true } })。已有的扁平解析参数调用仍然兼容,新代码建议使用嵌套配置。传入已解析的歌词数组或结果时不再执行解析。

这不是任意歌词对象的无损编码:标准 LRC 不记录结束时间;LRC 不保留歌手 ID、对唱标记和 ruby;SRT 不保留逐词时间、和声标记和 ruby;增强 LRC 的词尾标记只适用于有效的正时长片段。需要更完整的结构时可使用 TTML。

清理元数据标头

过滤歌词开头的演职人员信息与版权声明等非歌词文本行:

import { stripLyricMetadata } from "lyric-kit";

const cleanedLines = stripLyricMetadata(lines, {
  matchMetadata: {
    title: "歌曲名",
    artists: ["歌手名"],
  },
});

比较歌词版本

以一份歌词为基准,判断候选歌词是否为同一版本,并估算时间偏移。比较时忽略标点、元数据、语气词、背景人声和翻译;简繁转换需在调用前完成:

import { applyTimestampOffset, compareLyrics, hasWordTiming } from "lyric-kit";

const { verdict, offsetMs } = compareLyrics(officialLines, candidateLines, {
  matchMetadata: { title: "歌曲名", artists: ["歌手名"] },
});

// same:同版本;shifted:同版本但需平移;different:不同版本;unknown:无法判断
if (verdict === "shifted") applyTimestampOffset(candidateLines, -offsetMs);

// 是否有真实的逐字时间
hasWordTiming(candidateLines);

智能推断歌词语言

自动结合假名振假名注音(ruby)、全曲翻译与 CJK 字符比例,为歌词行推断并注入语言代码(ja / ko / zh-CN / und-Latn),便于界面选择字形(CJK 异体字)或对接 TTS / 音译:

import { applyLyricLanguages } from "lyric-kit";

// 原地推断并填充各行的 language 属性
applyLyricLanguages(lines);
console.log(lines[0].language); // 例如: "ja"

播放同步与卡拉OK进度

import {
  createActiveLyricFinder,
  findActiveLyricIndices,
  findLyricIndex,
  getWordSweepProgress,
} from "lyric-kit";

// 1. 单行快速查找当前激活行
const currentMs = 1500;
const lineIndex = findLyricIndex(lines, currentMs);

// 2. 多行和声/对唱重叠查找(返回当前时间所有活跃行下标)
const activeIndices = findActiveLyricIndices(lines, currentMs);

// 每帧查询或长歌词可预建时间索引,lines 须按 startTime 排序
const findActive = createActiveLyricFinder(lines);
const active = findActive(currentMs);
// 修改歌词顺序或时间戳后需重新创建索引

if (lineIndex !== -1) {
  const activeLine = lines[lineIndex];

  // 3. 计算每个字/音节的卡拉OK平滑染色进度 [0, 1]
  for (const word of activeLine.words) {
    const progress = getWordSweepProgress(word, activeLine.startTime, currentMs);
  }
}

API 参考

parseLyric(input, options?)

解析歌词并返回 LyricResult。

  • input: string | LyricInput
    • content: 主歌词文本。
    • format?: 手动指定主歌词格式。
    • translation?: 翻译文本。
    • translationFormat?: 手动指定翻译格式。
    • romaji?: 罗马音文本。若包含逐字时间戳(如 QRC / KRC / TTML),会自动将音节对齐挂载至各词的 word.romanWord。
    • romajiFormat?: 手动指定罗马音格式。
    • kana?: 独立的振假名注音文本(可选,若主歌词未解析出内嵌注音时生效,避免重复叠加)。
  • options: ParseOptions(可选)

ParseOptions

| 参数 | 类型 | 默认值 | 说明 | | :----------------- | :---------------------- | :------- | :------------------------------------------------------------------------------------------------------------------------------ | | format | LyricFormat | 自动检测 | 手动指定格式(lrc、ttml、qrc、krc、yrc、lys、srt、ass)。 | | detectBackground | boolean | true | 是否识别并分离括号内的和声与伴唱(标记为 isBG)。 | | extractMetadata | boolean | false | 是否提取歌曲元数据(歌曲名、歌手、专辑、制作人员、偏移量等)。 | | cleanKangxi | boolean | false | 是否将康熙部首及 CJK 兼容字符规范化为通用汉字。 | | applyOffset | boolean | false | 是否将输入的 offset 毫秒数累加至行、词和注音时间戳中(newTime = originalTime + offset,最小为 0),独立于 extractMetadata。 | | keepEmptyLines | boolean | false | 是否保留纯空白文本行(间奏标记)。默认 false 过滤空行并截断前行;true 时完整保留供播放器处理间奏。 | | multiLineMode | "join" \| "bilingual" | "join" | SRT 多行解析模式。join 用空格连接多行;bilingual 首行为原文,第二行为译文,第三行为音译。 | | preferredLang | string | "" | 多轨道 TTML 解析时优先匹配的翻译语言代码(如 "zh-CN")。 | | domParser | DOMParserLike | 自动 | 自定义 DOMParser 实例或构造函数(Node.js 等无原生 DOM 环境下解析 TTML / QRC 必填)。 |

独立格式解析器与序列化器

可直接导入各格式的专属解析与导出函数(统一采用全大写规范命名):

import {
  parseLRC,
  parseTTML,
  parseQRC,
  parseKRC,
  parseYRC,
  parseLyS,
  parseSRT,
  parseASS,
  toLRC,
  toEnhancedLRC,
  toTTML,
  toSRT,
} from "lyric-kit";

// 解析
const result = parseLRC(lrcText, { cleanKangxi: true });

// 序列化
const ttmlXml = toTTML(result);
const lrcText = toLRC(result.lines);

detectFormat(text)

分析原始文本特征并返回识别出的格式: "lrc" | "ttml" | "qrc" | "krc" | "yrc" | "lys" | "srt" | "ass"。

数据模型

interface LyricResult {
  lines: LyricLine[];
  metadata: LyricMetadata;
}

interface LyricLine {
  id?: string;
  language?: "ja" | "ko" | "zh-CN" | "und-Latn";
  words: LyricWord[];
  translatedLyric: string;
  romanLyric: string;
  startTime: number; // 毫秒 (ms)
  endTime: number; // 毫秒 (ms)
  isBG: boolean;
  isDuet: boolean;
  agentId?: string;
  songPart?: string;
  blockIndex?: number;
}

interface LyricWord {
  word: string;
  startTime: number; // 毫秒 (ms)
  endTime: number; // 毫秒 (ms)
  romanWord?: string; // 逐字罗马音/拼音音译 (如 "kai", "zeoi")
  ruby?: LyricSpan[]; // 振假名/注音跨度数组 (如日文假名 "かい")
  obscene?: boolean;
  emptyBeat?: number;
}

interface LyricSpan {
  word: string;
  startTime: number; // 毫秒 (ms)
  endTime: number; // 毫秒 (ms)
}

开源协议

AGPL-3.0