skrt
A fast, zero-copy parser and serializer for SubRip Text (SRT) subtitle files in Rust.
Features
- Zero-copy parsing - Subtitle text borrows directly from the input string when possible
- Flexible input handling - Supports LF and CRLF line endings, optional UTF-8 BOM, trailing whitespace, and missing final newlines
- Shift timestamps forward or backward with millisecond precision
- Create subtitle files programmatically
- Detailed error reporting
Installation
Add skrt to your Cargo.toml:
[]
= "0.1"
Usage
Parsing an SRT file
use Srt;
let data = r#"1
00:00:01,000 --> 00:00:04,000
Hello, world!
2
00:00:05,000 --> 00:00:08,000
This is a subtitle.
"#;
let srt = try_parse.unwrap;
for subtitle in srt.iter
Building subtitles programmatically
use ;
let mut srt = new;
srt.add_subtitle;
srt.add_subtitle;
let output = srt.serialize;
println!;
Shifting timestamps
use Srt;
let data = "1\n00:00:01,000 --> 00:00:04,000\nHello\n\n";
let mut srt = try_parse.unwrap;
// Shift all subtitles forward by 5 seconds
for sub in srt.iter_mut
println!;
Working with timestamps
use Timestamp;
let ts = from_millis.unwrap; // 1h 23m 45s
assert_eq!;
assert_eq!;
// Timestamps are comparable
let ts2 = from_millis.unwrap;
assert!;
Format Details
SRT files consist of subtitle blocks separated by blank lines. Each block contains:
- A sequence number (positive integer)
- A timestamp line: HH:MM:SS,mmm --> HH:MM:SS,mmm
- One or more lines of text
This crate handles common real-world variations:
- Both LF (\n) and CRLF (\r\n) line endings
- Optional UTF-8 BOM at the start of the file
- Trailing whitespace after timestamp lines
- Files that don't end with a trailing blank line
Development
Fuzz Testing
Install cargo-fuzz with cargo +nightly install cargo-fuzz, then cd fuzz && cargo +nightly fuzz run -j $(grep -c processor /proc/cpuinfo) parse_srt.