use std::{path::Path, process};
use avio::{
Av1Options, Av1Usage, BitrateMode, H264Options, H264Preset, H264Profile, H265Options,
H265Profile, PixelFormat, SvtAv1Options, VideoCodec, VideoCodecOptions, VideoDecoder,
VideoEncoder, Vp9Options,
};
fn main() {
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: codec_options --input <file> --output <file> \
[--codec h264|h265|av1|svt-av1|vp9]"
);
process::exit(1);
});
let output = output.unwrap_or_else(|| {
eprintln!("--output is required");
process::exit(1);
});
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();
drop(probe);
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);
println!("Input: {in_name} {width}×{height} {fps:.2} fps");
let (video_codec, codec_options, pixel_format, description) =
match codec_str.to_lowercase().as_str() {
"h264" => {
let opts = H264Options {
profile: H264Profile::High,
level: Some(41), bframes: 2,
gop_size: 250,
refs: 3,
preset: Some(H264Preset::Fast),
tune: None,
};
(
VideoCodec::H264,
VideoCodecOptions::H264(opts),
None,
"H.264 High@4.1, fast preset, 2 B-frames",
)
}
"h265" => {
let opts = H265Options {
profile: H265Profile::Main10,
preset: Some("fast".to_string()),
..H265Options::default()
};
(
VideoCodec::H265,
VideoCodecOptions::H265(opts),
Some(PixelFormat::Yuv420p10le),
"H.265 Main10 (10-bit), fast preset",
)
}
"av1" => {
let opts = Av1Options {
cpu_used: 6, tile_rows: 1, tile_cols: 1, usage: Av1Usage::VoD,
};
(
VideoCodec::Av1,
VideoCodecOptions::Av1(opts),
None,
"AV1 (libaom), cpu_used=6, 2×2 tiles, VoD mode",
)
}
"svt-av1" => {
let opts = SvtAv1Options {
preset: 8, tile_rows: 1,
tile_cols: 1,
svtav1_params: None,
};
(
VideoCodec::Av1Svt,
VideoCodecOptions::Av1Svt(opts),
None,
"AV1 (SVT-AV1 / libsvtav1), preset=8, 2×2 tiles",
)
}
"vp9" => {
let opts = Vp9Options {
cpu_used: 4,
cq_level: Some(33), tile_columns: 1,
tile_rows: 0,
row_mt: true,
};
(
VideoCodec::Vp9,
VideoCodecOptions::Vp9(opts),
None,
"VP9 (libvpx-vp9), cpu_used=4, CQ=33, row-MT enabled",
)
}
other => {
eprintln!("Unknown codec '{other}' (try h264, h265, av1, svt-av1, vp9)");
process::exit(1);
}
};
println!("Codec: {description}");
println!("Output: {out_name}");
println!();
let mut enc_builder = VideoEncoder::create(&output)
.video(width, height, fps)
.video_codec(video_codec)
.bitrate_mode(BitrateMode::Crf(26))
.codec_options(codec_options);
if let Some(fmt) = pixel_format {
enc_builder = enc_builder.pixel_format(fmt);
}
let mut encoder = match enc_builder.build() {
Ok(e) => e,
Err(e) => {
eprintln!("Error building encoder: {e}");
process::exit(1);
}
};
println!(
"Encoder opened: actual_codec={}",
encoder.actual_video_codec()
);
let mut decoder = match VideoDecoder::open(&input).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Error opening decoder: {e}");
process::exit(1);
}
};
let mut frames: u64 = 0;
loop {
let frame = match decoder.decode_one() {
Ok(Some(f)) => f,
Ok(None) => break,
Err(e) => {
eprintln!("Decode error: {e}");
process::exit(1);
}
};
if let Err(e) = encoder.push_video(&frame) {
eprintln!("Encode error: {e}");
process::exit(1);
}
frames += 1;
}
if let Err(e) = encoder.finish() {
eprintln!("Error finalising output: {e}");
process::exit(1);
}
#[allow(clippy::cast_precision_loss)]
let kb = std::fs::metadata(&output).map_or(0, |m| m.len()) as f64 / 1024.0;
let size_str = if kb < 1024.0 {
format!("{kb:.0} KB")
} else {
format!("{:.1} MB", kb / 1024.0)
};
println!("Done. {out_name} {size_str} {frames} frames");
}