How to Output Raw Video to Stdout with FFmpeg
This article explains how to use FFmpeg to decode and stream uncompressed, raw video frames directly to standard output (stdout). By defining the raw video multiplexer, specifying the pixel format, and targeting stdout via the pipe protocol, you can route frame data directly into external scripts, network sockets, or other command-line tools without writing temporary files to disk.
The Basic Command
To direct raw video data to stdout, specify the rawvideo
format using the -f flag and set the output destination to
- (or pipe:1):
ffmpeg -i input.mp4 -f rawvideo -pix_fmt rgb24 -Essential Flags and Options
Because raw video contains no container or header metadata, the receiving process needs strict consistency in frame layout. Use the following flags to control the output:
-f rawvideo: Forces the output format to be a sequence of raw, uncompressed video frames without container headers.-pix_fmt <format>: Explicitly sets the pixel layout for the output. Common options include:rgb24: 3 bytes per pixel (Red, Green, Blue interleaved).bgr24: 3 bytes per pixel (Blue, Green, Red interleaved, common in OpenCV).rgba: 4 bytes per pixel including the alpha channel.yuv420p: Standard planar YUV format commonly used in video codecs.
-orpipe:1: Directs the output stream to standard output.-v erroror-loglevel quiet: Suppresses FFmpeg's standard logging messages. While log messages usually output tostderr, suppressing them prevents accidental stream contamination or excessive console spam.-nostats: Disables encoding progress stats, ensuring only binary data is emitted.
Cleaner Piping Example
To pipe raw frames directly into a Python script or another binary processing tool cleanly:
ffmpeg -v error -i input.mp4 -f rawvideo -pix_fmt rgb24 - | python process_frames.pyResizing and Adjusting Framerate
Since reading raw video requires knowing the exact frame dimensions, you can enforce specific frame sizes and rates using output filters:
ffmpeg -i input.mp4 -vf "scale=640:480,fps=30" -f rawvideo -pix_fmt rgb24 -v error -Interpreting the Output Stream
When consuming stdout from this command, read the exact number of bytes corresponding to one frame per iteration:
\[\text{Bytes Per Frame} = \text{Width} \times \text{Height} \times \text{Bytes Per Pixel}\]
For a 640x480 frame formatted as rgb24, each frame
consists of \(640 \times 480 \times 3 =
921,600\text{ bytes}\). The consumer should repeatedly read this
fixed byte block until EOF (end of file) is reached.