Skip to main content

simple_video/
simple_video.rs

1// Example: Convert a video to ASCII frames using cascii as a library
2// Run with: cargo run --example simple_video
3
4use cascii::{AsciiConverter, ConversionOptions, VideoOptions};
5use std::path::Path;
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    // Create a converter
9    let converter = AsciiConverter::new();
10
11    // Configure video options
12    let video_opts = VideoOptions {
13        fps: 10,
14        start: Some("0".to_string()),
15        end: Some("2".to_string()), // Extract first 2 seconds
16        columns: 200,
17        extract_audio: false,
18        preprocess_filter: None,
19    };
20
21    // Configure conversion options
22    let conv_opts = ConversionOptions::default()
23        .with_font_ratio(0.5)
24        .with_luminance(20);
25
26    // Convert video
27    let input = Path::new("tests/video/input/test.mkv");
28    let output_dir = Path::new("example_video_output");
29
30    if input.exists() {
31        println!("Converting video to ASCII frames...");
32        println!("Input: {}", input.display());
33        println!("Output: {}", output_dir.display());
34        println!("Settings: {}fps, {}s duration", video_opts.fps, 2);
35
36        converter.convert_video(
37            input,
38            output_dir,
39            &video_opts,
40            &conv_opts,
41            false, // Don't keep intermediate PNG files
42        )?;
43
44        println!("✓ Video conversion complete!");
45        println!("ASCII frames saved to {}", output_dir.display());
46    } else {
47        println!("Note: {} not found.", input.display());
48        println!("To use this example, provide a video file at that path.");
49    }
50
51    Ok(())
52}