Dump Live RTMP Stream to FLV Using FFmpeg

Saving a live Real-Time Messaging Protocol (RTMP) stream directly into a Flash Video (FLV) container is straightforward with FFmpeg. Because RTMP natively encapsulates video and audio in the FLV format, you can stream-copy the media without re-encoding, preserving the original quality while utilizing minimal CPU resources. This guide covers the exact FFmpeg commands needed to capture, dump, and handle real-world connection issues when saving RTMP streams to disk.

The Basic Command

To dump a live RTMP stream directly to an FLV file, run:

ffmpeg -i "rtmp://example.com/live/stream_key" -c copy output.flv

Command Breakdown

  • -i "rtmp://...": Specifies the live RTMP source URL. Always wrap the URL in quotes to prevent special characters or tokens from breaking the terminal shell command.
  • -c copy: Tells FFmpeg to perform a direct stream copy of both audio and video without re-encoding. This guarantees no loss in fidelity and near-zero CPU overhead.
  • output.flv: The destination file. FFmpeg automatically infers the FLV container format from the .flv extension. If needed, you can explicitly enforce the container format by appending -f flv output.flv.

Handling Connection Drops

Live streams often suffer from packet drops or network interruptions. To ensure FFmpeg attempts to reconnect without terminating the recording prematurely, use the -reconnect options:

ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 5 -i "rtmp://example.com/live/stream_key" -c copy output.flv
  • -reconnect 1: Enables automatic reconnection if the connection drops unexpectedly.
  • -reconnect_at_eof 1: Reconnects even if an unexpected end-of-file marker is received.
  • -reconnect_streamed 1: Instructs FFmpeg that the input is a live stream.
  • -reconnect_delay_max 5: Limits the maximum wait time before attempting a reconnect to 5 seconds.

Listening to an Incoming RTMP Stream

If an external encoder (such as OBS Studio) is pushing an RTMP stream directly to your local machine, configure FFmpeg to act as an RTMP listener:

ffmpeg -listen 1 -i "rtmp://0.0.0.0:1935/live/stream_key" -c copy output.flv
  • -listen 1: Opens a listening socket on the specified port (typically 1935) to accept the incoming RTMP push, immediately dumping the received payloads into the FLV container.

Stopping the Capture

To cleanly close the output file and write all container metadata, press q or Ctrl + C in your terminal window. This ensures the output FLV file is closed properly and remains playable.