Trait Subtitle

Source
pub trait Subtitle: Display + FromStr {
    type Event;

    // Required methods
    fn from_path_with_encoding(
        path: impl AsRef<Path>,
        encoding: Option<&'static Encoding>,
    ) -> Result<Self, Error>;
    fn events(&self) -> &[Self::Event];
    fn events_mut(&mut self) -> &mut [Self::Event];

    // Provided methods
    fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> { ... }
    fn event(&self, index: usize) -> Option<&Self::Event> { ... }
    fn event_mut(&mut self, index: usize) -> Option<&mut Self::Event> { ... }
    fn export(&self, path: impl AsRef<Path>) -> Result<(), Error> { ... }
}
Expand description

Base trait for all subtitle implementations.

Required Associated Types§

Source

type Event

Event type for the given subtitle format

Required Methods§

Source

fn from_path_with_encoding( path: impl AsRef<Path>, encoding: Option<&'static Encoding>, ) -> Result<Self, Error>

Load subtitle format from path using the given encoding

§Errors

If an error is encountered while opening the file, returns Error::FileIoError

Source

fn events(&self) -> &[Self::Event]

Get list of events as a slice

Source

fn events_mut(&mut self) -> &mut [Self::Event]

Get list of events as a mutable slice

Provided Methods§

Source

fn from_path(path: impl AsRef<Path>) -> Result<Self, Error>

Load subtitle from given path. Automatically attempts to detect the encoding to use from the file contents.

§Errors

If an error is encountered while opening the file, returns Error::FileIoError

Examples found in repository?
examples/basic.rs (line 5)
3fn main() -> Result<(), Error> {
4    // We can directly specify the format to open a subtitle file
5    let vtt = WebVttSubtitle::from_path("/path/to/some.vtt")?;
6
7    // and then directly work with its data
8    println!("{}", vtt.header().cloned().unwrap_or_default());
9
10    // or we could use the more general interface to open (timed) subtitle files
11    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;
12
13    // Move the underlying data out in order to access format-specific properties
14    // Note that if the format doesn't match, this will perform a conversion instead of just moving the data
15    let mut srt = SubRipSubtitle::from(sub);
16
17    // Now we can access format-specific methods like SubRipSubtitle::renumber()
18    srt.renumber();
19
20    // Access and modify events
21    for event in srt.events_mut() {
22        event.shift(600.into());
23    }
24
25    // Write the modified subtitle to file
26    srt.export("/path/to/output.srt")?;
27
28    Ok(())
29}
Source

fn event(&self, index: usize) -> Option<&Self::Event>

Try to get event at given index

Source

fn event_mut(&mut self, index: usize) -> Option<&mut Self::Event>

Try to get mutable event at given index

Source

fn export(&self, path: impl AsRef<Path>) -> Result<(), Error>

Write subtitles to file at the given path

§Errors

Returns Error::FileIoError if method fails to create file at the specified path

Examples found in repository?
examples/conversion.rs (line 15)
3fn main() -> Result<(), Error> {
4    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;
5
6    // Get the file as its specific format
7    let srt = SubRipSubtitle::from(sub);
8
9    // You can use into() to convert the file
10    let vtt: WebVttSubtitle = srt.into();
11
12    // or from()
13    let ass = AssSubtitle::from(vtt);
14
15    ass.export("/path/to/converted.ass")?;
16
17    Ok(())
18}
More examples
Hide additional examples
examples/modification.rs (line 17)
3fn main() -> Result<(), Error> {
4    let sub = TimedSubtitleFile::new("/path/to/subtitle.ass")?;
5    let mut ass = AssSubtitle::from(sub);
6
7    println!("{}", ass.script_info());
8
9    for event in ass.events_mut() {
10        event.style = Some("Karaoke".to_string());
11
12        if event.duration() > TimeDelta::from(500) {
13            event.shift(2000.into());
14        }
15    }
16
17    ass.export("/path/to/output.ass")
18}
examples/basic.rs (line 26)
3fn main() -> Result<(), Error> {
4    // We can directly specify the format to open a subtitle file
5    let vtt = WebVttSubtitle::from_path("/path/to/some.vtt")?;
6
7    // and then directly work with its data
8    println!("{}", vtt.header().cloned().unwrap_or_default());
9
10    // or we could use the more general interface to open (timed) subtitle files
11    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;
12
13    // Move the underlying data out in order to access format-specific properties
14    // Note that if the format doesn't match, this will perform a conversion instead of just moving the data
15    let mut srt = SubRipSubtitle::from(sub);
16
17    // Now we can access format-specific methods like SubRipSubtitle::renumber()
18    srt.renumber();
19
20    // Access and modify events
21    for event in srt.events_mut() {
22        event.shift(600.into());
23    }
24
25    // Write the modified subtitle to file
26    srt.export("/path/to/output.srt")?;
27
28    Ok(())
29}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§