pub struct CaptionTrack { /* private fields */ }Expand description
An immutable, bounded caption timeline with handwritten SRT interchange.
Build or parse outside frame callbacks. Sampling scans at most 16,384 cues, preserving input order and overlaps without sorting or previous-frame state. Layer construction and overlapping-label placement belong to the caller.
use motion_content::captions::*;
use motion_core::prelude::*;
let captions = CaptionTrack::from_srt("1\n00:00:01,000 --> 00:00:03,000\nLight takes time.\n")?;
let time = Time::from_frame(30, fps!(30));
assert_eq!(
captions.active_at_time(time).next().unwrap().text(),
"Light takes time."
);
assert!(captions.to_srt()?.starts_with("1\n00:00:01,000"));Implementations§
Source§impl CaptionTrack
impl CaptionTrack
Sourcepub fn new(
cues: impl IntoIterator<Item = CaptionCue>,
) -> Result<Self, ValidationError>
pub fn new( cues: impl IntoIterator<Item = CaptionCue>, ) -> Result<Self, ValidationError>
Keeps input order, including overlaps and unsorted times; empty input is valid. Rejects more than 16,384 cues or over 8 MiB of aggregate UTF-8 text. Cloning a track clones its text; it does not share mutable playback state.
Sourcepub fn cues(&self) -> &[CaptionCue]
pub fn cues(&self) -> &[CaptionCue]
Returns immutable cues in their original input order.
Sourcepub fn active_at(&self, time: Duration) -> impl Iterator<Item = &CaptionCue>
pub fn active_at(&self, time: Duration) -> impl Iterator<Item = &CaptionCue>
Samples an explicit time in the same half-open interval and input order.
Sourcepub fn active_at_time(&self, time: Time) -> impl Iterator<Item = &CaptionCue>
pub fn active_at_time(&self, time: Time) -> impl Iterator<Item = &CaptionCue>
Samples signed ticks exactly, without rounding through nanoseconds. Negative times contain no captions. Cue intervals remain half-open.
Sourcepub fn from_srt(source: &str) -> Result<Self, ValidationError>
pub fn from_srt(source: &str) -> Result<Self, ValidationError>
Parses at most 8 MiB of UTF-8 SRT, with optional leading BOM and LF/CRLF.
Requires unsigned u64 cue identifiers and HH:MM:SS,mmm --> HH:MM:SS,mmm;
hours have at least two digits, minutes/seconds are below 60. Identifiers
need not be consecutive or unique. Blank lines separate cues; surrounding
blank lines and horizontal whitespace around identifiers/arrows are allowed.
Multiline bodies and literal markup are preserved; timing/layout extensions
are rejected. Syntax/limits return InvalidData with a one-based line number.
Empty input is a valid empty track. No files are read during sampling.
Sourcepub fn to_srt(&self) -> Result<String, ValidationError>
pub fn to_srt(&self) -> Result<String, ValidationError>
Serializes canonical SRT with sequential one-based identifiers and LF lines. Preserves cue order/text/times; does not sort or retain original identifiers. Rejects sub-millisecond times rather than rounding, and output over 8 MiB. No file writes, subtitle muxing or audio mixing are performed.