Trait aspasia::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)
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
fn main() -> Result<(), Error> {
    // We can directly specify the format to open a subtitle file
    let vtt = WebVttSubtitle::from_path("/path/to/some.vtt")?;

    // and then directly work with its data
    println!("{}", vtt.header().cloned().unwrap_or_default());

    // or we could use the more general interface to open (timed) subtitle files
    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;

    // Move the underlying data out in order to access format-specific properties
    // Note that if the format doesn't match, this will perform a conversion instead of just moving the data
    let mut srt = SubRipSubtitle::from(sub);

    // Now we can access format-specific methods like SubRipSubtitle::renumber()
    srt.renumber();

    // Access and modify events
    for event in srt.events_mut() {
        event.shift(600.into());
    }

    // Write the modified subtitle to file
    srt.export("/path/to/output.srt")?;

    Ok(())
}
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)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() -> Result<(), Error> {
    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;

    // Get the file as its specific format
    let srt = SubRipSubtitle::from(sub);

    // You can use into() to convert the file
    let vtt: WebVttSubtitle = srt.into();

    // or from()
    let ass = AssSubtitle::from(vtt);

    ass.export("/path/to/converted.ass")?;

    Ok(())
}
More examples
Hide additional examples
examples/modification.rs (line 17)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() -> Result<(), Error> {
    let sub = TimedSubtitleFile::new("/path/to/subtitle.ass")?;
    let mut ass = AssSubtitle::from(sub);

    println!("{}", ass.script_info());

    for event in ass.events_mut() {
        event.style = Some("Karaoke".to_string());

        if event.duration() > TimeDelta::from(500) {
            event.shift(2000.into());
        }
    }

    ass.export("/path/to/output.ass")
}
examples/basic.rs (line 26)
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
fn main() -> Result<(), Error> {
    // We can directly specify the format to open a subtitle file
    let vtt = WebVttSubtitle::from_path("/path/to/some.vtt")?;

    // and then directly work with its data
    println!("{}", vtt.header().cloned().unwrap_or_default());

    // or we could use the more general interface to open (timed) subtitle files
    let sub = TimedSubtitleFile::new("/path/to/file.srt")?;

    // Move the underlying data out in order to access format-specific properties
    // Note that if the format doesn't match, this will perform a conversion instead of just moving the data
    let mut srt = SubRipSubtitle::from(sub);

    // Now we can access format-specific methods like SubRipSubtitle::renumber()
    srt.renumber();

    // Access and modify events
    for event in srt.events_mut() {
        event.shift(600.into());
    }

    // Write the modified subtitle to file
    srt.export("/path/to/output.srt")?;

    Ok(())
}

Object Safety§

This trait is not object safe.

Implementors§