-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: demo rendering raw video to terminal
- Loading branch information
1 parent
ab66c11
commit bbf37b9
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |