可以解析字幕文本的解析器
npm install subtitle-parser
或者使用yarn
yarn install subtitle-parser
const subtitleParser = require("subtitle-parser");
const contentSRT =
`
1
00:00:01,410 --> 00:00:04,220
我们已经走得太远,以至于忘记了为什么出发。
We already walked too far, down to we had forgotten why embarked.
2
00:00:04,251 --> 00:00:06,234
天下没有不散的筵席
There is no never-ending feast
`;
// 你可以不写前五行
// 这不是这首歌歌词对应的时间,只是测试使用
/**
* ar: 艺人名
* ti: 标题
* al: 专辑
* by: 编者(做LRC歌词的人)
* offset: 时间补偿值(毫秒)
*/
const contentLRC =
`
[ar:A lot]
[ti:Something just like this]
[al:Memories... Do Not Open]
[by:RotCool]
[offset:0]
[00:05:50]I've been reading books of old
[00:10:50][00:30:51]I want to something just like this
`;
// 第二个参数不是必要的
console.log(subtitleParser.parse(contentSRT, "SRT"));
console.log(JSON.stringify(subtitleParser.parse(contentLRC, "LRC")));
它将会输出:
[
{
id: '1',
startTime: { baseForm: '00:00:01,410', htmlCurrentTime: 1.41 },
endTime: { baseForm: '00:00:04,220', htmlCurrentTime: 4.22 },
content: '我们已经走得太远,以至于忘记了为什么出发。\n' +
'We already walked too far, down to we had forgotten why embarked.'
},
{
id: '2',
startTime: { baseForm: '00:00:04,251', htmlCurrentTime: 4.251 },
endTime: { baseForm: '00:00:06,234', htmlCurrentTime: 6.234 },
content: '天下没有不散的筵席\nThere is no never-ending feast'
}
]
{
"content": [{
"time": {
"baseForm": "00:05:50",
"minute": 0,
"second": 5,
"millisecond": 50,
"htmlCurrentTime": 5.05
},
"content": "I've been reading books of old"
}, {
"time": {
"baseForm": "00:10:50",
"minute": 0,
"second": 10,
"millisecond": 50,
"htmlCurrentTime": 10.05
},
"content": "I want to something just like this"
}, {
"time": {
"baseForm": "00:30:51",
"minute": 0,
"second": 30,
"millisecond": 51,
"htmlCurrentTime": 30.051
},
"content": "I want to something just like this"
}],
"ar": "A lot",
"ti": "Something just like this",
"al": "Memories... Do Not Open",
"by": "RotCool",
"offset": "0"
}