npm package: @srtkit/core
Description: A set of utilities for processing and manipulating SRT (SubRip Text) subtitle files.
Author: Max Chang
Latest version: 0.0.5

# @srtkit/core

<!-- automd:badges color="yellow" license name="@srtkit/core" bundlephobia -->

[![npm version](https://img.shields.io/npm/v/@srtkit/core?color=yellow)](https://npmjs.com/package/@srtkit/core)
[![npm downloads](https://img.shields.io/npm/dm/@srtkit/core?color=yellow)](https://npm.chart.dev/@srtkit/core)
[![bundle size](https://img.shields.io/bundlephobia/minzip/@srtkit/core?color=yellow)](https://bundlephobia.com/package/@srtkit/core)
[![license](https://img.shields.io/github/license/maxchang3/srtkit?color=yellow)](https://github.com/maxchang3/srtkit/blob/main/LICENSE)

<!-- /automd -->

Core utilities for processing SRT files.

## Installation

<!-- automd:pm-install name="@srtkit/core" -->

```sh
# ✨ Auto-detect
npx nypm install @srtkit/core

# npm
npm install @srtkit/core

# yarn
yarn add @srtkit/core

# pnpm
pnpm add @srtkit/core

# bun
bun install @srtkit/core

# deno
deno install npm:@srtkit/core
```

<!-- /automd -->

## Usage

### SRT Parsing and Stringifying

We provide a simple wrapper around [srt-parser-2](https://github.com/1c7/srt-parser-2), making it smoother to process SRT files with `@srtkit/core`.

```ts
import { SRT, type Subtitle, type SubtitleEntry } from '@srtkit/core'

const subtitles = SRT.parse(srtString) // Parse SRT string to array of subtitle objects
const srtString = SRT.stringify(subtitles) // Stringify array of subtitle objects to SRT string
```

### Features

We offer a range of utility functions for subtitle processing. Each function operates on subtitle objects, so you should first parse your SRT string into subtitle objects, apply the desired feature function(s), and then convert the processed objects back to an SRT string.

**Example workflow:**

```ts
import { SRT } from '@srtkit/core'
import { cleanBasic } from '@srtkit/core/features'

const subtitles = SRT.parse(srtString)           // Parse SRT string to objects
const cleaned = cleanBasic(subtitles)            // Apply feature function
const resultSrt = SRT.stringify(cleaned)         // Convert back to SRT string
console.log(resultSrt)
```

<!-- automd:jsdocs src="./src/features" headingLevel="3" -->

#### `cleanBasic(subtitle, options?)`

Clean basic subtitle issues. By default, only trims whitespace.

#### `cleanBetween(subtitle, options?)`

Clean text between specified delimiters. By default, does nothing.

#### `cleanFormatting(subtitle, options?)`

Clean formatting from subtitle cues. By default, does nothing.

#### `mergeSubtitles(a, b, mode)`

Merge two monolingual subtitles into a bilingual subtitle.

Modes:

- `simple`: Assumes both subtitle arrays have entries aligned by time range.

Merges each pair of entries by combining their text, separated by a newline,

using the longer subtitle array as the base.

#### `splitSubtitle(subtitle, mode)`

Split a bilingual subtitle into two monolingual subtitles.

Modes:

- `newline`: Split by the first '\n' in each entry's text

- `evenOdd`: Odd indices -> Language A, Even indices -> Language B

<!-- /automd -->

## License

[MIT](./LICENSE) License © [maxchang3](https://github.com/maxchang3)

<!-- automd:with-automd -->

---

_🤖 auto updated with [automd](https://automd.unjs.io)_

<!-- /automd -->