parallelizer

1.3.3 • Public • Published

subtitles-parallelizer (WIP)

A javascript library to easily work with subtitles (.srt). It helps to parse and easily process subtitles!

Installation

npm install parallelizer or yarn add parallelizer

Example

Imagine, we downloaded subtitles with name movie.srt. For instance, to parse a movie trascript to work with this in the further future.

The API of subtitles-parallelizer or simply parallelizer is pretty simple. Without further ado, let's get to the point and take a look at an example.

1
00:00:01,000 --> 00:00:05,000
Subtitle 1.1
Subtitle 1.2

2
00:00:30,500 --> 01:30:00,000
Subtitle 2.1
Subtitle 2.2
Happy end 2.3
  1. Load file or get subtitles from third-party services and put them into a variable
import { promises as fs } from "fs";
import * as parallelizer from "parallelizer";

const run = async () => {
  const fileContent = await fs.readFile("movie.srt", "utf8");
  const sections = parallelizer.parse(text);

  console.log(sections);
};

run();

There we go, this is how our sections data array looks like

[
  {
    id: 1,
    startTime: '00:00:01',
    endTime: '00:00:05',
    startTimeWithMs: '00:00:01,000',
    endTimeWithMs: '00:00:05,000',
    content: 'Subtitle 1.1\n    Subtitle 1.2'
  },
  {
    id: 2,
    startTime: '00:00:30',
    endTime: '01:30:00',
    startTimeWithMs: '00:00:30,500',
    endTimeWithMs: '01:30:00,000',
    content: 'Subtitle 2.1\n    Subtitle 2.2\n    Happy end 2.3'
  }
]

API

parse

The function takes subtitles text in srt format and returns sections as an array of objects

parseBoth

The function takes settings and returns two parallelized subtitles (for instance, to parallelize two different subtitles in different languages)

parseByName

The function takes name and subtitles text to find a word or a phrase in each section's content of the subtitles

parseByTimestamp

The function takes subtitles text, start and end time to get sections between specific timestamps

srtTimeToSeconds

The function takes time in srt format (such as 00:01:00,200) and returns seconds

secondsToSrtTime

The function is the opposite of the srtTimeToSeconds function it takes time in seconds and returns a string in srt time format

resync

The function resynchonizes subtitles

parse

The function takes subtitles text in srt format and returns sections as an array of objects

Param Description
text subtitles text to parse into array of objects

Example

 const subtitles = `
 1
    00:00:01,000 --> 00:00:05,000
    Subtitle 1.1
    Subtitle 1.2
    `
 parse(subtitles)

 // output
 [
      {
        id: 1,
        startTime: '00:00:01',
        endTime: '00:00:05',
        startTimeWithMs: '00:00:01,000',
        endTimeWithMs: '00:00:05,000',
        content: 'Subtitle 1.1\nSubtitle 1.2'
      },
    ]

parseBoth

The function takes settings and returns two parallelized subtitles (for instance, to parallelize two different subtitles in different languages)

Returns: the tuple (array with two items), where items are the data structures of the same output as the parse function returns

Param Description
settings Settings to parallelize two subtitles
settings.start The start time in the each subtitles file
settings.end The end time in the each subtitles file
settings.firstSubtitles The first subtitles text
settings.secondSubtitles The second subtitles text

parseByName

The function takes name and subtitles text to find a word or a phrase in each section's content of the subtitles The function takes a third argument as offset object to widen array of objects left or right or both

Returns: array of objects (sections) like parse function does

Param Type Description
name A word or phrase to find
text The text to parse
offset Object Offset configuration object

parseByTimestamp

The function takes subtitles text, start and end time to get sections between specific timestamps

Returns: the same array of objects as parse function does

Param Description
text The text to parse
start The start timestamp
end The end timestamp

srtTimeToSeconds

The function takes time in srt format (such as 00:01:00,200) and returns seconds

Param Description
time srt time to convert in seconds

Example

srtTimeToSecond("00:01:30,000"); // 90

secondsToSrtTime

The function is the opposite of the srtTimeToSeconds function it takes time in seconds and returns a string in srt time format

Param Description
seconds convert seconds into srt time format

Example

secondsToSrtTime(90); // "00:01:30,000"

resync

The function resynchonizes subtitles

Param Description
subtitles array of objects that parse function produces
time offset time (you can use negative value for flexibity)

Example

const unresyncedSubtitles = [
  {
    id: 1,
    content: "a lot of text",
    startTime: "00:00:26",
    endTime: "00:00:29",
    startTimeWithMs: "00:00:26,500",
    endTimeWithMs: "00:00:29,461",
  },
  {
    id: 2,
    content: "a lot of text",
    startTime: "00:00:29",
    endTime: "00:00:36",
    startTimeWithMs: "00:00:29,500",
    endTimeWithMs: "00:00:36,461",
  },
];

resync(unresyncedSubtitles, 2000);

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.3.3
    5
    • latest

Version History

Package Sidebar

Install

npm i parallelizer

Weekly Downloads

5

Version

1.3.3

License

MIT

Unpacked Size

30.3 kB

Total Files

26

Last publish

Collaborators

  • termoxin