Extract KLV Metadata from MPEG-TS Using FFmpeg

This article provides a straightforward guide on how to extract Key-Length-Value (KLV) metadata from an MPEG-TS (.ts) video stream and save it to a text file using FFmpeg and FFprobe. You will learn the exact command-line instructions required to isolate the data stream, map it correctly, and export it into either raw binary format or a readable text format.

Understanding the KLV Stream

In an MPEG-TS container, KLV metadata (often used for MISB drone telemetry) is typically stored in a timed metadata stream, separate from the video and audio tracks. To extract this data, you must identify and target the data stream (often represented by the d specifier in FFmpeg).

Method 1: Extract Raw KLV Metadata to a File

To extract the raw KLV payload from the MPEG-TS file without re-encoding, use the following FFmpeg command. This copy-demuxes the data stream directly into a binary or text file:

ffmpeg -i input.ts -map 0:d -codec copy -f rawvideo output.klv

Command Breakdown: * -i input.ts: Specifies the input MPEG-TS video file. * -map 0:d: Selects all data streams (d) from the first input file (0). This excludes video and audio streams. * -codec copy: Copies the data stream stream directly without re-encoding. * -f rawvideo: Forces the output format to raw data. * output.klv: The destination file where the raw KLV packets will be written (you can also use .txt or .bin).

Method 2: Extract KLV Metadata as Human-Readable Text (JSON)

Because raw KLV data is binary, opening it in a standard text editor will show unreadable characters. To convert and write this metadata into a structured, readable text format (like JSON or XML), use ffprobe, which is included with the FFmpeg suite:

ffprobe -v error -select_streams d -show_packets -show_data_hash ASC -of json input.ts > output.json

Command Breakdown: * -v error: Suppresses unnecessary log messages, keeping the output clean. * -select_streams d: Selects only the data stream containing the KLV metadata. * -show_packets: Instructs ffprobe to display packet configuration and data payloads. * -show_data_hash ASC: Shows the payload data in an ASCII/Hex readable layout. * -of json: Formats the output as a JSON structure. * > output.json: Redirects the command-line output into a text file named output.json.