Pipe Raw PCM Audio to FFmpeg and Transcode to FLAC

This guide explains how to stream raw PCM audio data directly into FFmpeg using standard input (stdin) and transcode it into the lossless FLAC format. Because raw PCM data lacks header information, you must explicitly define the input audio properties—such as sample rate, bit depth, and channel count—for FFmpeg to interpret and convert the stream correctly.

To pipe raw PCM data into FFmpeg, you send the byte stream to stdin and instruct FFmpeg to read from the pipe using -i pipe:0 (or simply -i -). Since raw PCM does not contain metadata headers, you must place the input format specifiers before the input flag in the command.

Here is the template for the command:

[your_pcm_generator] | ffmpeg -f s16le -ar 44100 -ac 2 -i pipe:0 output.flac

Command Breakdown

  • -f s16le: Defines the input format. In this case, s16le stands for signed 16-bit little-endian PCM. Change this to match your source data (e.g., s32le for 32-bit, or f32le for 32-bit float).
  • -ar 44100: Sets the audio sample rate of the input stream to 44.1 kHz. Adjust this to match your source (e.g., 48000 for 48 kHz).
  • -ac 2: Specifies the number of audio channels. Use 1 for mono, 2 for stereo, or more depending on your input.
  • -i pipe:0 (or -i -): Tells FFmpeg to read the input from standard input (stdin) instead of a physical file.
  • output.flac: The output filename. FFmpeg automatically detects the .flac extension and applies the lossless FLAC encoder.

Real-World Example using cat

If you have a raw PCM file named audio.raw containing 16-bit stereo PCM at 48kHz, you can pipe it and transcode it like this:

cat audio.raw | ffmpeg -f s16le -ar 48000 -ac 2 -i - output.flac

This method allows you to seamlessly integrate FFmpeg into audio processing pipelines, enabling real-time compression from programming languages, scripts, or other command-line audio tools.