/srt-parser-ts

SubRip Text (SRT) parser

Primary LanguageTypeScriptGNU General Public License v3.0GPL-3.0

SubRip Text (SRT) Parser

GitHub License JSR Version deno score GitHub Actions Workflow Status

A parser for reading and manipulating SubRip Text (SRT) format.

Features

  • Parse SRT files into structured data.
  • Manipulate subtitle nodes.
  • Convert subtitle data back into SRT format.
  • Find subtitles that should be displayed at a specific time.
  • Shift subtitle timing by a specified offset.
  • Support for negative time values.
  • Support for multiple time formats (hh:mm:ss,mmm, hh:mm:ss.mmm, mm:ss,mmm, mm:ss, hh:mm:ss).

Usage

For example, we have a SRT file example.srt with the following content:

1
00:00:01,000 --> 00:00:04,000
Hello, world!

We can write a script to read the SRT file and manipulate the subtitle nodes:

import { type SrtNode, SubRipText } from '@leawind/srt-parser';

// Reformat SRT content
const reformatted: string = SubRipText.reformatFile('example.srt');

// Load SRT from file
const srt: SubRipText = SubRipText.fromFile('./example.srt');

// Manipulate subtitles
srt.shiftTime(1000); // Shift by 1 second
const nodes: SrtNode[] = srt.getNodesAt(3000); // Find all subtitles nodes at 3 seconds