use oximedia_codec::VideoFrame;
use oximedia_core::{PixelFormat, Rational, Timestamp};
use oximedia_subtitle::{parser::srt, Alignment, Color, OutlineStyle, Position, SubtitleStyle};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let srt_data = r"1
00:00:01,000 --> 00:00:04,000
Hello, World!
2
00:00:05,000 --> 00:00:08,000
This is a subtitle example
with multiple lines.
3
00:00:10,000 --> 00:00:13,000
<i>Styled subtitle with HTML tags</i>
";
let subtitles = srt::parse(srt_data.as_bytes())?;
println!("Parsed {} subtitles", subtitles.len());
for (i, subtitle) in subtitles.iter().enumerate() {
println!(
"Subtitle {}: {}ms - {}ms: {}",
i + 1,
subtitle.start_time,
subtitle.end_time,
subtitle.text
);
}
let mut frame = VideoFrame::new(PixelFormat::Rgb24, 1920, 1080);
frame.allocate();
if let Some(plane) = frame.planes.get_mut(0) {
let data_len = plane.data.len();
let mut new_data = vec![0u8; data_len];
for pixel in new_data.chunks_exact_mut(3) {
pixel[0] = 0; pixel[1] = 0; pixel[2] = 100; }
plane.data = new_data;
}
println!("\nTo use the subtitle renderer:");
println!("1. Load a font: Font::from_file(\"path/to/font.ttf\")?");
println!("2. Configure style with colors, size, outline, etc.");
println!("3. Create renderer: SubtitleRenderer::new(font, style)");
println!("4. Render: renderer.render_subtitle(&subtitle, &mut frame, timestamp)?");
let style = SubtitleStyle::default()
.with_font_size(48.0)
.with_color(255, 255, 255, 255) .with_outline(OutlineStyle::new(Color::black(), 2.0))
.with_position(Position::bottom_center())
.with_alignment(Alignment::Center)
.with_margins(40, 40, 40, 80);
println!(
"\nStyle configuration: Font size={}, Color=white, Outline=black 2px",
style.font_size
);
let ts1 = Timestamp::new(2000, Rational::new(1, 1000)); let ts2 = Timestamp::new(6000, Rational::new(1, 1000));
for subtitle in &subtitles {
#[allow(clippy::cast_possible_truncation)]
let ts1_ms = (ts1.to_seconds() * 1000.0).round() as i64;
#[allow(clippy::cast_possible_truncation)]
let ts2_ms = (ts2.to_seconds() * 1000.0).round() as i64;
if subtitle.is_active(ts1_ms) {
println!(
"\nAt t=2s, active subtitle: '{}'",
subtitle.text.lines().next().unwrap_or("")
);
}
if subtitle.is_active(ts2_ms) {
println!(
"At t=6s, active subtitle: '{}'",
subtitle.text.lines().next().unwrap_or("")
);
}
}
let output_srt = srt::write(&subtitles)?;
println!("\nGenerated SRT output:\n{output_srt}");
Ok(())
}