Skip to content

Commit

Permalink
docs: demo rendering raw video to terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanbabcock committed Nov 6, 2024
1 parent ab66c11 commit bbf37b9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions examples/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use anyhow::Result;
use ffmpeg_sidecar::command::FfmpegCommand;

const OUTPUT_WIDTH: u32 = 80;
const OUTPUT_HEIGHT: u32 = 30;
const OUTPUT_FRAMERATE: u32 = 60;

/// Render video to the terminal
fn main() -> Result<()> {
let iter = FfmpegCommand::new()
.format("lavfi")
.arg("-re") // "realtime"
.input(format!(
"testsrc=size={OUTPUT_WIDTH}x{OUTPUT_HEIGHT}:rate={OUTPUT_FRAMERATE}"
))
.rawvideo()
.spawn()?
.iter()?
.filter_frames();

for frame in iter {
// clear the previous frame
if frame.frame_num > 0 {
for _ in 0..frame.height {
print!("\x1B[{}A", 1);
}
}

// Print the pixels colored with ANSI codes
for y in 0..frame.height {
for x in 0..frame.width {
let idx = (y * frame.width + x) as usize * 3;
let r = frame.data[idx] as u32;
let g = frame.data[idx + 1] as u32;
let b = frame.data[idx + 2] as u32;
print!("\x1B[48;2;{r};{g};{b}m ");
}
println!("\x1B[0m");
}
}

Ok(())
}

0 comments on commit bbf37b9

Please sign in to comment.