use std::{path::Path, process};
use avio::{PixelFormat, VideoDecoder};
fn main() {
let mut args = std::env::args().skip(1);
let mut input = None::<String>;
while let Some(flag) = args.next() {
match flag.as_str() {
"--input" | "-i" => input = Some(args.next().unwrap_or_default()),
other => {
eprintln!("Unknown flag: {other}");
process::exit(1);
}
}
}
let input = input.unwrap_or_else(|| {
eprintln!("Usage: decode_scaled --input <file>");
process::exit(1);
});
let in_name = Path::new(&input)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(&input);
println!("Input: {in_name}");
println!();
let probe = match VideoDecoder::open(&input).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Cannot open video: {e}");
process::exit(1);
}
};
let src_w = probe.width();
let src_h = probe.height();
println!(
"Source: {src_w}×{src_h} codec={}",
probe.stream_info().codec_name()
);
println!();
println!("=== output_size(320, 240) ===");
let mut dec = match VideoDecoder::open(&input).output_size(320, 240).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Skipping: {e}");
return;
}
};
match dec.decode_one() {
Ok(Some(frame)) => println!(
"Frame: {}×{} format={}",
frame.width(),
frame.height(),
frame.format()
),
Ok(None) => println!("No frames"),
Err(e) => println!("Error: {e}"),
}
println!();
println!("=== output_width(640) ===");
let mut dec = match VideoDecoder::open(&input).output_width(640).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Skipping: {e}");
return;
}
};
match dec.decode_one() {
Ok(Some(frame)) => println!(
"Frame: {}×{} format={} (src {}×{})",
frame.width(),
frame.height(),
frame.format(),
src_w,
src_h
),
Ok(None) => println!("No frames"),
Err(e) => println!("Error: {e}"),
}
println!();
println!("=== output_height(360) ===");
let mut dec = match VideoDecoder::open(&input).output_height(360).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Skipping: {e}");
return;
}
};
match dec.decode_one() {
Ok(Some(frame)) => println!(
"Frame: {}×{} format={} (src {}×{})",
frame.width(),
frame.height(),
frame.format(),
src_w,
src_h
),
Ok(None) => println!("No frames"),
Err(e) => println!("Error: {e}"),
}
println!();
println!("=== output_size(160, 90) + output_format(Rgb24) ===");
let mut dec = match VideoDecoder::open(&input)
.output_size(160, 90)
.output_format(PixelFormat::Rgb24)
.build()
{
Ok(d) => d,
Err(e) => {
eprintln!("Skipping: {e}");
return;
}
};
match dec.decode_one() {
Ok(Some(frame)) => {
let expected_bytes = 160 * 90 * 3; println!(
"Frame: {}×{} format={} planes={} total_bytes={} (expected {})",
frame.width(),
frame.height(),
frame.format(),
frame.num_planes(),
frame.total_size(),
expected_bytes,
);
}
Ok(None) => println!("No frames"),
Err(e) => println!("Error: {e}"),
}
}