Pipe Raw PCM Audio to FFmpeg and Convert to FLAC

This guide explains how to pipe raw Pulse-Code Modulation (PCM) audio data into FFmpeg via standard input (stdin) and transcode it directly to the lossless FLAC format. Because raw PCM data contains no headers to describe its properties, you must explicitly declare the sample format, sample rate, and channel count in the FFmpeg command before passing the input stream.

The Basic Command

To accept audio from a pipe, tell FFmpeg to read from stdin by specifying -i - (a hyphen representing the pipe) along with the raw audio parameters:

cat input.raw | ffmpeg -f s16le -ar 44100 -ac 2 -i - output.flac

Or, when piping directly from another command or application:

your_audio_source | ffmpeg -f s16le -ar 44100 -ac 2 -i - output.flac

Required Input Parameters

Because raw PCM lacks container metadata, the following flags must be placed before -i -:

  • -f <format> (Format): Specifies the sample format and endianness.
    • s16le: Signed 16-bit Little-Endian (Standard CD audio).
    • s24le: Signed 24-bit Little-Endian.
    • s32le: Signed 32-bit Little-Endian.
    • f32le: 32-bit floating-point Little-Endian.
    • (Run ffmpeg -formats to view all available raw PCM demuxers).
  • -ar <rate> (Sample Rate): Specifies the frequency in Hertz (e.g., 44100 for 44.1 kHz, 48000 for 48 kHz).
  • -ac <channels> (Channels): Specifies the number of audio channels (e.g., 1 for mono, 2 for stereo).
  • -i -: Instructs FFmpeg to read the raw data stream from standard input.

Advanced Examples

24-bit, 48 kHz Studio Quality

If your source generates 24-bit audio at 48,000 Hz in stereo:

generate_audio | ffmpeg -f s24le -ar 48000 -ac 2 -i - -c:a flac output.flac

Adjusting FLAC Compression Level

FLAC compression levels range from 0 (fastest encoding, larger file size) to 12 (slowest encoding, smallest file size). The default level is 5. You can specify the compression level using -compression_level:

cat audio.pcm | ffmpeg -f s16le -ar 44100 -ac 2 -i - -c:a flac -compression_level 8 output.flac

Piping from ALSA Recording Tool (arecord)

You can capture live microphone input via arecord and encode it directly to FLAC:

arecord -f cd -t raw | ffmpeg -f s16le -ar 44100 -ac 2 -i - output.flac