[][src]Crate srtlib

A simple library for handling .srt subtitle files.

This library allows you to handle subtitle files as collections of multiple subtitle structs, letting you modify the subtitles without directly messing with the .srt files.

Subtitle collections can be generated by parsing strings and files, but also from the ground up, enabling total control of all the elements of each subtitle.


use srtlib::Subtitles;

// Parse subtitles from file that uses the utf-8 encoding.
let mut subs = Subtitles::parse_from_file("subtitles.srt", None).unwrap();

// Move every subtitle 10 seconds forward in time.
for s in &mut subs {

// Write subtitles back to the same .srt file.
subs.write_to_file("subtitles.srt", None).unwrap();
use srtlib::{Timestamp, Subtitle, Subtitles};

// Construct a new, empty Subtitles collection.
let mut subs = Subtitles::new();

// Construct a new subtitle.
let one = Subtitle::new(1, Timestamp::new(0, 0, 0, 0), Timestamp::new(0, 0, 2, 0), "Hello world!".to_string());

// Add subtitle at the end of the subs collection.

// Construct a new subtitle by parsing a string.
let two = Subtitle::parse("2\n00:00:02,500 --> 00:00:05,000\nThis is a subtitle.".to_string()).unwrap();

// Add subtitle at the end of the subs collection.

// Write the subtitles to a .srt file.
subs.write_to_file("test.srt", None).unwrap();
use std::fmt::Write;
use srtlib::Subtitles;

// Parse subtitles from a string and convert to vector.
let mut subs = Subtitles::parse_from_str("3\n00:00:05,000 --> 00:00:07,200\nFoobar\n\n\
                                          1\n00:00:00,000 --> 00:00:02,400\nHello\n\n\
                                          2\n00:00:03,000 --> 00:00:05,000\nWorld\n\n".to_string()

// Sort the subtitles.

// Collect all subtitle text into a string.
let mut res = String::new();
for s in subs {
    write!(&mut res, "{}\n", s.text).unwrap();

assert_eq!(res, "Hello\nWorld\nFoobar\n".to_string());



A single subtitle.


A collection of Subtitle structs.


A simple timestamp following the timecode format hours:minutes:seconds,milliseconds.



The error type returned by any function that parses strings or files.