https://github.com/leawind/srt-parser-ts
SubRip Text (SRT) parser
https://github.com/leawind/srt-parser-ts
deno srt subrip subtitle typescript typescript-library
Last synced: 2 months ago
JSON representation
SubRip Text (SRT) parser
- Host: GitHub
- URL: https://github.com/leawind/srt-parser-ts
- Owner: Leawind
- License: gpl-3.0
- Created: 2025-03-21T08:22:59.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-24T14:53:06.000Z (3 months ago)
- Last Synced: 2025-03-24T15:44:37.287Z (3 months ago)
- Topics: deno, srt, subrip, subtitle, typescript, typescript-library
- Language: TypeScript
- Homepage: https://jsr.io/@leawind/srt-parser
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SubRip Text (SRT) Parser
[](https://github.com/LEAWIND/srt-parser-ts)
[](https://jsr.io/@leawind/srt-parser)
[](https://jsr.io/@leawind/srt-parser/doc)
[](https://github.com/Leawind/srt-parser-ts/actions/workflows/deno-test.yaml)This project provides a parser for SubRip Text (SRT) files, which are commonly used for subtitles. The parser can read, manipulate, and write SRT files.
## Features
- Parse SRT files into structured data.
- Manipulate subtitle nodes.
- Convert subtitle data back into SRT format.## Usage
### Parsing an SRT File
You can parse an SRT file using the `SubRipText.parse` method:
```typescript
import { SubRipText } from '@leawind/srt-parser';// Example SRT content
const srtContent = `
1
00:00:01,000 --> 00:00:04,000
Hello, world!2
00:00:05,000 --> 00:00:08,000
This is a subtitle example.
`;// Parse the SRT content
const srt = SubRipText.parse(srtContent);// Access subtitle nodes
srt.nodes.forEach(node => {
console.log(node.toString());
});
```### Creating and Manipulating Subtitle Nodes
You can create and manipulate subtitle nodes using the `SrtNode` class:
```typescript
import { SrtNode } from '@leawind/srt-parser';// Create a new subtitle node
const node = new SrtNode(1, 1000, 4000, 'Hello, world!');// Modify the subtitle text
node.subtitle = 'Hello, universe!';// Convert the node to SRT format
console.log(node.toString());
```### Error Handling
The parser provides detailed error messages when encountering invalid SRT content:
```typescript
import { SrtSyntaxError, SubRipText } from '@leawind/srt-parser';try {
const srt = SubRipText.parse('Invalid SRT content');
} catch (error) {
if (error instanceof SrtSyntaxError) {
console.error('Syntax error:', error.message);
} else {
console.error('Unexpected error:', error);
}
}
```