This is a SRT parser for Javascript
It read .srt
file into an array
npm
npm install srt-parser-2
or yarn
yarn add srt-parser-2
This is a srt format file:
1
00:00:11,544 --> 00:00:12,682
Hello
it would become:
[{
id: '1',
startTime: '00:00:11,544',
endTime: '00:00:12,682',
text: 'Hello'
}]
Since it only process text,
it should work in both Browser
and Node.js
enviroment
var { default: srtParser2 } = require("srt-parser-2")
var parser = new srtParser2()
var srt = `
1
00:00:11,544 --> 00:00:12,682
Hello
`
var result = parser.fromSrt(srt);
console.log(result);
npx srt-parser-2 -i input.srt -o output.json --minify
Options:
Option | Required | Default |
---|---|---|
--input or -i | Yes | |
--output or -o | No | output.json |
--minify | No | false |
MIT
Why this one special? There are plently SRT parser on npm like:
- subtitles-parser: Last update(on npmjs): 2013-10-17
- subtitles-parser-vtt: Last update: 2020-2-14
- subtitle: Last update: 2019-4-4
- srt: Last update: 2012-7-18
Nothing wrong.
All of them can handle this format:
1
00:00:11,544 --> 00:00:12,682
Hello
00:00:11.544
This is wrong format, it use period as separator
Or this:
00:00:11,5440
This is also wrong format, millisecond has 4 digit
Or this:
1:00:11,5
Similiar, hour & millisecond is only 1 digit (wrong)
Or this
00:00:00.05
etc
Format | Other parser | srt-parser-2 | srt-parser-2 would turn this into |
---|---|---|---|
00:00:01,544 | Yes ✅ | Yes ✅ | 00:00:01,544 |
00:00:01.544 | ❓ Yes for some of them | Yes ✅ | 00:00:01,544 |
00:00:01.54 | ❓ Yes for some of them | Yes ✅ | 00:00:01,544 |
00:00:00.3333 | No ❌ | Yes ✅ | 00:00:00,333 |
00:00:00.3 | No ❌ | Yes ✅ | 00:00:00,300 |
1:2:3.4 | No ❌ | Yes ✅ | 01:02:03,400 |
Basic principle:
- If hour,minute,second is shorter than 2 digit, pad start with "0", if longer than 2 digit, only save first 2 digit.
- Millisecond is the same, but it's 3 digit.
- Seperator can be
.
(periods) or,
(comma), periods(incorrect) will be replace with comma(correct)
- Support more time format (even wrong format)
- Have extensive test
I am writing Tern - Subtitle File Translator
Some of the user says they have trouble translate some of the .srt
file
And I found out these .srt
file have format like 00:00:01.544
and 00:00:00.05
that's why I write this