Can libmp3lame read audio from standard input
This article explains whether the libmp3lame library and
the associated LAME command-line utility support reading uncompressed
audio data directly from standard input (stdin). It clarifies the
technical distinction between the C library API and the command-line
executable, while providing practical examples of how to pipe raw PCM
audio into the encoder.
The Library (libmp3lame) vs. The Command-Line Tool (LAME)
To answer the question accurately, we must distinguish between the
libmp3lame software library and the lame
command-line interface (CLI).
- The
libmp3lameLibrary: No, the library itself does not directly read from stdin. As a C programming library,libmp3lameoperates entirely in-memory. It does not handle file I/O or input streams. Instead, the host application calling the library is responsible for reading uncompressed audio data (from stdin, a file, or a network stream) into a memory buffer and then passing that buffer to library functions likelame_encode_buffer(). - The
lameCommand-Line Tool: Yes, the command-line utility built on top of the library fully supports reading uncompressed audio from stdin.
How to Pipe Uncompressed Audio to the LAME CLI
When using the LAME command-line tool, you can instruct it to read
from standard input by using a hyphen (-) in place of the
input file path.
Because standard input is a continuous stream, it often lacks the header metadata found in container formats like WAV or AIFF. Therefore, when piping raw, headerless PCM audio, you must explicitly define the audio properties using command-line flags.
Here is the standard command structure for piping raw PCM audio into LAME:
cat input.raw | lame -r -s 44.1 --bitwidth 16 -m s - output.mp3Explanation of the Parameters:
-r: Informs LAME that the input is raw PCM data rather than a formatted file with headers.-s 44.1: Specifies the sample rate of the input audio in kHz (e.g., 44.1 kHz).--bitwidth 16: Specifies the bit depth of the raw input audio (typically 16-bit).-m s: Specifies the mode (e.g.,sfor stereo,mfor mono).-: Represents standard input (stdin) as the input source.output.mp3: The path to the destination MP3 file.