How to Pipe Raw Audio into FFmpeg on macOS
Piping raw audio from an external encoder into FFmpeg on macOS is an efficient way to stream, transcode, or record real-time audio without saving intermediary files to your disk. Because raw audio lacks header metadata, you must explicitly define the audio properties when passing the stream to FFmpeg. This guide provides a direct, step-by-step method to pipe raw pulse-code modulation (PCM) audio from an external command-line encoder or capture tool directly into FFmpeg using standard input (stdin).
The Basic Command Structure
To pipe raw audio into FFmpeg, you use the pipe operator
(|) in the macOS Terminal. You must instruct FFmpeg to read
from standard input using -i - (or -i pipe:0)
and manually define the input audio parameters before the input
flag.
The basic syntax is:
[external_encoder_command] | ffmpeg -f [format] -ar [sample_rate] -ac [channels] -i - [output_options] output.extStep-by-Step Implementation
1. Define Your Input Parameters
Since raw audio has no headers to tell FFmpeg its format, you must
specify the following parameters before -i -: *
-f (Format): The sample format of the raw
audio. Common formats include s16le (signed 16-bit
little-endian), s32le (signed 32-bit little-endian), or
f32le (32-bit floating-point little-endian). *
-ar (Audio Rate): The sample rate in Hz
(e.g., 44100 or 48000). *
-ac (Audio Channels): The number of
channels (e.g., 1 for mono, 2 for stereo).
2. Run the Pipe Command
Below is a practical example using a generic external encoder command that outputs raw 16-bit little-endian, 44.1 kHz, stereo audio, piping it into FFmpeg to encode as an AAC file:
external_encoder --output-raw | ffmpeg -f s16le -ar 44100 -ac 2 -i - -c:a aac output.m4a3. macOS Example Using SoX (Sound eXchange)
If you are capturing live audio on macOS using a command-line tool like SoX (installed via Homebrew), you can pipe the raw output directly into FFmpeg:
rec -t raw -r 48000 -c 2 -b 16 -e signed-integer - | ffmpeg -f s16le -ar 48000 -ac 2 -i - output.mp3In this command: * rec captures system/microphone audio
and outputs raw PCM to stdout (-). * ffmpeg
reads the raw 16-bit signed-integer, 48 kHz, stereo stream from stdin
(-i -). * FFmpeg compresses the stream into
output.mp3.
Troubleshooting Buffer and Latency Issues
When piping real-time raw audio, you may encounter buffer underruns or latency. You can optimize the process using these optional FFmpeg flags:
-blocksize: Set this before the input to define the size of the buffer blocks FFmpeg reads from the pipe (e.g.,-blocksize 4096).-fflags nobuffer: Reduces latency by analyzing input streams without buffering.-max_delay: Limits the maximum delay in microseconds for processing input.