Skip to main content

SubtitleHandle

Struct SubtitleHandle 

Source
pub struct SubtitleHandle<'a> { /* private fields */ }
Expand description

Subtitle extraction operations.

Obtained via MediaFile::subtitle or MediaFile::subtitle_track. Extracts text-based subtitle events from the media file.

Implementations§

Source§

impl<'a> SubtitleHandle<'a>

Source

pub fn extract(&mut self) -> Result<Vec<SubtitleEvent>, UnbundleError>

Extract all subtitle entries from the stream.

Returns a list of SubtitleEvent values sorted by start time.

§Errors
§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let entries = unbundler.subtitle().extract()?;
println!("Found {} subtitle entries", entries.len());
Source

pub fn save<P: AsRef<Path>>( &mut self, path: P, format: SubtitleFormat, ) -> Result<(), UnbundleError>

Extract subtitles and save them to a file.

Extracts all subtitle entries and writes them in the specified format.

§Errors

Returns errors from extract or I/O errors when writing the file.

§Example
use unbundle::{MediaFile, SubtitleFormat, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
unbundler.subtitle().save("subtitles.srt", SubtitleFormat::Srt)?;
Source

pub fn extract_text( &mut self, format: SubtitleFormat, ) -> Result<String, UnbundleError>

Extract subtitles and format them as a string.

Convenience method that returns the formatted subtitle text without writing to a file.

§Errors

Returns errors from extract.

Source

pub fn extract_range( &mut self, start: Duration, end: Duration, ) -> Result<Vec<SubtitleEvent>, UnbundleError>

Extract subtitle entries within a time range.

Returns only the SubtitleEvent values whose display interval overlaps [start, end). A subtitle is included when its start time is before end and its end time is after start.

§Errors
§Example
use std::time::Duration;
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let subs = unbundler
    .subtitle()
    .extract_range(Duration::from_secs(10), Duration::from_secs(30))?;
println!("Found {} subtitles in range", subs.len());
Source

pub fn save_range<P: AsRef<Path>>( &mut self, path: P, format: SubtitleFormat, start: Duration, end: Duration, ) -> Result<(), UnbundleError>

Extract subtitles in a time range and save to a file.

Combines extract_range with file output in the specified format.

§Errors

Returns errors from extract_range or I/O errors when writing the file.

§Example
use std::time::Duration;
use unbundle::{MediaFile, SubtitleFormat, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
unbundler.subtitle().save_range(
    "partial.srt",
    SubtitleFormat::Srt,
    Duration::from_secs(0),
    Duration::from_secs(60),
)?;
Source

pub fn extract_text_range( &mut self, format: SubtitleFormat, start: Duration, end: Duration, ) -> Result<String, UnbundleError>

Extract subtitles in a time range and format as a string.

Combines extract_range with text formatting.

§Errors

Returns errors from extract_range.

Source

pub fn search( &mut self, query: &str, ) -> Result<Vec<SubtitleEvent>, UnbundleError>

Search subtitle entries for text matching a pattern (case-insensitive).

Returns all SubtitleEvent values whose text contains query (compared case-insensitively).

§Errors

Returns errors from extract.

§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let results = unbundler.subtitle().search("hello")?;
for sub in &results {
    println!("[{:?}] {}", sub.start_time, sub.text);
}
Source

pub fn search_exact( &mut self, query: &str, ) -> Result<Vec<SubtitleEvent>, UnbundleError>

Search subtitle entries for an exact text match (case-sensitive).

Returns all SubtitleEvent values whose text contains query exactly (case-sensitive comparison).

§Errors

Returns errors from extract.

§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let results = unbundler.subtitle().search_exact("Hello")?;
Source

pub fn extract_bitmaps( &mut self, ) -> Result<Vec<BitmapSubtitleEvent>, UnbundleError>

Extract bitmap subtitle events as images.

DVD, PGS, and DVB subtitle tracks use images rather than text. This method decodes those bitmap rects and converts each one into a BitmapSubtitleEvent containing an image::DynamicImage along with timing and positional metadata.

Text-only subtitle rects are silently skipped.

§Errors
§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let bitmaps = unbundler.subtitle().extract_bitmaps()?;
for (i, bmp) in bitmaps.iter().enumerate() {
    bmp.image.save(format!("sub_{i}.png")).unwrap();
}
Source

pub fn render_at( &mut self, timestamp: Duration, ) -> Result<Option<DynamicImage>, UnbundleError>

Render active bitmap subtitle events at a timestamp into one image.

This is intended for image-based subtitle codecs (for example PGS, DVB, DVD subtitles). Text subtitle tracks return Ok(None) because they have no bitmap payload.

When multiple bitmap events overlap at timestamp, they are composited in extraction order onto a transparent RGBA canvas.

Source

pub fn stream_copy<P: AsRef<Path>>( &mut self, path: P, ) -> Result<(), UnbundleError>

Copy the subtitle stream verbatim to a file without re-encoding.

Unlike save which decodes subtitles and re-formats them as SRT/WebVTT, this copies packets directly from the input, preserving the original codec and timing. The output container format is inferred from the file extension.

This is equivalent to ffmpeg -i input.mkv -vn -an -c:s copy output.srt.

§Errors
§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
unbundler.subtitle().stream_copy("output.srt")?;
Source

pub fn stream_copy_range<P: AsRef<Path>>( &mut self, path: P, start: Duration, end: Duration, ) -> Result<(), UnbundleError>

Copy a subtitle segment verbatim to a file without re-encoding.

Like stream_copy but copies only packets between start and end. Because there is no re-encoding, the actual boundaries are aligned to the nearest packet.

§Errors
§Example
use std::time::Duration;

use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
unbundler.subtitle().stream_copy_range(
    "segment.srt",
    Duration::from_secs(10),
    Duration::from_secs(60),
)?;
Source

pub fn stream_copy_with_options<P: AsRef<Path>>( &mut self, path: P, config: &ExtractOptions, ) -> Result<(), UnbundleError>

Copy the subtitle stream verbatim to a file with cancellation support.

Like stream_copy but accepts an ExtractOptions for cancellation.

§Errors

Returns UnbundleError::Cancelled if cancellation is requested, or any error from stream_copy.

Source

pub fn stream_copy_range_with_options<P: AsRef<Path>>( &mut self, path: P, start: Duration, end: Duration, config: &ExtractOptions, ) -> Result<(), UnbundleError>

Copy a subtitle segment verbatim to a file with cancellation support.

Like stream_copy_range but accepts an ExtractOptions.

Source

pub fn stream_copy_to_memory( &mut self, container_format: &str, ) -> Result<Vec<u8>, UnbundleError>

Copy the subtitle stream verbatim to memory without re-encoding.

container_format is the FFmpeg short name for the output container (e.g. "matroska" for MKV, "srt" for SubRip).

§Errors
§Example
use unbundle::{MediaFile, UnbundleError};

let mut unbundler = MediaFile::open("input.mkv")?;
let bytes = unbundler.subtitle().stream_copy_to_memory("srt")?;
println!("Copied {} bytes", bytes.len());
Source

pub fn stream_copy_range_to_memory( &mut self, container_format: &str, start: Duration, end: Duration, ) -> Result<Vec<u8>, UnbundleError>

Copy a subtitle segment verbatim to memory without re-encoding.

Like stream_copy_to_memory but copies only packets between start and end.

§Errors

Auto Trait Implementations§

§

impl<'a> Freeze for SubtitleHandle<'a>

§

impl<'a> RefUnwindSafe for SubtitleHandle<'a>

§

impl<'a> Send for SubtitleHandle<'a>

§

impl<'a> !Sync for SubtitleHandle<'a>

§

impl<'a> Unpin for SubtitleHandle<'a>

§

impl<'a> UnsafeUnpin for SubtitleHandle<'a>

§

impl<'a> !UnwindSafe for SubtitleHandle<'a>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.