New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

srt-parser-2

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

srt-parser-2

Parse SRT into array

latest
Source
npmnpm
Version
1.2.3
Version published
Weekly downloads
58K
-4.04%
Maintainers
1
Weekly downloads
 
Created
Source

srt-parser-2

An SRT parser for Javascript.

It reads an .srt file into an array.

Install

npm

npm install srt-parser-2

or yarn

yarn add srt-parser-2

Example

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',
    startSeconds: 11.544,
    endTime: '00:00:12,682',
    endSeconds: 12.682,
    text: 'Hello'
}]

Enviroment support

Since it only process text,
it should work in both Browser and Node.js enviroment

Usage

let srt = `
1
00:00:11,544 --> 00:00:12,682
Hello
`;

import srtParser2 from "srt-parser-2";
var parser = new srtParser2();
var srt_array = parser.fromSrt(srt);
console.log(srt_array);

// turn array back to SRT string.
var srt_string = parser.toSrt(srt_array);
console.log(srt_string);

You can run this example using node example/1.Comma.js

CLI

npx srt-parser-2 -i input.srt -o output.json --minify

Options:

OptionRequiredDefault
--input or -iYes
--output or -oNooutput.json
--minifyNofalse

License

MIT

Why?

Why this one special? There are plently SRT parser on npm:

  • subtitles-parser
  • subtitles-parser-vtt
  • subtitle
  • srt

What's wrong with them?

Nothing wrong.
All of them can handle this format:

1
00:00:11,544 --> 00:00:12,682
Hello

But I want to handle format like these:

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 (should be 3)

Or this:

1:00:11,5

Similiar, hour & millisecond is only 1 digit (wrong)

Or this

00:00:00.05

etc

Format Support

FormatOther parsersrt-parser-2srt-parser-2 would turn this into
00:00:01,544Yes :white_check_mark:Yes :white_check_mark:00:00:01,544
00:00:01.544:question: Yes for some of themYes :white_check_mark:00:00:01,544
00:00:01.54:question: Yes for some of themYes :white_check_mark:00:00:01,544
00:00:00.3333No :x:Yes :white_check_mark:00:00:00,333
00:00:00.3No :x:Yes :white_check_mark:00:00:00,300
1:2:3.4No :x:Yes :white_check_mark: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)

Conclusion

  • Support more time format (even wrong format)
  • Have extensive test

Keywords

subtitles

FAQs

Package last updated on 14 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts