use std::{path::Path, process};
use avio::{
AacOptions, AacProfile, AudioCodec, AudioCodecOptions, AudioDecoder, AudioEncoder, FlacOptions,
Mp3Options, Mp3Quality, OpusApplication, OpusOptions, OutputContainer,
};
fn main() {
let mut args = std::env::args().skip(1);
let mut input = None::<String>;
let mut output = None::<String>;
let mut codec_str = "opus".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(|| "opus".to_string()),
other => {
eprintln!("Unknown flag: {other}");
process::exit(1);
}
}
}
let input = input.unwrap_or_else(|| {
eprintln!(
"Usage: audio_codec_options --input <file> --output <file> \
[--codec opus|aac|mp3|flac]"
);
process::exit(1);
});
let output = output.unwrap_or_else(|| {
eprintln!("--output is required");
process::exit(1);
});
let probe = match AudioDecoder::open(&input).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Error opening input: {e}");
process::exit(1);
}
};
let sample_rate = probe.sample_rate();
let channels = probe.channels();
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} {sample_rate} Hz {channels} ch");
let (audio_codec, codec_options, bitrate, container, description) =
match codec_str.to_lowercase().as_str() {
"opus" => {
let opts = OpusOptions {
application: OpusApplication::Audio,
frame_duration_ms: Some(20),
};
(
AudioCodec::Opus,
AudioCodecOptions::Opus(opts),
128_000u64,
Some(OutputContainer::Ogg),
"Opus, Audio application, 128 kbps, 20 ms frames, OGG container",
)
}
"aac" => {
let opts = AacOptions {
profile: AacProfile::Lc,
vbr_quality: None, };
(
AudioCodec::Aac,
AudioCodecOptions::Aac(opts),
192_000u64,
None, "AAC-LC, CBR 192 kbps",
)
}
"mp3" => {
let opts = Mp3Options {
quality: Mp3Quality::Vbr(2), };
(
AudioCodec::Mp3,
AudioCodecOptions::Mp3(opts),
0u64, None, "MP3, VBR quality V2 (~190 kbps average)",
)
}
"flac" => {
let opts = FlacOptions {
compression_level: 6,
};
(
AudioCodec::Flac,
AudioCodecOptions::Flac(opts),
0u64, Some(OutputContainer::Flac),
"FLAC, compression level 6, FLAC container",
)
}
other => {
eprintln!("Unknown codec '{other}' (try opus, aac, mp3, flac)");
process::exit(1);
}
};
println!("Codec: {description}");
println!("Output: {out_name}");
println!();
let mut enc_builder = AudioEncoder::create(&output)
.audio(sample_rate, channels)
.audio_codec(audio_codec)
.audio_bitrate(bitrate)
.codec_options(codec_options);
if let Some(c) = container {
enc_builder = enc_builder.container(c);
}
let mut encoder = match enc_builder.build() {
Ok(e) => e,
Err(e) => {
eprintln!("Error building encoder: {e}");
process::exit(1);
}
};
let mut decoder = match AudioDecoder::open(&input).build() {
Ok(d) => d,
Err(e) => {
eprintln!("Error opening decoder: {e}");
process::exit(1);
}
};
let mut chunks: u64 = 0;
loop {
let chunk = match decoder.decode_one() {
Ok(Some(c)) => c,
Ok(None) => break,
Err(e) => {
eprintln!("Decode error: {e}");
process::exit(1);
}
};
if let Err(e) = encoder.push(&chunk) {
eprintln!("Encode error: {e}");
process::exit(1);
}
chunks += 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} {chunks} audio chunks");
}