#mp3 #lame #mp3lame #codec #libmp3lame #sample-rate

no-std mp3lame-encoder

High level bindings to mp3lame encoder

7 releases

0.2.1 Dec 4, 2024
0.2.0 Jun 14, 2024
0.1.5 Mar 17, 2024
0.1.4 Aug 28, 2023
0.1.0 Dec 10, 2022

#38 in Audio

Download history 243/week @ 2024-08-22 256/week @ 2024-08-29 490/week @ 2024-09-05 1292/week @ 2024-09-12 2743/week @ 2024-09-19 3524/week @ 2024-09-26 3415/week @ 2024-10-03 4199/week @ 2024-10-10 3727/week @ 2024-10-17 6509/week @ 2024-10-24 8094/week @ 2024-10-31 6695/week @ 2024-11-07 8654/week @ 2024-11-14 10375/week @ 2024-11-21 10397/week @ 2024-11-28 8882/week @ 2024-12-05

38,784 downloads per month
Used in 17 crates (5 directly)

LGPL-3.0

35KB
594 lines

mp3lame-encoder

Rust Crates.io Documentation

High level wrapper over mp3lame-sys

Example

use mp3lame_encoder::{Builder, Id3Tag, DualPcm, FlushNoGap};

let mut mp3_encoder = Builder::new().expect("Create LAME builder");
mp3_encoder.set_num_channels(2).expect("set channels");
mp3_encoder.set_sample_rate(44_100).expect("set sample rate");
mp3_encoder.set_brate(mp3lame_encoder::Bitrate::Kbps192).expect("set brate");
mp3_encoder.set_quality(mp3lame_encoder::Quality::Best).expect("set quality");
mp3_encoder.set_id3_tag(Id3Tag {
    title: b"My title",
    artist: &[],
    album: b"My album",
    year: b"Current year",
    comment: b"Just my comment",
});
let mut mp3_encoder = mp3_encoder.build().expect("To initialize LAME encoder");

//use actual PCM data
let input = DualPcm {
    left: &[0u16, 0],
    right: &[0u16, 0],
};

let mut mp3_out_buffer = Vec::new();
mp3_out_buffer.reserve(mp3lame_encoder::max_required_buffer_size(input.left.len()));
let encoded_size = mp3_encoder.encode(input, mp3_out_buffer.spare_capacity_mut()).expect("To encode");
unsafe {
    mp3_out_buffer.set_len(mp3_out_buffer.len().wrapping_add(encoded_size));
}

let encoded_size = mp3_encoder.flush::<FlushNoGap>(mp3_out_buffer.spare_capacity_mut()).expect("to flush");
unsafe {
    mp3_out_buffer.set_len(mp3_out_buffer.len().wrapping_add(encoded_size));
}
//At this point your mp3_out_buffer should have full MP3 data, ready to be written on file system or whatever

License

LAME library is under LGPL License. Hence this crate is licensed under the same shitty license

Dependencies

~3MB
~70K SLoC