A lightweight FFmpeg audio decoding wrapper designed for music player applications.
- Simple and intuitive API
- Pure Rust compilation via cc — no C build system required
- Fully statically linked with zero external FFmpeg dependencies
- Bundled with a specific, up-to-date FFmpeg version (currently 8.1.1)
- Optional
tracingfeature for FFmpeg log integration
This crate leverages FFmpeg's decoding capabilities and supports almost all audio formats, including:
- MP3, AAC, FLAC, WAV, OGG, Opus, WMA, ALAC, AIFF, and more
You can find the complete list in generate_config.ts
The resampler output supports the following Rust-native sample types:
| Type | FFmpeg Format |
|---|---|
f32 |
32-bit Float |
i16 |
16-bit Signed |
i32 |
32-bit Signed |
u8 |
8-bit Unsigned |
[dependencies]
ffmpeg_audio = { git = "https://github.com/apoint123/ffmpeg-audio" }
use std::fs::File;
use ffmpeg_audio::{AudioReader, ResampleOptions};
// 1. Initialize the decoding engine
let reader = AudioReader::new(File::open("song.mp3")?)?;
// 2. Configure target audio parameters (e.g., 48kHz, Stereo, 32-bit Float)
let options = ResampleOptions::new()
.sample_rate(48000)
.channels(2)
.format::<f32>();
// 3. Transform into a resampled data pipeline
let mut resampled = reader.into_resampled(options)?;
// 4. Safely pull typed audio frames
while let Some(samples) = resampled.receive_frame_as::<f32>()? {
// `samples` is strictly typed as &[f32]
// process your interleaved samples here
}This crate covers all Rust Tier 1 targets and most mainstream Tier 2 targets. Highlights include:
- Windows — MSVC & GNU toolchains (
x86_64,x86,aarch64,arm) - Linux / BSD —
x86_64,x86,aarch64,armv7(including FreeBSD, NetBSD, OpenBSD, and more) - macOS —
x86_64&aarch64 - iOS / tvOS / watchOS / visionOS —
aarch64(device & simulator),x86_64(simulator) - Android —
aarch64,armv7,x86,x86_64 - WebAssembly —
wasm32-unknown-emscripten(note:wasm32-unknown-unknownis not supported)
For the full list of supported target triples, see get_config_dir_name().
Warning
Building for a target triple not listed in get_config_dir_name() might fail at compile time, or cause a runtime UB.
The crates/ffmpeg_audio/examples directory contains runnable examples:
play.rs— A complete audio player usingcpalfor audio outputmetadata.rs— Extracts audio metadata and cover art from files
Run an example with:
cargo run --example play -- path/to/audio.mp3
cargo run --example metadata -- path/to/audio.flacTo build for wasm32-unknown-emscripten, install the following prerequisites:
-
Emscripten SDK (emsdk) — Follow the official installation guide. After installation, make sure
emccis in yourPATH. -
Rust target:
rustup target add wasm32-unknown-emscripten
-
libclang — Required by bindgen to generate C bindings. Most systems already have it; Emscripten SDK also ships one.
Then build as usual:
cargo build --target wasm32-unknown-emscripten --releaseThe output (a .js + .wasm pair) can be run with Node.js:
node target/wasm32-unknown-emscripten/release/your_app.jsThis is a Cargo workspace containing two crates:
ffmpeg_audio— High-level Rust API for audio decoding and resamplingffmpeg_audio_sys— Low-level FFI bindings to FFmpeg's C libraries
This crate focuses solely on audio decoding. The following features are not included:
- Video processing
- Encoders, muxers, and filters
- Swscale (video scaling/conversion)
- Hardware acceleration
- Device and network protocol support