How FFmpeg Handles Speex Audio Streams

This article explains how the FFmpeg multimedia framework processes Speex audio streams. It covers FFmpeg’s native decoding capabilities, the requirement of external libraries for encoding, container compatibility, and practical command-line examples for converting and manipulating Speex audio.

Decoding Speex Audio

FFmpeg features a native, built-in Speex decoder. This allows users to decode, play, and convert Speex audio streams out of the box without needing any external libraries or special compilation configurations. Whether the Speex stream is contained within an Ogg wrapper (.spx or .ogg) or a Flash Video container (.flv), FFmpeg can read and demux the stream automatically.

To convert a Speex audio file to a more common format like WAV, you can use the following command:

ffmpeg -i input.spx output.wav

Encoding Speex Audio

Unlike decoding, FFmpeg does not have a native, proprietary Speex encoder. To compress or encode audio into the Speex format, FFmpeg must be compiled with the external libspeex library.

When compiling FFmpeg from source, this is enabled by adding the --enable-libspeex flag to the configuration script. Most package managers (like apt, brew, or dnf) include libspeex support in their default FFmpeg builds.

To encode an audio file to Speex using the external library, specify the libspeex encoder:

ffmpeg -i input.wav -c:a libspeex -b:a 15k output.spx

Container Compatibility and Muxing

Speex audio is most commonly encapsulated in two types of containers:

  1. Ogg/OGG (.spx or .ogg): This is the standard native container for Speex. FFmpeg fully supports muxing and demuxing Speex inside Ogg containers.
  2. Flash Video (.flv): Speex was widely used for real-time voice communication in Flash applications. FFmpeg can extract Speex streams from FLV files or mux Speex streams into FLV files.

Stream Copying

If you want to extract a Speex stream from a video container without re-encoding (which preserves original quality and saves CPU power), you can use the copy codec:

ffmpeg -i input.flv -vn -c:a copy output.spx

Common Speex Encoding Parameters

When using the libspeex encoder in FFmpeg, you can control the output quality and bandwidth using several parameters: