Pipe Raw Video to a Network Socket Using FFmpeg

Piping raw video to a network socket using FFmpeg is an efficient method for low-latency video transmission, real-time processing, and local network streaming. This article provides a direct, step-by-step guide on how to configure FFmpeg to output uncompressed raw video data and stream it over TCP or UDP protocols to a receiving socket.

Choosing Between TCP and UDP

Before streaming, you must select the appropriate network protocol for your use case:

  • TCP (Transmission Control Protocol): Best for reliable transmission where no packet loss is acceptable, though it introduces slightly more latency.
  • UDP (User Datagram Protocol): Best for real-time, ultra-low-latency streaming where occasional packet loss is acceptable.

Piping Raw Video Over TCP

To pipe raw video over TCP, you must first set up a listener to receive the stream, or configure FFmpeg to listen for an incoming connection.

Method 1: FFmpeg Connects to a Listening Socket

First, start a listener on the receiving machine using a tool like netcat to save the incoming raw stream:

nc -l -p 12345 > received_video.yuv

Next, run the FFmpeg command to convert an input file to raw video and send it to the listening socket:

ffmpeg -i input.mp4 -f rawvideo -pix_fmt yuv420p tcp://127.0.0.1:12345

Method 2: FFmpeg Acts as the TCP Server

You can also configure FFmpeg to listen for an incoming connection by appending the ?listen parameter to the URL:

ffmpeg -i input.mp4 -f rawvideo -pix_fmt yuv420p tcp://127.0.0.1:12345?listen

On the receiving end, you can connect and read the raw stream using another FFmpeg instance or a custom application:

ffmpeg -f rawvideo -pixel_format yuv420p -video_size 1920x1080 -framerate 30 -i tcp://127.0.0.1:12345 output.mp4

Piping Raw Video Over UDP

UDP is connectionless, meaning FFmpeg can begin streaming immediately without waiting for a receiver to connect.

To stream raw video over UDP to a specific destination IP and port, use the following command:

ffmpeg -i input.mp4 -f rawvideo -pix_fmt yuv420p udp://127.0.0.1:12345

To receive and play this UDP stream in real-time using FFplay, run the following command on the receiving machine (note that because raw video has no headers, you must explicitly define the pixel format, resolution, and framerate):

ffplay -f rawvideo -pixel_format yuv420p -video_size 1920x1080 -framerate 30 udp://127.0.0.1:12345

Key Parameter Explanations

  • -f rawvideo: Forces FFmpeg to use the raw video muxer, stripping away all container headers and metadata.
  • -pix_fmt yuv420p: Defines the pixel format. YUV420p is highly compatible, but you can also use rgb24 or yuyv422 depending on your requirements.
  • -video_size / -pixel_format / -framerate: These flags are mandatory on the receiving side because raw video streams do not contain container headers to describe the video geometry or timing.