| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use std::process::ExitCode; |
| 9 | |
| 10 | use ec_aac::{AacEncoder, AacEncoderConfig}; |
| 11 | |
| 12 | fn main() -> ExitCode { |
| 13 | let args: Vec<String> = std::env::args().collect(); |
| 14 | if args.len() != 6 { |
| 15 | eprintln!("usage: aacenc_file <in.f32le> <channels> <rate> <kbps> <out.aac>"); |
| 16 | return ExitCode::from(2); |
| 17 | } |
| 18 | let parse = |s: &str| s.parse::<u32>().ok(); |
| 19 | let (Some(channels), Some(rate), Some(kbps)) = (parse(&args[2]), parse(&args[3]), parse(&args[4])) |
| 20 | else { |
| 21 | eprintln!("channels, rate and kbps must be unsigned integers"); |
| 22 | return ExitCode::from(2); |
| 23 | }; |
| 24 | let raw = match std::fs::read(&args[1]) { |
| 25 | Ok(raw) => raw, |
| 26 | Err(e) => { |
| 27 | eprintln!("read {}: {e}", args[1]); |
| 28 | return ExitCode::FAILURE; |
| 29 | } |
| 30 | }; |
| 31 | let pcm: Vec<f32> = raw |
| 32 | .chunks(4) |
| 33 | .map(|b| f32::from_le_bytes(b.try_into().expect("whole f32 samples"))) |
| 34 | .collect(); |
| 35 | let mut enc = AacEncoder::new(AacEncoderConfig { |
| 36 | bitrate_bps: kbps * 1_000, |
| 37 | adts: true, |
| 38 | ..Default::default() |
| 39 | }); |
| 40 | if let Err(e) = enc.push_pcm(&pcm, channels as u16, rate) { |
| 41 | eprintln!("push_pcm: {e:?}"); |
| 42 | return ExitCode::FAILURE; |
| 43 | } |
| 44 | enc.finish(); |
| 45 | let mut out = Vec::new(); |
| 46 | while let Ok(p) = enc.next_packet() { |
| 47 | out.extend_from_slice(&p.data); |
| 48 | } |
| 49 | if let Err(e) = std::fs::write(&args[5], &out) { |
| 50 | eprintln!("write {}: {e}", args[5]); |
| 51 | return ExitCode::FAILURE; |
| 52 | } |
| 53 | ExitCode::SUCCESS |
| 54 | } |