Skip to main content

AudioDecoderBuilder

Struct AudioDecoderBuilder 

Source
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

Source

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
§Examples
use ff_decode::AudioDecoder;
use ff_format::SampleFormat;

let decoder = AudioDecoder::open("audio.mp3")?
    .output_format(SampleFormat::F32)
    .build()?;
Source

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()?;
Source

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()?;
Source

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:

OptionValueReason
buffer_size65536Enlarges the UDP receive buffer
overrun_nonfatal1Discards 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()?;
Source

pub fn path(&self) -> &Path

Returns the configured file path.

Source

pub fn get_output_format(&self) -> Option<SampleFormat>

Returns the configured output format, if any.

Source

pub fn get_output_sample_rate(&self) -> Option<u32>

Returns the configured output sample rate, if any.

Source

pub fn get_output_channels(&self) -> Option<u32>

Returns the configured output channel count, if any.

Source

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:

§Examples
use ff_decode::AudioDecoder;

let decoder = AudioDecoder::open("audio.mp3")?
    .build()?;

// Start decoding
for result in &mut decoder {
    let frame = result?;
    // Process frame...
}

Trait Implementations§

Source§

impl Debug for AudioDecoderBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.