Stream Video Over UDP Unicast Using FFmpeg

Streaming a local video file to a specific IP address over UDP unicast is a fast and lightweight way to transmit media across a local network using FFmpeg. This guide covers the exact command required to transmit your file in real time, explains the essential flags needed for proper playback, and demonstrates how to receive and verify the stream on the destination device.

The Basic Streaming Command

To stream a video file over UDP unicast without re-encoding, open your terminal and run:

ffmpeg -re -i input.mp4 -c copy -f mpegts udp://192.168.1.50:1234

Replace input.mp4 with the path to your video file, 192.168.1.50 with the target receiver's IP address, and 1234 with your preferred destination port.

Command Breakdown

  • -re: Forces FFmpeg to read the input at its native frame rate. Without this flag, FFmpeg processes and sends the file as fast as the CPU can handle, overwhelming network buffers and breaking real-time playback.
  • -i input.mp4: Specifies the source video file.
  • -c copy: Copies the existing audio and video codecs directly into the output without re-encoding, keeping CPU usage minimal.
  • -f mpegts: Sets the output container format to MPEG Transport Stream (MPEG-TS). UDP does not natively handle file containers like MP4, making MPEG-TS the standard container for raw UDP video delivery.
  • udp://192.168.1.50:1234: Defines the network protocol, target unicast IP, and destination port.

Re-encoding for Network Reliability

If your source file uses codecs incompatible with MPEG-TS, or if you need to constrain the bitrate to avoid packet loss over Wi-Fi, transcode the stream on the fly:

ffmpeg -re -i input.mp4 -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k -c:a aac -b:a 128k -f mpegts "udp://192.168.1.50:1234?pkt_size=1316"
  • -preset veryfast: Minimizes encoding latency.
  • -b:v, -maxrate, -bufsize: Enforces a constant bitrate (CBR) profile, preventing network throughput spikes.
  • pkt_size=1316: Configures FFmpeg to fit exactly seven 188-byte MPEG-TS packets into a single 1500-byte MTU Ethernet frame, preventing packet fragmentation.

Receiving the Stream

To verify the transmission on the receiving machine (192.168.1.50), ensure your firewall allows UDP traffic on the chosen port, then test with one of the following methods:

Using FFplay:

ffplay -i udp://0.0.0.0:1234

Using VLC Media Player:

  1. Open VLC and go to Media > Open Network Stream.
  2. Enter the URL: udp://@:1234
  3. Click Play.