JSPM

  • Created
  • Published
  • Downloads 24613
  • Score
    100M100P100Q133861F
  • License MIT

Parse, manipulate and stringify SRT (SubRip) format, with partial support for WebVTT

Package Exports

  • subtitle

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (subtitle) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

subtitle

Build Status Code Climate Coverage Status npm downloads

Parse, manipulate and stringify SRT (SubRip) format, with partial support for WebVTT.

"Thanks for this rad package!" John-David Dalton, creator of Lodash

Installation

npm

npm install subtitle

yarn

yarn add subtitle

API

The API is minimal and provides only six pure functions:

parse

  • parse(input: string): Caption[]

It receives a string containing a SRT or VTT content and returns an array of captions:

import { parse } from 'subtitle'
import fs from 'fs'

const input = fs.readFileSync('awesome-movie.srt', 'utf8')

parse(input)

// returns an array like this:
[
  {
    start: 20000, // milliseconds
    end: 24400,
    text: 'Bla Bla Bla Bla'
  },
  {
    start: 24600,
    end: 27800,
    text: 'Bla Bla Bla Bla',
    settings: 'align:middle line:90%'
  },
  // ...
]

stringify

  • stringify(captions: Caption[], options?: { format: 'srt' | 'vtt }): string

It receives an array of captions and returns a string in SRT (default), but it also supports VTT format through the options.

import { stringify } from 'subtitle'

stringify(captions)
// returns a string in SRT format

stringify(options, { format: 'vtt' })
// returns a string in VTT format

resync

  • resync(captions: Caption[], time: number): Caption[]

Resync all the given captions at once:

import { resync } from 'subtitle'

// Advance subtitles by 1s
const newCaptions = resync(captions, 1000)

// Delay 250ms
const newCaptions = resync(captions, -250)

parseTimestamp

  • parseTimestamp(timestamp: string): number

Receives a timestamp (SRT or VTT) and returns its value in milliseconds:

import { parseTimestamp } from 'subtitle'

parseTimestamp('00:00:24,400')
// => 24400

parseTimestamp('00:24.400')
// => 24400

parseTimestamps

  • parseTimestamps(timestamps: string): Timestamp

It receives a timestamps string, like 00:01:00,500 --> 00:01:10,800. It also supports VTT formats like 12:34:56,789 --> 98:76:54,321 align:middle line:90%.

import { parseTimestamps } from 'subtitle'

parseTimestamps('00:01:00,500 --> 00:01:10,800')
// => { start: 60500, end: 70800 }

parseTimestamps('12:34:56,789 --> 98:76:54,321 align:middle line:90%')
// => { start: 45296789, end: 357414321, settings: 'align:middle line:90%' }

formatTimestamp

  • formatTimestamp(timestamp: number, options?: { format: 'srt' | 'vtt' }): string

It receives a timestamp in milliseconds and returns it formatted as SRT or VTT:

import { formatTimestamp } from 'subtitle'

formatTimestamp(142542)
// => '00:02:22,542'

formatTimestamp(142542, { format: 'vtt' })
// => '00:02:22.542'

License

MIT