1.3.3 • Published 3 years ago

parallelizer v1.3.3

Weekly downloads
22
License
MIT
Repository
github
Last release
3 years ago

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

ParamDescription
textsubtitles 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

ParamDescription
settingsSettings to parallelize two subtitles
settings.startThe start time in the each subtitles file
settings.endThe end time in the each subtitles file
settings.firstSubtitlesThe first subtitles text
settings.secondSubtitlesThe 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

ParamTypeDescription
nameA word or phrase to find
textThe text to parse
offsetObjectOffset 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

ParamDescription
textThe text to parse
startThe start timestamp
endThe end timestamp

srtTimeToSeconds

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

ParamDescription
timesrt 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

ParamDescription
secondsconvert seconds into srt time format

Example

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

resync

The function resynchonizes subtitles

ParamDescription
subtitlesarray of objects that parse function produces
timeoffset 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);
1.3.3

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.2.0

4 years ago

1.2.1

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago