pub enum TimedSubtitleFile {
    Ass(AssSubtitle),
    MicroDvd(TimedMicroDvdSubtitle),
    Ssa(SsaSubtitle),
    SubRip(SubRipSubtitle),
    WebVtt(WebVttSubtitle),
}
Expand description

Convenience interface for interacting with time-based subtitle files in a generic manner.

For example, managing a collection of multiple subtitles of unknown or different formats.

For accessing or modifying format-specific data/methods, such as embedded fonts in SubStation Alpha files, you should convert to the format-specific types using from() or into()

Variants§

§

Ass(AssSubtitle)

File in Advanced SubStation Alpha V4+ (.ass) format

§

MicroDvd(TimedMicroDvdSubtitle)

Timed version of file in MicroDVD (.sub) format

§

Ssa(SsaSubtitle)

File in Substation Alpha V4 (.ssa) format

§

SubRip(SubRipSubtitle)

File in SubRip (.srt) format

§

WebVtt(WebVttSubtitle)

File in WebVTT (.vtt) format

Implementations§

source§

impl TimedSubtitleFile

source

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

Automatically attempts to detect format using the file extension and file contents.

Using the detected format, try to parse the given path and load its data

Errors
Examples found in repository?
examples/conversion.rs (line 4)
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 4)
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 11)
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

pub fn with_format( path: impl AsRef<Path>, format: Format ) -> Result<Self, Error>

Try to load and parse file as the given format

Errors

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

source

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

Exports contents to file in the corresponding format

Errors

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

Trait Implementations§

source§

impl Debug for TimedSubtitleFile

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl From<TimedSubtitleFile> for AssSubtitle

source§

fn from(value: TimedSubtitleFile) -> Self

Converts to this type from the input type.
source§

impl From<TimedSubtitleFile> for PlainSubtitle

source§

fn from(value: TimedSubtitleFile) -> Self

Converts to this type from the input type.
source§

impl From<TimedSubtitleFile> for SsaSubtitle

source§

fn from(value: TimedSubtitleFile) -> Self

Converts to this type from the input type.
source§

impl From<TimedSubtitleFile> for SubRipSubtitle

source§

fn from(value: TimedSubtitleFile) -> Self

Converts to this type from the input type.
source§

impl From<TimedSubtitleFile> for WebVttSubtitle

source§

fn from(value: TimedSubtitleFile) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.