Read from a Named Pipe in FFmpeg
Reading from a named pipe (FIFO) allows FFmpeg to ingest streaming video or audio data generated by another process in real time without creating intermediary disk files. This guide demonstrates how to create a named pipe on Unix-like systems and Windows, feed data into it, and instruct FFmpeg to read the pipe as an input source using the correct flags to prevent hanging and format-detection errors.
1. Creating and Reading a Named Pipe on Linux and macOS
On Unix-like operating systems, named pipes are created using the
mkfifo utility. Once created, they behave like standard
files in the filesystem, but transfer data entirely in memory.
Step 1: Create the Pipe
Run the following command to create a FIFO buffer:
mkfifo /tmp/stream_pipeStep 2: Read with FFmpeg
Because pipes are non-seekable streams, FFmpeg may struggle to detect
the file format automatically. You should explicitly declare the
incoming container or codec format using the -f flag:
ffmpeg -f mpegts -i /tmp/stream_pipe -c copy output.mp4Step 3: Write Data to the Pipe
A named pipe blocks until both a reader and a writer are connected. In a separate terminal or background job, push data into the pipe:
cat input_source.ts > /tmp/stream_pipeOnce the writer begins sending bytes, FFmpeg immediately starts processing the input stream.
2. Handling Raw Video or Audio Inputs
If the data being piped does not have headers (such as raw video
frames or raw PCM audio), you must define the input specifications
before the -i parameter so FFmpeg knows how to interpret
the stream.
Example: Raw YUV Video
ffmpeg -f rawvideo -pixel_format yuv420p -video_size 1920x1080 -framerate 30 -i /tmp/stream_pipe output.mp4Example: Raw PCM Audio
ffmpeg -f s16le -ar 44100 -ac 2 -i /tmp/stream_pipe output.wav3. Reading Named Pipes on Windows
Windows uses a different naming convention for pipes, formatted as
\\.\pipe\<pipename>. FFmpeg supports these paths
natively.
To read from an existing Windows named pipe:
ffmpeg -f mpegts -i \\.\pipe\my_pipe -c copy output.mp44. Key Flags for Non-Seekable Streams
Named pipes do not allow random access (seeking). The following flags help resolve common issues when piping into FFmpeg:
Explicit Format (
-f <format>): Essential for formats that do not have self-describing container headers, or to prevent FFmpeg from waiting indefinitely while trying to probe the input.Probing Limits (
-probesizeand-analyzeduration): Reduces latency when starting the process by limiting how much data FFmpeg inspects before starting the decode or remux:ffmpeg -probesize 32k -analyzeduration 0 -f mpegts -i /tmp/stream_pipe output.mp4Thread Queue Size (
-thread_queue_size): If the writing process bursts data faster than FFmpeg consumes it, increase the queue buffer to prevent packet dropping:ffmpeg -thread_queue_size 512 -f mpegts -i /tmp/stream_pipe output.mp4