subtitler 1.2.0

Parse, convert, validate, edit, and generate subtitles in 9 formats (SRT, VTT, ASS/SSA, MicroDVD, SubViewer, TTML, SBV, LRC). Full CLI included.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Subtitler

> A Rust library for parsing, manipulating, and generating subtitles in multiple formats.

[![Crates.io](https://img.shields.io/crates/v/subtitler?style=flat-square)](https://crates.io/crates/subtitler)
[![License](https://img.shields.io/badge/license-Apache%202.0-blue?style=flat-square)](LICENSE)

- SRT, WebVTT, ASS/SSA, **MicroDVD**, and **SubViewer** format support
- Rich text extraction (bold, italic, underline, color, voice tags)
- Encoding detection and auto-decoding (UTF-8, UTF-16, BOM, chardetng fallback)
- Format detection, conversion, and validation
- Frame-based timecode support
- Utility operations: sort, merge, split, validate, framerate transform
- Async I/O powered by `tokio`
- Serialize/Deserialize via `serde`

## Installation

```sh
cargo add subtitler
```

Or as a CLI tool:

```sh
cargo install subtitler
```

## Quick Start

### Parse (any format, auto-detect)

```rust
// High-level entry points auto-detect the format.
let data = std::fs::read("subtitle.srt")?;
let file = subtitler::parse_bytes(&data)?;
println!("{} subtitles, format: {:?}", file.subtitles().len(), file.format());

// Or from a file / URL (async, real I/O):
// let file = subtitler::parse_file("subtitle.srt").await?;
// let file = subtitler::parse_url("https://example.com/sub.vtt").await?;
```

The high-level entry points require the `SubtitleFormat` trait to be in scope
for methods like `subtitles()` / `validate()`:

```rust
use subtitler::SubtitleFormat; // brings trait methods into scope
```

### Parse a specific format (low-level)

```rust
use subtitler::srt;

// Parsing cores are sync (they never did real async I/O).
let content = "1\n00:00:01,000 --> 00:00:03,500\nHello, world!\n\n";
let subtitles = srt::parse_content(content)?;
println!("{:?}", subtitles);
```

```rust
use subtitler::vtt;
let content = "WEBVTT\n\n1\n00:00:01.000 --> 00:00:03.500\nHello, world!\n\n";
let subtitles = vtt::parse_content(content)?;
```

```rust
use subtitler::ass;
let content = "[Script Info]\nScriptType: v4.00+\n\n[V4+ Styles]\nFormat: Name, Fontname, ...\nStyle: Default,Arial,20,...\n\n[Events]\nFormat: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text\nDialogue: 0,0:00:01.00,0:00:03.50,Default,,0,0,0,,Hello!\n";
let file = ass::parse_content(content)?;
println!("{}", file.to_string());
```

### Convert between formats

```rust
use subtitler::SubtitleFormat;
use subtitler::model::Format;

let file = subtitler::parse_file("input.srt").await?;
let vtt_str = file.to_string_with_format(&Format::Vtt);
std::fs::write("output.vtt", vtt_str)?;
```

### Generate subtitle files

```rust
use subtitler::model::Subtitle;
use subtitler::srt;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let subtitles = vec![
        Subtitle::new(1000, 3500, "Hello!"),
        Subtitle::new(4000, 6500, "World!"),
    ];
    srt::generate(&subtitles, "output.srt").await?;
    Ok(())
}
```

### Detect format

```rust
use subtitler::detect_format;
use subtitler::model::Format;

let data = std::fs::read("unknown.sub")?;
match detect_format(&data) {
    Some(Format::Srt) => println!("SRT detected"),
    Some(Format::Vtt) => println!("WebVTT detected"),
    Some(Format::Ass) => println!("ASS detected"),
    Some(Format::Ssa) => println!("SSA detected"),
    Some(Format::MicroDvd) => println!("MicroDVD detected"),
    Some(Format::SubViewer) => println!("SubViewer detected"),
    None => println!("Unknown format"),
}
```

## Feature flags

All formats are enabled by default. To trim compile size, disable the formats
you don't need:

```toml
[dependencies]
subtitler = { version = "1.0", default-features = false, features = ["srt", "vtt"] }
```

| Flag         | Format           |
|--------------|------------------|
| `srt`        | SubRip (`.srt`)  |
| `vtt`        | WebVTT (`.vtt`)  |
| `ass`        | Advanced SubStation Alpha (`.ass`) |
| `ssa`        | SubStation Alpha (`.ssa`) |
| `microdvd`   | MicroDVD (`.sub`)|
| `subviewer`  | SubViewer        |
| `ttml`       | TTML/IMSC (`.ttml`, `.xml`) |
| `sbv`        | YouTube SBV (`.sbv`) |
| `lrc`        | Lyrics LRC (`.lrc`) |
| `http`       | `parse_url()` via `reqwest` |

See `MIGRATION.md` for upgrading from 0.1.x.

## API Reference

### Data Model

```rust
pub struct Subtitle {
    pub index: Option<usize>,     // subtitle number
    pub start: u64,               // start time in milliseconds
    pub end: u64,                 // end time in milliseconds
    pub text: String,             // subtitle text (stripped of tags)
    pub settings: Option<String>, // VTT cue settings
    pub text_parts: Vec<TextPart>, // structured rich text parts

    // ASS/SSA fields
    pub style: Option<String>,    // style name reference
    pub actor: Option<String>,    // speaker/actor name
    pub layer: Option<i32>,       // z-ordering
    pub margin_l: Option<i32>,    // left margin
    pub margin_r: Option<i32>,    // right margin
    pub margin_v: Option<i32>,    // vertical margin
    pub effect: Option<String>,   // effect name
    pub is_comment: bool,         // comment flag
}

pub struct TextPart {
    pub text: String,
    pub bold: bool,
    pub italic: bool,
    pub underline: bool,
    pub color: Option<String>,
    pub voice: Option<String>,
}

pub struct AssStyle {
    pub name: String,
    pub fontname: String,
    pub fontsize: u32,
    pub primary_color: String,
    pub secondary_color: String,
    // ... 23 fields total
}

pub enum SubtitleFile {
    Srt(Vec<Subtitle>),
    Vtt { header: Option<String>, subtitles: Vec<Subtitle> },
    Ass { info: HashMap<String, String>, styles: Vec<AssStyle>, subtitles: Vec<Subtitle> },
}
```

### SRT Module (`subtitler::srt`)

| Function | Description |
|----------|-------------|
| `parse_file(path)` | Parse SRT from file |
| `parse_bytes(data)` | Parse SRT from bytes |
| `parse_content(content)` | Parse SRT from string |
| `parse_url(url)` | Parse SRT from HTTP URL (requires `http` feature) |
| `generate(subtitles, path)` | Write SRT to file |
| `to_string(subtitles)` | Format subtitles as SRT string |
| `detect_format(data)` | Detect if data is SRT |

### WebVTT Module (`subtitler::vtt`)

| Function | Description |
|----------|-------------|
| `parse_file(path)` | Parse VTT from file |
| `parse_bytes(data)` | Parse VTT from bytes |
| `parse_content(content)` | Parse VTT from string |
| `parse_content_full(content)` | Parse VTT returning header + subtitles |
| `parse_url(url)` | Parse VTT from HTTP URL (requires `http` feature) |
| `generate(subtitles, path)` | Write VTT to file |
| `to_string(subtitles, header)` | Format subtitles as VTT string |
| `detect_format(data)` | Detect if data is VTT |

### ASS Module (`subtitler::ass`)

| Function | Description |
|----------|-------------|
| `parse_content(content)` | Parse ASS/SSA from string, returns `SubtitleFile` |
| `parse_file(path)` | Parse ASS/SSA from file (async) |
| `parse_bytes(data)` | Parse ASS/SSA from byte slice |
| `parse_url(url)` | Parse ASS/SSA from HTTP URL (requires `http` feature) |
| `to_string(info, styles, subtitles)` | Format as ASS string |
| `detect_format(data)` | Detect if data is ASS/SSA |

### MicroDVD Module (`subtitler::microdvd`)

| Function | Description |
|----------|-------------|
| `parse_content(content, fps)` | Parse MicroDVD frame-based content |
| `to_string(subtitles, fps)` | Format as MicroDVD string |
| `to_string_with_fps_header(subtitles, fps)` | Format with FPS declaration header |
| `detect_format(data)` | Detect if data is MicroDVD |

### SubViewer Module (`subtitler::subviewer`)

| Function | Description |
|----------|-------------|
| `parse_content(content)` | Parse SubViewer 1.0/2.0 content |
| `to_string(subtitles)` | Format as SubViewer 2.0 with headers |
| `detect_format(data)` | Detect if data is SubViewer |

### Encoding Utilities (`subtitler::encoding`)

| Function | Description |
|----------|-------------|
| `detect_encoding(data)` | Auto-detect character encoding (UTF-8/16/BOM/chardetng) |
| `decode_to_string(data)` | Decode bytes to string using detected encoding |

### Timestamp Utilities (`subtitler::utils`)

| Function | Description |
|----------|-------------|
| `parse_timestamp(ts)` | Parse `"00:00:01,500"``1500` ms |
| `parse_timestamps(ts)` | Parse `"00:00:01,000 --> 00:00:03,500"``Timestamp` |
| `format_timestamp(ms, fmt)` | Format ms to `"00:00:01,000"` (SRT) or `"00:00:01.000"` (VTT) |
| `pad_left(value, length)` | Zero-pad integer to fixed width |

### Frame Utilities (`subtitler::model`)

| Function | Description |
|----------|-------------|
| `ms_to_frames(ms, fps)` | Convert milliseconds to frame count |
| `frames_to_ms(frames, fps)` | Convert frame count to milliseconds |

### SubtitleFile Methods (`subtitler::model::SubtitleFile`)

| Method | Description |
|--------|-------------|
| `subtitles()` | Get reference to subtitle list |
| `subtitles_mut()` | Get mutable reference |
| `format()` | Get detected format enum |
| `shift_all(offset_ms)` | Shift all timestamps |
| `sort()` | Sort by start time |
| `validate()` | Check for timing issues |
| `validate_extended(max_chars, max_gap, max_cps)` | Extended validation |
| `merge_adjacent(max_gap_ms)` | Merge subtitles within gap threshold |
| `remove_overlaps()` | Fix overlapping subtitles by adjusting start times |
| `split_long(max_chars)` | Split long subtitles at word boundaries |
| `transform_framerate(in_fps, out_fps)` | Rescale timestamps for framerate change |
| `map(fn)` | Transform each subtitle (consuming) |
| `filter(fn)` | Filter subtitles (consuming) |
| `to_string()` | Format to appropriate format string |

### Subtitle Methods (`subtitler::model::Subtitle`)

| Method | Description |
|--------|-------------|
| `new(start, end, text)` | Create new subtitle |
| `shift(offset_ms)` | Shift this subtitle's timing |
| `duration_ms()` | Get duration in milliseconds |
| `chars_per_second()` | Calculate characters-per-second rate |
| `reading_speed_wpm()` | Calculate reading speed in words per minute |
| `strip_tags()` | Remove HTML/ASS formatting tags from subtitle text |

### Validation Issues (`subtitler::model::ValidationIssue`)

| Variant | Description |
|---------|-------------|
| `Overlap` | Two subtitles have overlapping time ranges |
| `NegativeDuration` | End time is before start time |
| `ZeroDuration` | Start and end times are equal |
| `DecreasingStartTime` | Start times are not monotonically increasing |
| `TooLongGap` | Gap between subtitles exceeds threshold |
| `TextTooLong` | Subtitle text exceeds character limit |
| `CpsTooHigh` | Characters-per-second exceeds threshold |

### Format Detection (`subtitler::detect_format`)

```rust
pub fn detect_format(data: &[u8]) -> Option<SubtitleFormat>
```

Auto-detects SRT (by index+timestamp pattern), WebVTT (by `WEBVTT` header), or ASS/SSA (by `[Script Info]` section).

## CLI Usage

### Parse subtitles

```sh
# Parse SRT file and display contents
subtitler parse movie.srt

# Parse with JSON output
subtitler parse movie.vtt --json

# Parse from URL
subtitler parse https://example.com/subtitles.srt

# Parse from stdin
cat movie.srt | subtitler parse -

# Force format
subtitler parse data.txt --format srt
```

### Convert between formats

```sh
# Auto-detect source, infer target from extension
subtitler convert input.srt output.vtt

# Explicit source and target
subtitler convert input.srt output.ass --from srt --to ass

# Convert with time shift
subtitler convert input.srt output.vtt --shift -500

# Pipe to stdout
subtitler convert input.srt -
```

### Validate subtitles

```sh
# Basic timing validation
subtitler validate movie.srt

# Extended validation with custom thresholds
subtitler validate movie.srt --max-chars 42 --max-gap 5000 --max-cps 25

# Basic checks only (no text limits)
subtitler validate movie.srt --basic

# JSON output
subtitler validate movie.srt --json
```

### Edit & transform

```sh
# Sort by time
subtitler edit input.srt --output output.srt --sort

# Shift all timestamps (+500ms delay, -200ms advance)
subtitler edit input.srt --output output.srt --shift 500
subtitler edit input.srt --output output.srt --shift=-200

# Merge adjacent subtitles (gap <= 300ms)
subtitler edit input.srt --output output.srt --merge 300

# Split long subtitles (max 42 chars per line)
subtitler edit input.srt --output output.srt --split 42

# Multiple operations
subtitler edit input.srt --output output.vtt --sort --shift -300 --merge 100

# Framerate conversion
subtitler edit input.srt --output output.srt --transform-fps 23.976 25.0
```

### File info & statistics

```sh
subtitler info movie.srt
# Output:
#   File:         movie.srt
#   Format:       srt
#   Subtitles:    150
#   Time range:   0ms -> 5400000ms
#   Duration:     5400000ms (5400.0s)
#   Avg duration: 2500ms
#   Min duration: 500ms
#   Max duration: 8000ms
#   Total chars:  4500
#   Max CPS:      22.5
#   Timing issues: 0
```

### Detect format

```sh
subtitler detect unknown.sub   # prints: srt, vtt, ass, or ssa
```

## Features

| Feature | Default | Description |
|---------|---------|-------------|
| `http` | Yes | Enable `parse_url()` via `reqwest` |

## Examples

See the [examples](https://github.com/subtitle-rs/subtitler/tree/main/examples) directory for more usage patterns:

- `parse-srt-file` — Parse SRT from file
- `parse-srt-content` — Parse SRT from inline content
- `parse-srt-http` — Parse SRT from URL
- `create-srt-file` — Generate SRT file
- `parse-vtt-file` — Parse VTT from file
- `parse-vtt-content` — Parse VTT from inline content
- `parse-vtt-http` — Parse VTT from URL
- `create-vtt-file` — Generate VTT file
- `parse-ass-content` — Parse ASS from inline content
- `format-convert` — Convert between SRT/VTT/ASS formats
- `utility-ops` — Sort, validate, merge, and split operations
- `frame-conversion` — Frame-based timecode conversion

## License

Apache 2.0