How to Stream MPEG-TS with KLV over UDP using FFmpeg

This article provides a practical guide on how to construct and run an FFmpeg command to stream an MPEG-TS (MPEG Transport Stream) containing synchronized Key-Length-Value (KLV) metadata over a UDP network. You will learn how to map both video and data streams correctly, ensure real-time transmission, and configure the stream output to preserve the KLV packet structure.

Streaming an Existing File with Video and KLV Metadata

If you already have a file (such as an .ts or .mpg file) that contains both a video track and a KLV metadata track, you can stream it over UDP using the following FFmpeg command:

ffmpeg -re -i input.ts -map 0:v -map 0:d -c copy -f mpegts "udp://239.0.0.1:1234?pkt_size=1316"

Here is a breakdown of the critical parameters used in this command:


Multiplexing and Streaming Separate Video and KLV Files

If your video stream and KLV metadata are in separate sources (for example, an MP4 video file and a raw binary KLV stream), you can multiplex them into a single MPEG-TS stream on the fly:

ffmpeg -re -i video.mp4 -re -i klv_data.bin -map 0:v -map 1:d -c:v libx264 -c:d copy -f mpegts "udp://192.168.1.50:5000?pkt_size=1316"

Key differences for this configuration include: