Save UDP Stream to Disk Without Re-encoding with FFmpeg

Capturing a live UDP network stream and saving it directly to local storage using FFmpeg without re-encoding is an efficient way to preserve exact video quality while consuming minimal CPU resources. By instructing FFmpeg to bypass the decoding and encoding pipelines and perform a direct stream copy, you can record high-bitrate broadcasts, IP cameras, or MPEG-TS feeds reliably. This guide demonstrates the fundamental command structure, key configuration flags, and best practices to prevent packet loss during recording.

The Basic Command

To record an incoming UDP stream directly to a file without re-encoding, use the -c copy flag:

ffmpeg -i "udp://@0.0.0.0:1234" -c copy output.ts

Command Breakdown

  • -i "udp://@0.0.0.0:1234": Specifies the input protocol and socket. The @ symbol binds FFmpeg to a local network interface or port. Using 0.0.0.0:1234 listens on port 1234 across all available network interfaces. If receiving a multicast stream, replace 0.0.0.0 with the multicast IP (e.g., udp://@239.255.1.1:1234).
  • -c copy: Tells FFmpeg to copy the audio, video, and subtitle streams directly from the source packets into the output container without decoding and re-encoding.
  • output.ts: The destination file. Using the MPEG Transport Stream (.ts) container is standard for UDP captures because it does not require a finalized header or index table, meaning the file remains playable even if the capture process terminates abruptly.

Preventing Packet Drops and Buffer Overruns

UDP does not have built-in retransmission or flow control. If the system buffer fills up faster than FFmpeg can write to disk, incoming packets are dropped. To mitigate packet loss, increase the UDP receive buffer size and configure buffer overrun behavior using query parameters:

ffmpeg -i "udp://@0.0.0.0:1234?overrun_nonfatal=1&fifo_size=2867200" -c copy output.ts
  • fifo_size=2867200: Sets the size of the internal circular buffer (FIFO) in packets (each UDP packet is typically 188 bytes for MPEG-TS). A higher value provides a safety net during temporary disk write spikes.
  • overrun_nonfatal=1: Prevents FFmpeg from crashing if a buffer overrun occurs, allowing it to recover and continue recording.

Choosing the Output Container

While .ts is the most resilient container for live stream recording, you can also output to .mp4 or .mkv if required:

  • Matroska (.mkv): Supports almost all codecs and recovers well from unexpected interruptions:

    ffmpeg -i "udp://@0.0.0.0:1234" -c copy output.mkv
  • MP4 (.mp4): Standard MP4 requires a "moov atom" written at the end of the recording. If the stream stops abruptly, the file becomes corrupted. To safely stream-copy directly into MP4, enable fragmented MP4 mode:

    ffmpeg -i "udp://@0.0.0.0:1234" -c copy -movflags frag_keyframe+empty_moov output.mp4