Capture RTMP Stream to stdout with FFmpeg
This article explains how to capture a live RTMP network stream using
FFmpeg and pipe the raw media payload directly to stdout
(standard output). By sending the stream directly to stdout, you can
feed live video data into other command-line applications, custom
scripts, or media players in real-time without writing temporary files
to your disk.
To capture an RTMP stream and output it directly to stdout, use the following basic FFmpeg command structure:
ffmpeg -i rtmp://your_server_address/live/stream_key -c copy -f mpegts pipe:1Here is a breakdown of what each parameter does in this command:
-i rtmp://your_server_address/live/stream_key: Specifies the input URL of the live RTMP stream you want to capture.-c copy: Tells FFmpeg to copy both the audio and video streams directly without re-encoding them. This minimizes CPU usage and preserves the original quality of the stream.-f mpegts: Forces the output format. While RTMP natively uses the FLV container,mpegts(MPEG Transport Stream) is highly recommended when piping video over stdout because it is designed for streaming and can be easily parsed by downstream applications even if the stream starts mid-frame.pipe:1(or-): Directs FFmpeg to write the output data to standard output (stdout) instead of a physical file.
Real-World Examples
1. Piping to a Media Player (ffplay) You can verify
your stdout pipe is working by sending the stream directly to another
media player like ffplay:
ffmpeg -i rtmp://example.com/live/stream -c copy -f mpegts pipe:1 | ffplay -2. Piping to a Custom Script or Tool If you are processing the stream with a custom Python or Node.js script, or a tool like WebRTC broadcasters, you can read the binary data directly from the system’s standard input:
ffmpeg -i rtmp://example.com/live/stream -c copy -f mpegts - | python3 process_stream.pyAlternative Formats
If your downstream application strictly requires the original FLV container format instead of MPEG-TS, you can change the format flag:
ffmpeg -i rtmp://example.com/live/stream -c copy -f flv -