npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@splayer/subtitle

v2.0.3

Published

Parse and manipulate SRT (SubRip)

Downloads

25

Readme

subtitle.js

Build Status Code Climate Coverage Status npm version JavaScript Style Guide

Parse, manipulate and stringify SRT (SubRip) format. WebVTT as input is also supported.

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

Installation

npm install subtitle --save

Usage

// ES2015 modules
import * as Subtitle from 'subtitle'
import { parse, stringify, stringifyVtt, resync, toMS, toSrtTime, toVttTime } from 'subtitle'
// ES6 CommonJS
const Subtitle = require('subtitle')
const { parse, stringify, stringifyVtt, resync, toMS, toSrtTime, toVttTime } = require('subtitle')
// ES5 CommonJS
var Subtitle = require('subtitle')
Subtitle.parse
Subtitle.stringify
Subtitle.stringifyVtt
Subtitle.resync
Subtitle.toMS
Subtitle.toSrtTime
Subtitle.toVttTime

Browser

If you don't use a bundler like Webpack or Browserify, you can just copy the script subtitle.bundle.js from the dist folder and link it to your page.

<script src="path/to/subtitle.bundle.js"></script>
<script>
  // `Subtitle` will be globally available
  console.log(window.Subtitle)
  /*
    {
      parse: function parse()
      resync: function resync()
      stringify: function stringify()
      stringifyVtt: function stringifyVtt()
      toMS: function toMS()
      toSrtTime: function toSrtTime()
      toVttTime: function toVttTime()
  */
</script>

API

The API is minimal and provide only five functions, two of which have SRT and WebVTT variants:

parse(srt: String) -> Array

Parses a SRT or WebVTT string and returns an array:

parse(mySrtOrVttContent)
[
  {
    start: 20000, // time in ms
    end: 24400,
    text: 'Bla Bla Bla Bla'
  },
  {
    start: 24600,
    end: 27800,
    text: 'Bla Bla Bla Bla',
    settings: 'align:middle line:90%' // WebVTT only
  }
]

stringify(captions: Array) -> String

The reverse of parse. It gets an array with subtitles and converts it to a valid SRT string.

The stringifyVtt(captions: Array) -> String function is also available for converting to a valid WebVTT string.

const subtitles = [
  {
    start: '00:00:20,000',
    end: '00:00:24,400',
    text: 'Bla Bla Bla Bla'
  },
  {
    start: 24600, // timestamp in milliseconds is supported as well
    end: 27800,
    text: 'Bla Bla Bla Bla',
    settings: 'align:middle line:90%' // Ignored in SRT format
  }
]

const srt = stringify(subtitles)
// returns the following string:
/*
1
00:00:20,000 --> 00:00:24,400
Bla Bla Bla Bla

2
00:00:24,600 --> 00:00:27,800
Bla Bla Bla Bla
*/

const vtt = stringifyVtt(subtitles)
// returns the following string:
/*
WEBVTT

1
00:00:20.000 --> 00:00:24.400
Bla Bla Bla Bla

2
00:00:24.600 --> 00:00:27.800 align:middle line:90%
Bla Bla Bla Bla
*/

resync(captions: Array, time: Number) -> Object

Resync all captions at once.

const subtitles = [
  {
    start: '00:00:20,000',
    end: '00:00:24,400',
    text: 'Bla Bla Bla Bla'
  },
  {
    start: 24600, // timestamp in millseconds is supported as well
    end: 27800,
    text: 'Bla Bla Bla Bla'
  }
]

// Advance 1s
const newSubtitles = resync(subtitles, 1000)

// Delay 250ms
const newSubtitles = resync(subtitles, -250) //

// Then, you can stringify your new subtitles:
stringify(newSubtitles)

toMS(timestamp: String) -> Number

Convert a SRT or WebVTT timestamp to milliseconds:

toMS('00:00:24,400')
// 24400

toMS('00:24.400')
// 24400

toSrtTime(timestamp: Number) -> String

Convert a time from milliseconds to a SRT timestamp:

toSrtTime(24400)
// '00:00:24,400'

toVttTime(timestamp: Number) -> String

Convert a time from milliseconds to a WebVTT timestamp:

toVttTime(24400)
// '00:00:24.400'

Tests

Subtitle.js uses AVA for running tests and nyc for code coverage.

If you want to run these tests, you need to install all devDependencies:

npm install

Now you can run the tests with the following command:

npm test

Code Coverage

You can check the code coverage by running the following command:

npm run coverage

If you want a pretty HTML report, run the following:

npm run report

Your report will be available in the coverage folder.

License

MIT