Enum SubtitleFile

Source
pub enum SubtitleFile {
    SubRipFile(SrtFile),
    SubStationAlpha(SsaFile),
    VobSubIdxFile(IdxFile),
    VobSubSubFile(VobFile),
    MicroDVDFile(MdvdFile),
}
Expand description

Unified wrapper around the all individual subtitle file types.

Variants§

§

SubRipFile(SrtFile)

.srt file

§

SubStationAlpha(SsaFile)

.ssa/.ass file

§

VobSubIdxFile(IdxFile)

.idx file

§

VobSubSubFile(VobFile)

.sub file (VobSub/binary)

§

MicroDVDFile(MdvdFile)

.sub file (MicroDVD/text)

Implementations§

Source§

impl SubtitleFile

Source

pub fn get_subtitle_entries(&self) -> Result<Vec<SubtitleEntry>>

The subtitle entries can be changed by calling update_subtitle_entries().

Examples found in repository?
examples/example1.rs (line 24)
16fn main() {
17    // your setup goes here
18    let path = PathBuf::from("path/your_example_file.ssa");
19    let file_content: String = read_file(&path); // your own load routine
20
21    // parse the file
22    let format = get_subtitle_format(path.extension(), file_content.as_bytes()).expect("unknown format");
23    let mut subtitle_file = parse_str(format, &file_content, 25.0).expect("parser error");
24    let mut subtitle_entries: Vec<SubtitleEntry> = subtitle_file.get_subtitle_entries().expect("unexpected error");
25
26    // shift all subtitle entries by 1 minute and append "subparse" to each subtitle line
27    for subtitle_entry in &mut subtitle_entries {
28        subtitle_entry.timespan += TimeDelta::from_mins(1);
29
30        // image based subtitles like .idx (VobSub) don't have text, so
31        // a text is optional
32        if let Some(ref mut line_ref) = subtitle_entry.line {
33            line_ref.push_str("subparse");
34        }
35    }
36
37    // update the entries in the subtitle file
38    subtitle_file.update_subtitle_entries(&subtitle_entries).expect("unexpected error");
39
40    // print the corrected file to stdout
41    let data: Vec<u8> = subtitle_file.to_data().expect("unexpected errror");
42    let data_string = String::from_utf8(data).expect("UTF-8 conversion error");
43    println!("{}", data_string);
44}
Source

pub fn update_subtitle_entries(&mut self, i: &[SubtitleEntry]) -> Result<()>

Set the entries from the subtitle entries from the get_subtitle_entries().

The length of the given input slice should always match the length of the vector length from get_subtitle_entries(). This function can not delete/create new entries, but preserves everything else in the file (formatting, authors, …).

If the input entry has entry.line == None, the line will not be overwritten.

Be aware that .idx files cannot save time_spans_ (a subtitle will be shown between two consecutive timepoints/there are no separate starts and ends) - so the timepoint will be set to the start of the corresponding input-timespan.

Examples found in repository?
examples/example1.rs (line 38)
16fn main() {
17    // your setup goes here
18    let path = PathBuf::from("path/your_example_file.ssa");
19    let file_content: String = read_file(&path); // your own load routine
20
21    // parse the file
22    let format = get_subtitle_format(path.extension(), file_content.as_bytes()).expect("unknown format");
23    let mut subtitle_file = parse_str(format, &file_content, 25.0).expect("parser error");
24    let mut subtitle_entries: Vec<SubtitleEntry> = subtitle_file.get_subtitle_entries().expect("unexpected error");
25
26    // shift all subtitle entries by 1 minute and append "subparse" to each subtitle line
27    for subtitle_entry in &mut subtitle_entries {
28        subtitle_entry.timespan += TimeDelta::from_mins(1);
29
30        // image based subtitles like .idx (VobSub) don't have text, so
31        // a text is optional
32        if let Some(ref mut line_ref) = subtitle_entry.line {
33            line_ref.push_str("subparse");
34        }
35    }
36
37    // update the entries in the subtitle file
38    subtitle_file.update_subtitle_entries(&subtitle_entries).expect("unexpected error");
39
40    // print the corrected file to stdout
41    let data: Vec<u8> = subtitle_file.to_data().expect("unexpected errror");
42    let data_string = String::from_utf8(data).expect("UTF-8 conversion error");
43    println!("{}", data_string);
44}
Source

pub fn to_data(&self) -> Result<Vec<u8>>

Returns a byte-stream in the respective format (.ssa, .srt, etc.) with the (probably) altered information.

Examples found in repository?
examples/example1.rs (line 41)
16fn main() {
17    // your setup goes here
18    let path = PathBuf::from("path/your_example_file.ssa");
19    let file_content: String = read_file(&path); // your own load routine
20
21    // parse the file
22    let format = get_subtitle_format(path.extension(), file_content.as_bytes()).expect("unknown format");
23    let mut subtitle_file = parse_str(format, &file_content, 25.0).expect("parser error");
24    let mut subtitle_entries: Vec<SubtitleEntry> = subtitle_file.get_subtitle_entries().expect("unexpected error");
25
26    // shift all subtitle entries by 1 minute and append "subparse" to each subtitle line
27    for subtitle_entry in &mut subtitle_entries {
28        subtitle_entry.timespan += TimeDelta::from_mins(1);
29
30        // image based subtitles like .idx (VobSub) don't have text, so
31        // a text is optional
32        if let Some(ref mut line_ref) = subtitle_entry.line {
33            line_ref.push_str("subparse");
34        }
35    }
36
37    // update the entries in the subtitle file
38    subtitle_file.update_subtitle_entries(&subtitle_entries).expect("unexpected error");
39
40    // print the corrected file to stdout
41    let data: Vec<u8> = subtitle_file.to_data().expect("unexpected errror");
42    let data_string = String::from_utf8(data).expect("UTF-8 conversion error");
43    println!("{}", data_string);
44}

Trait Implementations§

Source§

impl Clone for SubtitleFile

Source§

fn clone(&self) -> SubtitleFile

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SubtitleFile

Source§

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

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

impl From<IdxFile> for SubtitleFile

Source§

fn from(f: IdxFile) -> SubtitleFile

Converts to this type from the input type.
Source§

impl From<MdvdFile> for SubtitleFile

Source§

fn from(f: MdvdFile) -> SubtitleFile

Converts to this type from the input type.
Source§

impl From<SrtFile> for SubtitleFile

Source§

fn from(f: SrtFile) -> SubtitleFile

Converts to this type from the input type.
Source§

impl From<SsaFile> for SubtitleFile

Source§

fn from(f: SsaFile) -> SubtitleFile

Converts to this type from the input type.
Source§

impl From<VobFile> for SubtitleFile

Source§

fn from(f: VobFile) -> SubtitleFile

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

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>,

Source§

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.