#rodio #ffmpeg #encoding

ffmpeg-decoder

Decodes audio files using ffmpeg with rust. Can be used as a rodio source.

4 releases

0.1.3 Sep 16, 2020
0.1.2 Mar 27, 2020
0.1.1 Mar 26, 2020
0.1.0 Mar 26, 2020

#817 in Audio

MIT license

24KB
536 lines

ffmpeg-decoder

Crates.io

Decodes audio files and converts sample format to signed 16bit. Can be used as a playback source with rodio.

Rodio Source

Decoder implies rodio's Source trait, as well as Iterator. Enable feature flag rodio_source to include this. Decoder can then be used as a source for Rodio, with the benefits of being able to decode everything ffmpeg supports.

Testing with CLI

Convert input file to signed 16bit and save as .raw alongisde original

cargo run --release -- convert path/to/test.mp3

Play with rodio

cargo run --release -- play path/to/test.flac

lib.rs:

Decodes audio files using ffmpeg bindings

Create a Decoder by supplying a Path to an audio file. Decoder implies Iterator where each iteration returns a single i16 signed 16bit sample. Also implements rodio's Source trait, where the Decoder can be supplied as a sink source for playback.

Features Flags

  • rodio_source to enable rodio's Source trait

Example as Rodio Source

use rodio::Sink;
use std::path::PathBuf;

fn play_file(input: PathBuf) -> Result<(), Error> {
    let decoder = ffmpeg_decoder::Decoder::open(&input)?;

    let device = rodio::default_output_device().unwrap();
    let sink = Sink::new(&device);

    sink.append(decoder);
    sink.play();
    sink.sleep_until_end();

    Ok(())
}

Dependencies

~0.5–4MB
~70K SLoC