Write Raw Video to a Named Pipe with FFmpeg

This guide explains how to stream uncompressed raw video frames from FFmpeg into a Unix named pipe (FIFO). Using a named pipe allows you to pass video data directly into another process—such as a custom C/C++, Python, or OpenCV program—in real time without writing intermediate files to disk. You will learn how to create the FIFO, configure FFmpeg to output raw frames in a specific pixel format, and handle the data stream effectively.

1. Create the Named Pipe

In Unix-like systems, create a FIFO using the mkfifo command:

mkfifo /tmp/video_pipe

A named pipe acts like a regular file on the filesystem, but it functions entirely in memory. It blocks execution until both a reader and a writer have opened the pipe.

2. Output Raw Video with FFmpeg

To write raw, uncompressed frames into the FIFO, tell FFmpeg to use the rawvideo muxer and define an explicit pixel format (such as rgb24 or yuv420p).

Run the following command:

ffmpeg -re -i input.mp4 -f rawvideo -pix_fmt rgb24 -y /tmp/video_pipe

Key Arguments Explained:

  • -re: Reads the input at its native frame rate (real-time). Omit this flag if you want FFmpeg to process frames as fast as possible.
  • -i input.mp4: Specifies the input source (file, network stream, or device).
  • -f rawvideo: Forces the output format to be uncompressed raw video frames without headers, containers, or metadata.
  • -pix_fmt rgb24: Sets the color format. rgb24 produces 3 bytes per pixel (Red, Green, Blue). You can also use formats like yuv420p or bgr24 depending on what your consumer application expects.
  • -y /tmp/video_pipe: Overwrites the destination (opens the pipe for writing).

3. Reading the Frames

Because raw video contains no headers, the consuming application must know the exact frame width, height, and pixel format in advance to properly slice the byte stream.

To determine frame size in bytes:

  • RGB24 / BGR24: Width × Height × 3
  • RGBA / BGRA: Width × Height × 4
  • YUV420p: Width × Height × 1.5

For a 1920×1080 video using rgb24: \[\text{Frame Size} = 1920 \times 1080 \times 3 = 6,220,800\text{ bytes}\]

Python Reader Example

import os

pipe_path = "/tmp/video_pipe"
width, height = 1920, 1080
frame_size = width * height * 3

with open(pipe_path, "rb") as pipe:
    while True:
        raw_frame = pipe.read(frame_size)
        if len(raw_frame) < frame_size:
            break  # End of stream
        # Process the frame (e.g., numpy.frombuffer(raw_frame, dtype='uint8'))

Always launch your reader program before or concurrently with the FFmpeg writer command to avoid hanging terminal processes.

4. Cleanup

When streaming is finished, remove the FIFO from the filesystem using rm:

rm /tmp/video_pipe