Library to parallelize subtitles (.srt)
npm install parallelizerA javascript library to easily work with subtitles (.srt). It helps to parse and easily process subtitles!
- Installation
- Example
- API
npm install parallelizer
oryarn add parallelizer
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
``js
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'
}
]
The function takes subtitles text in srt format and returns sections as an array of objects
The function takes settings and returns two parallelized subtitles
(for instance, to parallelize two different subtitles in different languages)
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 subtitles text, start and end time to get sections between specific timestamps
The function takes time in srt format (such as 00:01:00,200) and returns seconds
The function is the opposite of the srtTimeToSeconds function
it takes time in seconds and returns a string in srt time format
The function resynchonizes subtitles
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
`js
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'
},
]
`
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 |
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 |
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 |
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
`js`
srtTimeToSecond("00:01:30,000"); // 90
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
`js`
secondsToSrtTime(90); // "00:01:30,000"
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
`js
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);
``