render_subtitle/
render_subtitle.rs1use oximedia_codec::VideoFrame;
4use oximedia_core::{PixelFormat, Rational, Timestamp};
5use oximedia_subtitle::{parser::srt, Alignment, Color, OutlineStyle, Position, SubtitleStyle};
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8 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 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 let mut frame = VideoFrame::new(PixelFormat::Rgb24, 1920, 1080);
39 frame.allocate();
40
41 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; pixel[1] = 0; pixel[2] = 100; }
50 plane.data = new_data;
51 }
52
53 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 let style = SubtitleStyle::default()
63 .with_font_size(48.0)
64 .with_color(255, 255, 255, 255) .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 let ts1 = Timestamp::new(2000, Rational::new(1, 1000)); let ts2 = Timestamp::new(6000, Rational::new(1, 1000)); 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 let output_srt = srt::write(&subtitles)?;
101 println!("\nGenerated SRT output:\n{output_srt}");
102
103 Ok(())
104}