pub struct Subtitle {
pub id: Option<String>,
pub start_time: i64,
pub end_time: i64,
pub text: String,
pub style: Option<SubtitleStyle>,
pub position: Option<Position>,
pub animations: Vec<Animation>,
}Expand description
A single subtitle cue with timing and content.
Fields§
§id: Option<String>Unique identifier (e.g. sequence number in SRT).
start_time: i64Start time in milliseconds.
end_time: i64End time in milliseconds.
text: StringSubtitle text content.
style: Option<SubtitleStyle>Optional styling override.
position: Option<Position>Position override.
animations: Vec<Animation>Animation effects.
Implementations§
Source§impl Subtitle
impl Subtitle
Sourcepub fn is_active(&self, timestamp_ms: i64) -> bool
pub fn is_active(&self, timestamp_ms: i64) -> bool
Check if this subtitle is active at the given timestamp.
Examples found in repository?
examples/render_subtitle.rs (line 85)
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 // Example SRT subtitle data
9 let srt_data = r"1
1000:00:01,000 --> 00:00:04,000
11Hello, World!
12
132
1400:00:05,000 --> 00:00:08,000
15This is a subtitle example
16with multiple lines.
17
183
1900:00:10,000 --> 00:00:13,000
20<i>Styled subtitle with HTML tags</i>
21";
22
23 // Parse SRT subtitles
24 let subtitles = srt::parse(srt_data.as_bytes())?;
25 println!("Parsed {} subtitles", subtitles.len());
26
27 for (i, subtitle) in subtitles.iter().enumerate() {
28 println!(
29 "Subtitle {}: {}ms - {}ms: {}",
30 i + 1,
31 subtitle.start_time,
32 subtitle.end_time,
33 subtitle.text
34 );
35 }
36
37 // Create a sample video frame
38 let mut frame = VideoFrame::new(PixelFormat::Rgb24, 1920, 1080);
39 frame.allocate();
40
41 // Fill with blue background
42 if let Some(plane) = frame.planes.get_mut(0) {
43 let data_len = plane.data.len();
44 let mut new_data = vec![0u8; data_len];
45 for pixel in new_data.chunks_exact_mut(3) {
46 pixel[0] = 0; // R
47 pixel[1] = 0; // G
48 pixel[2] = 100; // B
49 }
50 plane.data = new_data;
51 }
52
53 // Note: In a real application, you would load a font file
54 // For this example, we'll show how to create the renderer
55 println!("\nTo use the subtitle renderer:");
56 println!("1. Load a font: Font::from_file(\"path/to/font.ttf\")?");
57 println!("2. Configure style with colors, size, outline, etc.");
58 println!("3. Create renderer: SubtitleRenderer::new(font, style)");
59 println!("4. Render: renderer.render_subtitle(&subtitle, &mut frame, timestamp)?");
60
61 // Example style configuration
62 let style = SubtitleStyle::default()
63 .with_font_size(48.0)
64 .with_color(255, 255, 255, 255) // White text
65 .with_outline(OutlineStyle::new(Color::black(), 2.0))
66 .with_position(Position::bottom_center())
67 .with_alignment(Alignment::Center)
68 .with_margins(40, 40, 40, 80);
69
70 println!(
71 "\nStyle configuration: Font size={}, Color=white, Outline=black 2px",
72 style.font_size
73 );
74
75 // Example timestamps
76 let ts1 = Timestamp::new(2000, Rational::new(1, 1000)); // 2 seconds
77 let ts2 = Timestamp::new(6000, Rational::new(1, 1000)); // 6 seconds
78
79 // Show which subtitles would be active at different times
80 for subtitle in &subtitles {
81 #[allow(clippy::cast_possible_truncation)]
82 let ts1_ms = (ts1.to_seconds() * 1000.0).round() as i64;
83 #[allow(clippy::cast_possible_truncation)]
84 let ts2_ms = (ts2.to_seconds() * 1000.0).round() as i64;
85 if subtitle.is_active(ts1_ms) {
86 println!(
87 "\nAt t=2s, active subtitle: '{}'",
88 subtitle.text.lines().next().unwrap_or("")
89 );
90 }
91 if subtitle.is_active(ts2_ms) {
92 println!(
93 "At t=6s, active subtitle: '{}'",
94 subtitle.text.lines().next().unwrap_or("")
95 );
96 }
97 }
98
99 // Example: Write subtitles back to SRT format
100 let output_srt = srt::write(&subtitles)?;
101 println!("\nGenerated SRT output:\n{output_srt}");
102
103 Ok(())
104}Sourcepub fn with_animation(self, animation: Animation) -> Self
pub fn with_animation(self, animation: Animation) -> Self
Add an animation effect.
Sourcepub fn with_position(self, position: Position) -> Self
pub fn with_position(self, position: Position) -> Self
Set position override.
Sourcepub fn with_style(self, style: SubtitleStyle) -> Self
pub fn with_style(self, style: SubtitleStyle) -> Self
Set style override.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Subtitle
impl RefUnwindSafe for Subtitle
impl Send for Subtitle
impl Sync for Subtitle
impl Unpin for Subtitle
impl UnsafeUnpin for Subtitle
impl UnwindSafe for Subtitle
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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>
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 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>
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