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>
impl<'a> SubtitleHandle<'a>
Sourcepub fn extract(&mut self) -> Result<Vec<SubtitleEvent>, UnbundleError>
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
UnbundleError::NoSubtitleStreamif no subtitle stream exists.UnbundleError::SubtitleDecodeErrorif decoding fails.
§Example
use unbundle::{MediaFile, UnbundleError};
let mut unbundler = MediaFile::open("input.mkv")?;
let entries = unbundler.subtitle().extract()?;
println!("Found {} subtitle entries", entries.len());Sourcepub fn save<P: AsRef<Path>>(
&mut self,
path: P,
format: SubtitleFormat,
) -> Result<(), UnbundleError>
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)?;Sourcepub fn extract_text(
&mut self,
format: SubtitleFormat,
) -> Result<String, UnbundleError>
pub fn extract_text( &mut self, format: SubtitleFormat, ) -> Result<String, UnbundleError>
Sourcepub fn extract_range(
&mut self,
start: Duration,
end: Duration,
) -> Result<Vec<SubtitleEvent>, UnbundleError>
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
UnbundleError::InvalidRangeifstart >= end.- Plus any errors from
extract.
§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());Sourcepub fn save_range<P: AsRef<Path>>(
&mut self,
path: P,
format: SubtitleFormat,
start: Duration,
end: Duration,
) -> Result<(), UnbundleError>
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),
)?;Sourcepub fn extract_text_range(
&mut self,
format: SubtitleFormat,
start: Duration,
end: Duration,
) -> Result<String, UnbundleError>
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.
Sourcepub fn search(
&mut self,
query: &str,
) -> Result<Vec<SubtitleEvent>, UnbundleError>
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);
}Sourcepub fn search_exact(
&mut self,
query: &str,
) -> Result<Vec<SubtitleEvent>, UnbundleError>
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")?;Sourcepub fn extract_bitmaps(
&mut self,
) -> Result<Vec<BitmapSubtitleEvent>, UnbundleError>
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
UnbundleError::NoSubtitleStreamif no subtitle stream exists.UnbundleError::SubtitleDecodeErrorif decoding fails.
§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();
}Sourcepub fn render_at(
&mut self,
timestamp: Duration,
) -> Result<Option<DynamicImage>, UnbundleError>
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.
Sourcepub fn stream_copy<P: AsRef<Path>>(
&mut self,
path: P,
) -> Result<(), UnbundleError>
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
UnbundleError::NoSubtitleStreamif no subtitle stream exists.UnbundleError::StreamCopyErrorif the output container does not support the source codec.
§Example
use unbundle::{MediaFile, UnbundleError};
let mut unbundler = MediaFile::open("input.mkv")?;
unbundler.subtitle().stream_copy("output.srt")?;Sourcepub fn stream_copy_range<P: AsRef<Path>>(
&mut self,
path: P,
start: Duration,
end: Duration,
) -> Result<(), UnbundleError>
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
UnbundleError::InvalidRangeifstart >= end.- Plus any errors from
stream_copy.
§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),
)?;Sourcepub fn stream_copy_with_options<P: AsRef<Path>>(
&mut self,
path: P,
config: &ExtractOptions,
) -> Result<(), UnbundleError>
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.
Sourcepub fn stream_copy_range_with_options<P: AsRef<Path>>(
&mut self,
path: P,
start: Duration,
end: Duration,
config: &ExtractOptions,
) -> Result<(), UnbundleError>
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.
Sourcepub fn stream_copy_to_memory(
&mut self,
container_format: &str,
) -> Result<Vec<u8>, UnbundleError>
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
UnbundleError::NoSubtitleStreamif no subtitle stream exists.UnbundleError::StreamCopyErrorif the container format is invalid or does not support the source codec.
§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());Sourcepub fn stream_copy_range_to_memory(
&mut self,
container_format: &str,
start: Duration,
end: Duration,
) -> Result<Vec<u8>, UnbundleError>
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
UnbundleError::InvalidRangeifstart >= end.- Plus any errors from
stream_copy_to_memory.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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