3 releases

0.1.2 May 18, 2021
0.1.1 May 18, 2021
0.1.0 May 18, 2021

#891 in Audio

Download history 36/week @ 2024-02-05 18/week @ 2024-02-12 15/week @ 2024-02-19 28/week @ 2024-02-26 14/week @ 2024-03-04 12/week @ 2024-03-11 33/week @ 2024-03-18 13/week @ 2024-03-25 61/week @ 2024-04-01

121 downloads per month

MIT license

398 lines

Ogg Opus

A decoder/encoder for Ogg Opus in Rust


Add to your cargo.toml

ogg-opus = "^0.1"

Minimum Rust version

Since we use const generics, the minimum version of Rust is 1.51



This example makes use of wav crate, you can use it adding to your cargo.toml file:

wav = "^1.0"
let mut f = File::open("my_file.wav").unwrap();
let (_, b) = wav::read(&mut f).unwrap();
let audio = b.try_into_sixteen().unwrap();
let opus = ogg_opus::encode::<16000, 1>(&audio).unwrap();


Read from file

let mut f = File::open("my_file.ogg").unwrap();
let (raw, header) = ogg_opus::decode::<_,16000>(f).unwrap();

Read from Vec

use std::io::Cursor;

// Let's say this vec contains Ogg Opus data
let opus: Vec<u8> = Vec::new();
let (raw, header) = ogg_opus::decode::<_,16000>(Cursor::new(opus)).unwrap();

What works and what not

  • Only supports i16 (integer of 16 bits) for the raw part.
  • Both mono and stereo are supported but only mono is tested.
  • More channels than stereo are untested and will probably break it.
  • Supports decoding and encoding any sample rate supported by Opus (8k Hz, 12k Hz, 24k Hz and 64k Hz) but only 16k Hz has been tested
  • Encoding is set to a bitrate of 24k (because of Lily's constraints)
  • There's still some inaccuracies around start and end of audio (can't tell if it's due to the encoder or the decoder)
  • Advanced decode and encoding features (repairables streams, fec and others)


~74K SLoC