@braccato/parsers

中文文档
0.4.1发布于 2026/11/07MITTypeScriptES Module构建来源已验证近 30 天下载 7212

Lyrics format parsers: TTML, LRC, SRT, QRC, Plain

原始使用文档
文档目录7 节
  1. Install
  2. Usage
  3. Parser Interface
  4. Format specifics
  5. TTML
  6. LRC
  7. QRC

@braccato/parsers

Lyrics format parsers for TTML, LRC, SRT, QRC, and plain text, with automatic format detection. Produces the Lyric[] array that @braccato/core renders.

The parsers guide shows how to parse the response from each Better Lyrics API provider.

Install

npm i @braccato/parsers

Usage

import { detectParser } from "@braccato/parsers";

const parser = detectParser(inputText);
const lyrics = parser.parse(inputText, durationMs);

Or use a specific parser directly:

import { TTMLParser, LRCParser, SRTParser, QRCParser, PlainParser } from "@braccato/parsers";

const lyrics = TTMLParser.parse(ttmlString);

Parser Interface

All parsers implement:

interface LyricParser {
  parse(input: string, duration?: number): Lyric[];
  metadata(input: string): LyricMetadata;
  detect(input: string): boolean;
}

metadata reads what the file says about the song rather than its lines, which today is who wrote it. songwriters is empty when the file credits nobody:

const { songwriters } = detectParser(inputText).metadata(inputText); // ["Max Martin", "Savan Kotecha"]
FormatWhere the songwriters come from
TTML<songwriters> in any metadata container (Apple's iTunesMetadata, lrc.red's sourceMetadata), and Composer's <composer:meta key="songwriter">
LRC[au:] and [lr:]. [by:] names whoever made the file and is left out
QRCCredit lines such as 作词: or Written by:. Producer, mixing and the other roles are left out
SRT, PlainNothing

Names that LRC and QRC hold as free text are split on /, 、, , and ,. Every format drops duplicates and keeps the order the file gives.

detectParser tries each format in priority order: TTML, LRC, SRT, QRC, Plain. The same rules are exported on their own as detectFormat from the dependency-free @braccato/parsers/format subpath, which returns "ttml" | "lrc" | "srt" | "qrc" | "plain" without loading the XML parser.

TTMLParser.parse ignores its duration argument, because a TTML document states its own duration on <body dur>. Pass a duration through parseTTMLContent instead when the document omits it.

Format specifics

TTML

parseTTMLContent returns the parsed lines along with whether the document is word synced and the language it declares:

import { parseTTMLContent } from "@braccato/parsers";

const { lyrics, isWordSynced, language } = parseTTMLContent(ttmlString, {
  songDurationMs: 214000, // only used when <body> carries no dur
  instrumentalGapMs: 5000, // silence longer than this becomes an instrumental line
});

Instrumental lines are inserted whether or not the document states a duration. This changed in 0.2.0: 0.1.x only inserted them for a document whose <body> carried a dur, and the implementation this package was rewritten from does not gate them, so the gate is gone. A document with no dur now gains an intro instrumental when its first line starts more than instrumentalGapMs in, and one between any two lines separated by more than that. The outro is the exception, since nothing states where the song ends: pass songDurationMs to get it.

It reads syllable timing, ttm:role="x-bg" background vocals, ttm:agent (mapped to stable vocalist slots v1, v2, and v1000 for a group), explicit flags from either explicit or AMLL's obscene, itunes:key, translations and transliterations. Namespace prefixes that a document uses without declaring are recovered rather than rejected, and times may be clock values or offset times such as 432.25s, 5m or 250ms.

LRC

LRCParser.parse runs a set of timing fixers suited to Musixmatch word by word lyrics. For a source whose timings are already clean, use parseLRC, which returns the document exactly as stated:

import { parseLRC, lrcFixers } from "@braccato/parsers";

const lyrics = parseLRC(lrcText, durationMs, {
  instrumentalGapMs: 5000, // silence longer than this becomes an instrumental line
});
lrcFixers(lyrics); // optional, mutates in place

Instrumental lines follow the same rule as TTML. A blank timed line, or one holding only music notes, is not a lyric: it ends the line before it, and the silence it leaves becomes an instrumental line when it runs longer than instrumentalGapMs. Blank lines in a row make one break. In enhanced LRC, a timestamp with nothing after it closes the last word and the line, so the silence after it counts without a blank line. An intro is added when the first line starts after the threshold. An outro needs the last line to state where it ends, through a closing timestamp or a trailing blank line, because a plain last line runs to the end of the song.

QRC

QRCParser.parse accepts either the <QrcInfos> envelope QQ Music returns or a bare QRC body. Pass song metadata through parseQRC to drop the opening lines that only echo the title or artist:

import { parseQRC } from "@braccato/parsers";

const lyrics = parseQRC(qrcXml, durationMs, { title: "Song", artist: "Artist" });

Singer prefixes (Name: at the head of a line) become agents and are stripped from the text, sticking to the following lines until the next one appears. Credit lines are dropped.

See the full documentation for type definitions.

版本与发布记录

@braccato/parsers 共发布 12 个版本,首次发布于 2026/03/11;过去 12 个月发布了 12 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.4.1latest2026/11/07今天
0.4.02026/10/1821 天前
0.3.22026/10/011 个月前
0.3.12026/09/261 个月前
0.3.02026/09/261 个月前
0.2.32026/09/171 个月前

关于 @braccato/parsers 的常见问题

@braccato/parsers 是什么?

Lyrics format parsers: TTML, LRC, SRT, QRC, Plain

如何安装 @braccato/parsers?

在项目目录运行 npm install @braccato/parsers,或使用 pnpm add / yarn add / bun add。

@braccato/parsers 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@braccato/parsers 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@braccato/parsers 可以商用吗?

@braccato/parsers 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@braccato/parsers 的下载量有多少?

根据 npm 官方下载统计,@braccato/parsers 在 2026/10/09 – 2026/11/07 这 30 天内被下载 7,212 次,日均约 240 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@braccato/parsers 还在维护吗?

最新版本 0.4.1 发布于 2026/11/07(今天);至今共发布 12 个版本,其中过去 12 个月发布了 12 个;首次发布于 2026/03/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。