Skip to content

Commit

Permalink
vobsub: wip - replace subtitles with VobsubParser
Browse files Browse the repository at this point in the history
  • Loading branch information
gwen-lg committed Mar 28, 2024
1 parent 2d96239 commit 4ca9b07
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/vobsub/idx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ impl Index {

/// Iterate over the subtitles associated with this `*.idx` file.
#[must_use]
pub fn subtitles(&self) -> sub::Subtitles {
sub::subtitles(&self.sub_data)
pub fn subtitles(&self) -> sub::VobsubParser {
sub::VobsubParser::new(&self.sub_data)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/vobsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ mod sub;
pub use self::idx::{read_palette, Index};
pub use self::palette::{palette, Palette};
pub use self::probe::{is_idx_file, is_sub_file};
pub use self::sub::{subtitles, Coordinates, Subtitle, Subtitles};
pub use self::sub::{Coordinates, Subtitle, VobsubParser};
63 changes: 30 additions & 33 deletions src/vobsub/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,38 @@ macro_rules! try_iter {
/// An internal iterator over subtitles. These subtitles may not have a
/// valid `end_time`, so we'll try to fix them up before letting the user
/// see them.
struct SubtitlesInternal<'a> {
pub struct VobsubParser<'a> {
pes_packets: ps::PesPackets<'a>,
}

impl<'a> Iterator for SubtitlesInternal<'a> {
impl<'a> VobsubParser<'a> {
/// To parse a `vobsub` (.sub) file content.
/// Return an iterator over the subtitles in this data stream.
#[must_use]
pub fn new(input: &'a [u8]) -> Self {
Self {
pes_packets: ps::pes_packets(input),
}
}

// /// Create a `vobsub` parser from a file.
// #[profiling::function]
// pub fn from_file<P>(path: P) -> Result<Self, SubError>
// where
// P: AsRef<Path>,
// {
// let path = path.as_ref();
// let sup_file = fs::File::open(path).map_err(|source| SubError::Io {
// source,
// path: path.into(),
// })?;

// let reader = BufReader::new(sup_file);
// Ok(VobsubParser::new(reader))
// }
}

impl<'a> Iterator for VobsubParser<'a> {
type Item = Result<Subtitle, SubError>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -581,36 +608,6 @@ impl<'a> Iterator for SubtitlesInternal<'a> {
}
}

/// An iterator over subtitles.
pub struct Subtitles<'a> {
internal: SubtitlesInternal<'a>,
}

impl<'a> Iterator for Subtitles<'a> {
type Item = Result<Subtitle, SubError>;

// This whole routine exists to make sure that `end_time` is set to a
// useful value even if the subtitles themselves didn't supply one.
// I'm not even sure this is valid, but it has been observed in the
// wild.
fn next(&mut self) -> Option<Self::Item> {
profiling::scope!("Subtitles next");

// Attempt to fetch a new subtitle.
self.internal.next()
}
}

/// Return an iterator over the subtitles in this data stream.
#[must_use]
pub fn subtitles(input: &[u8]) -> Subtitles {
Subtitles {
internal: SubtitlesInternal {
pes_packets: ps::pes_packets(input),
},
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -689,7 +686,7 @@ mod tests {
let mut f = fs::File::open("./fixtures/example.sub").unwrap();
let mut buffer = vec![];
f.read_to_end(&mut buffer).unwrap();
let mut subs = subtitles(&buffer);
let mut subs = VobsubParser::new(&buffer);
let sub1 = subs.next().expect("missing sub 1").unwrap();
assert!(sub1.start_time - 49.4 < 0.1);
assert!(sub1.end_time.unwrap() - 50.9 < 0.1);
Expand Down

0 comments on commit 4ca9b07

Please sign in to comment.