use std::{path::Path, process};
use avio::{AudioDecoder, PixelFormat, SampleFormat, 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_format_convert --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!();
println!("=== Video: output_format(PixelFormat::Rgb24) ===");
let mut vdec = match VideoDecoder::open(&input)
.output_format(PixelFormat::Rgb24)
.build()
{
Ok(d) => d,
Err(e) => {
eprintln!("Skipping video: {e}");
return;
}
};
let src_w = vdec.width();
let src_h = vdec.height();
println!(
"Source: {src_w}×{src_h} codec={}",
vdec.stream_info().codec_name()
);
match vdec.decode_one() {
Ok(Some(frame)) => {
println!(
"Frame: {}×{} format={} planes={} total_bytes={}",
frame.width(),
frame.height(),
frame.format(),
frame.num_planes(),
frame.total_size(),
);
let plane = frame.plane(0);
println!(
"Plane[0]: stride={} data_len={}",
frame.stride(0).unwrap_or(0),
plane.map_or(0, |p| p.len()),
);
}
Ok(None) => println!("No frames in file"),
Err(e) => println!("Decode error: {e}"),
}
println!();
println!("=== Audio: output_format(SampleFormat::I16) + output_sample_rate(44100) ===");
let mut adec = match AudioDecoder::open(&input)
.output_format(SampleFormat::I16)
.output_sample_rate(44_100)
.build()
{
Ok(d) => d,
Err(e) => {
eprintln!("Skipping audio: {e}");
return;
}
};
println!(
"Source: sample_rate={} channels={} codec={}",
adec.sample_rate(),
adec.channels(),
adec.stream_info().codec_name()
);
match adec.decode_one() {
Ok(Some(frame)) => {
println!(
"Frame: samples={} channels={} sample_rate={} format={}",
frame.samples(),
frame.channels(),
frame.sample_rate(),
frame.format(),
);
let plane = frame.data();
println!("Plane[0]: data_len={} bytes_per_sample=2", plane.len());
}
Ok(None) => println!("No audio frames in file"),
Err(e) => println!("Decode error: {e}"),
}
}