pub struct AudioDecoderBuilder { /* private fields */ }Expand description
Builder for configuring and constructing an AudioDecoder.
This struct provides a fluent interface for setting up decoder options
before opening an audio file. It is created by calling AudioDecoder::open().
§Examples
§Basic Usage
use ff_decode::AudioDecoder;
let decoder = AudioDecoder::open("audio.mp3")?
.build()?;§With Custom Format and Sample Rate
use ff_decode::AudioDecoder;
use ff_format::SampleFormat;
let decoder = AudioDecoder::open("audio.mp3")?
.output_format(SampleFormat::F32)
.output_sample_rate(48000)
.build()?;Implementations§
Source§impl AudioDecoderBuilder
impl AudioDecoderBuilder
Sourcepub fn output_format(self, format: SampleFormat) -> Self
pub fn output_format(self, format: SampleFormat) -> Self
Sets the output sample format for decoded frames.
If not set, frames are returned in the source format. Setting an output format enables automatic conversion during decoding.
§Common Formats
SampleFormat::F32- 32-bit float, most common for editingSampleFormat::I16- 16-bit integer, CD qualitySampleFormat::F32p- Planar 32-bit float, efficient for processing
§Examples
use ff_decode::AudioDecoder;
use ff_format::SampleFormat;
let decoder = AudioDecoder::open("audio.mp3")?
.output_format(SampleFormat::F32)
.build()?;Sourcepub fn output_sample_rate(self, sample_rate: u32) -> Self
pub fn output_sample_rate(self, sample_rate: u32) -> Self
Sets the output sample rate in Hz.
If not set, frames are returned at the source sample rate. Setting an output sample rate enables automatic resampling during decoding.
§Common Sample Rates
- 44100 Hz - CD quality audio
- 48000 Hz - Professional audio, most common in video
- 96000 Hz - High-resolution audio
§Examples
use ff_decode::AudioDecoder;
// Resample to 48kHz
let decoder = AudioDecoder::open("audio.mp3")?
.output_sample_rate(48000)
.build()?;Sourcepub fn output_channels(self, channels: u32) -> Self
pub fn output_channels(self, channels: u32) -> Self
Sets the output channel count.
If not set, frames are returned with the source channel count. Setting an output channel count enables automatic channel remixing during decoding.
§Common Channel Counts
- 1 - Mono
- 2 - Stereo
- 6 - 5.1 surround sound
§Examples
use ff_decode::AudioDecoder;
// Convert to stereo
let decoder = AudioDecoder::open("audio.mp3")?
.output_channels(2)
.build()?;Sourcepub fn network(self, opts: NetworkOptions) -> Self
pub fn network(self, opts: NetworkOptions) -> Self
Sets network options for URL-based audio sources (HTTP, RTSP, RTMP, etc.).
When set, the builder skips the file-existence check and passes connect
and read timeouts to avformat_open_input via an AVDictionary.
Call this before .build() when opening rtmp://, rtsp://, http://,
https://, udp://, srt://, or rtp:// URLs.
§HLS / M3U8 Playlists
Audio-only HLS playlists (.m3u8 pointing to AAC or MP3 segments) are
detected automatically by FFmpeg. Pass the full HTTP(S) URL:
use ff_decode::AudioDecoder;
use ff_format::NetworkOptions;
let decoder = AudioDecoder::open("https://example.com/audio/index.m3u8")
.network(NetworkOptions::default())
.build()?;§UDP / MPEG-TS
udp:// URLs are always live — is_live() returns true and seeking
is not supported. Two extra AVDictionary options are set automatically
to reduce packet loss on high-bitrate streams:
| Option | Value | Reason |
|---|---|---|
buffer_size | 65536 | Enlarges the UDP receive buffer |
overrun_nonfatal | 1 | Discards excess data instead of erroring |
§SRT (Secure Reliable Transport)
SRT URLs (srt://host:port) require the srt feature flag and a
libsrt-enabled FFmpeg build. Enable the feature in Cargo.toml:
[dependencies]
ff-decode = { version = "*", features = ["srt"] }Without the srt feature, opening an srt:// URL returns
DecodeError::ConnectionFailed. If the feature is enabled but the
linked FFmpeg was not built with --enable-libsrt, the same error is
returned with a message directing you to rebuild FFmpeg.
use ff_decode::AudioDecoder;
use ff_format::NetworkOptions;
let decoder = AudioDecoder::open("srt://ingest.example.com:4200")
.network(NetworkOptions::default())
.build()?;§Credentials
HTTP basic-auth credentials must be embedded directly in the URL:
https://user:password@cdn.example.com/audio/index.m3u8.
The password is redacted in log output.
§DRM Limitation
DRM-protected streams are not supported:
- HLS:
FairPlay, Widevine, AES-128 with external key servers - DASH: CENC,
PlayReady, Widevine
FFmpeg can parse the manifest and fetch segments, but key delivery
to a DRM license server is outside the scope of this API.
§Examples
use ff_decode::AudioDecoder;
use ff_format::NetworkOptions;
use std::time::Duration;
let decoder = AudioDecoder::open("http://stream.example.com/audio.aac")
.network(NetworkOptions {
connect_timeout: Duration::from_secs(5),
..Default::default()
})
.build()?;Sourcepub fn get_output_format(&self) -> Option<SampleFormat>
pub fn get_output_format(&self) -> Option<SampleFormat>
Returns the configured output format, if any.
Sourcepub fn get_output_sample_rate(&self) -> Option<u32>
pub fn get_output_sample_rate(&self) -> Option<u32>
Returns the configured output sample rate, if any.
Sourcepub fn get_output_channels(&self) -> Option<u32>
pub fn get_output_channels(&self) -> Option<u32>
Returns the configured output channel count, if any.
Sourcepub fn build(self) -> Result<AudioDecoder, DecodeError>
pub fn build(self) -> Result<AudioDecoder, DecodeError>
Builds the audio decoder with the configured options.
This method opens the media file, initializes the decoder context, and prepares for frame decoding.
§Errors
Returns an error if:
- The file cannot be found (
DecodeError::FileNotFound) - The file contains no audio stream (
DecodeError::NoAudioStream) - The codec is not supported (
DecodeError::UnsupportedCodec) - Other
FFmpegerrors occur (DecodeError::Ffmpeg)
§Examples
use ff_decode::AudioDecoder;
let decoder = AudioDecoder::open("audio.mp3")?
.build()?;
// Start decoding
for result in &mut decoder {
let frame = result?;
// Process frame...
}