use std::{path::Path, process};
use avio::{AsyncVideoDecoder, AsyncVideoEncoder, VideoCodec, VideoDecoder, VideoEncoder};
use futures::StreamExt;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut args = std::env::args().skip(1);
let mut input = None::<String>;
let mut output = None::<String>;
let mut codec_str = "h264".to_string();
while let Some(flag) = args.next() {
match flag.as_str() {
"--input" | "-i" => input = Some(args.next().unwrap_or_default()),
"--output" | "-o" => output = Some(args.next().unwrap_or_default()),
"--codec" | "-c" => codec_str = args.next().unwrap_or_else(|| "h264".to_string()),
other => {
eprintln!("Unknown flag: {other}");
process::exit(1);
}
}
}
let input = input.unwrap_or_else(|| {
eprintln!(
"Usage: async_transcode --input <file> --output <file> \
[--codec h264|h265|vp9|av1]"
);
process::exit(1);
});
let output = output.unwrap_or_else(|| {
eprintln!("--output is required");
process::exit(1);
});
let codec = match codec_str.to_lowercase().as_str() {
"h264" | "avc" => VideoCodec::H264,
"h265" | "hevc" => VideoCodec::H265,
"vp9" => VideoCodec::Vp9,
"av1" => VideoCodec::Av1,
other => {
eprintln!("Unknown codec '{other}' (try h264, h265, vp9, av1)");
process::exit(1);
}
};
let in_name = Path::new(&input)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(&input);
let out_name = Path::new(&output)
.file_name()
.and_then(|n| n.to_str())
.unwrap_or(&output);
let probe = match VideoDecoder::open(&input).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Error opening input: {e}");
process::exit(1);
}
};
let width = probe.width();
let height = probe.height();
let fps = probe.frame_rate();
let in_codec = probe.stream_info().codec_name().to_string();
drop(probe);
println!("Input: {in_name} {width}×{height} {fps:.2} fps codec={in_codec}");
println!("Output: {out_name} {width}×{height} codec={codec_str}");
println!();
println!("=== Pattern 1: sequential async transcode ===");
println!("Encoding...");
let mut encoder = match AsyncVideoEncoder::from_builder(
VideoEncoder::create(&output)
.video(width, height, fps)
.video_codec(codec),
) {
Ok(e) => e,
Err(e) => {
eprintln!("Error building encoder: {e}");
process::exit(1);
}
};
let decoder = match AsyncVideoDecoder::open(input.clone()).await {
Ok(d) => d,
Err(e) => {
eprintln!("Error opening decoder: {e}");
process::exit(1);
}
};
let mut frames: u64 = 0;
let stream = decoder.into_stream();
tokio::pin!(stream);
while let Some(result) = stream.next().await {
match result {
Ok(frame) => {
if let Err(e) = encoder.push(frame).await {
eprintln!("Encode error: {e}");
break;
}
frames += 1;
}
Err(e) => {
eprintln!("Decode error: {e}");
break;
}
}
}
if let Err(e) = encoder.finish().await {
eprintln!("Error finalising output: {e}");
process::exit(1);
}
let size_str = file_size_str(&output);
println!("Done. {out_name} {size_str} {frames} frames");
println!();
println!("=== Pattern 2: concurrent decode + encode ===");
println!("Encoding...");
let (tx, mut rx) = tokio::sync::mpsc::channel(16);
let decode_input = input.clone();
let decode_task = tokio::spawn(async move {
let decoder = AsyncVideoDecoder::open(decode_input)
.await
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
let stream = decoder.into_stream();
tokio::pin!(stream);
while let Some(result) = stream.next().await {
let frame =
result.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>)?;
if tx.send(frame).await.is_err() {
break;
}
}
Ok::<_, Box<dyn std::error::Error + Send + Sync>>(())
});
let mut encoder2 = match AsyncVideoEncoder::from_builder(
VideoEncoder::create(&output)
.video(width, height, fps)
.video_codec(codec),
) {
Ok(e) => e,
Err(e) => {
eprintln!("Error building encoder: {e}");
process::exit(1);
}
};
let mut frames2: u64 = 0;
while let Some(frame) = rx.recv().await {
if let Err(e) = encoder2.push(frame).await {
eprintln!("Encode error: {e}");
break;
}
frames2 += 1;
}
if let Err(e) = encoder2.finish().await {
eprintln!("Error finalising output: {e}");
process::exit(1);
}
match decode_task.await {
Ok(Ok(())) => {}
Ok(Err(e)) => eprintln!("Decode task error: {e}"),
Err(e) => eprintln!("Decode task panicked: {e}"),
}
let size_str2 = file_size_str(&output);
println!("Done. {out_name} {size_str2} {frames2} frames");
Ok(())
}
fn file_size_str(path: &str) -> String {
match std::fs::metadata(path) {
Ok(m) => {
#[allow(clippy::cast_precision_loss)]
let kb = m.len() as f64 / 1024.0;
if kb < 1024.0 {
format!("{kb:.0} KB")
} else {
format!("{:.1} MB", kb / 1024.0)
}
}
Err(_) => "(unknown size)".to_string(),
}
}