In this article I'd like to introduce mpeg-ts-analyzer, an open-source tool I have been building to analyze MPEG-2 Transport Streams.

mpeg-ts-analyzer on GitHub

This is what it looks like. First, the command-line tool:

Terminal demo of the CLI checks

And the desktop GUI:

The GUI: Overview, Timeline, Bitrate, Packets and Detailed inspection

Why I built it

I used to work at a video equipment manufacturer. There, we used a commercial library, MainConcept, to create streams. Depending on how we tuned its parameters, such as the VBV buffer settings, a stream could end up outside the MPEG-2 TS specification. To find out why, I had to understand MPEG-2 TS in detail and look closely inside the streams.

Go was getting a lot of attention at that time. So in 2016 I started writing this tool, partly to learn Go as well.

The problem: PCR, DTS and PTS

An MPEG-2 TS cuts video and audio into small 188-byte packets and mixes them into one stream, at a fine enough grain that both keep arriving a little at a time. Timestamps then control when each frame is decoded and shown, using the PCR as the reference time. This is how video and audio play without drifting apart:

  • PCR (Program Clock Reference): the reference time. It is a sample of the program clock, sent regularly so that the decoder can recover and synchronize its own clock.
  • DTS (Decoding Time Stamp): the time, on that clock, when a frame must be decoded.
  • PTS (Presentation Time Stamp): the time, on that clock, when a frame is shown or a piece of audio is played. Because video and audio follow the same clock, they stay in sync.

PCR, DTS and PTS in one picture

The problem I had was the gap between the PCR and the DTS/PTS. When a frame's data arrives, the clock recovered from the PCR gives the time of its arrival. The frame's DTS (or its PTS, if it has no DTS) must not be more than 1000 ms after that time. For ordinary video and audio, the specification does not allow data to stay in the decoder's buffer any longer than that.

Depending on the encoder settings, this gap sometimes went over 1000 ms. The decoder we were targeting was strict about this limit, so we had to keep this gap within 1000 ms.

The picture below shows the two cases. It is simplified: the arrival of a frame is drawn as a single moment.

The gap between the PCR and the DTS

The first version: a CLI and bit-level dumps

The first version was a command-line tool. From the start, my goal was to get the largest gap between the PCR and the DTS/PTS in a stream. So the tool printed the max gap, and also the max PCR interval. To see where a large gap came from, it could also print each PCR and DTS (or PTS) value:

0x000034e0 PCR: 0x018344a0[00940.000000ms] 0x000034e0 DTS: 0x00024090[01640.000000ms] (pid:0x100) (delay:700.000000ms) 0x0000359c PTS: 0x00023a3a[01621.977778ms] (pid:0x101) (delay:681.977778ms) 0x00004970 PCR: 0x01a43a20[01020.000000ms] (Interval:00080.000000ms) 0x00004970 DTS: 0x00025cb0[01720.000000ms] (pid:0x100) (delay:700.000000ms) 

"delay" is the gap in the picture: how far the DTS or PTS is ahead of the PCR clock when the data arrives. "Interval" is the time from the previous PCR.

Later, the CLI came to show the maximum values with a pass/fail verdict, like this:

Compliance Check Results: Max PCR interval: 80.000000ms [OK, limit: <= 100.000000ms] PCR discontinuities: 0 undeclared in 60 PCRs [OK] PCR-PTS/DTS max gap: 726.666667ms [OK, limit: <= 1000.000000ms] 

"PCR-PTS/DTS max gap" checks the gap in the picture against the 1000 ms limit, and "Max PCR interval" checks that consecutive PCR values are no more than 100 ms apart. So I can see at once whether the stream is within the limits.

I wrote the bit-level parser myself. Fields in MPEG-2 TS have odd sizes, like 1, 3, 13 or 33 bits. Many of them start in the middle of a byte and cross byte boundaries, and the parser often has to skip reserved bits. So I wrote a small bit reader that keeps a bit position, and reads a field in three parts: the head (the rest of the current byte), the middle (whole bytes) and the tail (the top bits of the last byte). Then it joins them into one value.

Reading a field that crosses byte boundaries in three parts

A timestamp is only correct if the parser that reads it is correct. To check my parser, I also added dump options that print the parsed result of each header, field by field, so that I could compare it with the bits of the packet:

=============================================================== TS Header =============================================================== transport_error_indicator : 0 payload_unit_start_indicator : 1 transport_priority : 1 pid : 0x100 transport_scrambling_control : 0 adaptation_field_control : 1 continuity_counter : 0 

This was enough for my original goal, so for several years I did not change the tool much.

Then AI came along

Today I work as a web engineer, and I don't have much time for this project. But when AI-assisted development became popular, I found that I could finally do what I had wanted to do, in a small amount of time. Since I started using AI in 2026, the project has had more than 300 commits. In the seven years before that, it had about 60.

I used Claude Code and Codex, and let them develop and review each other's work. Reference tools such as TSDuck already exist, so I compared new measurements with an independent implementation wherever I could, and confirmed that the results were valid. So I could move fast with AI without giving up on accuracy.

Now the tool can check whether a stream follows the specification from many angles, not only the timestamps. For example:

  • continuity counters of every PID
  • PAT/PMT repetition, CRC and version changes
  • PIDs referenced by the PAT or PMT but missing from the stream, and PIDs that are not referenced
  • PES headers and PTS repetition
  • sync loss, transport errors and scrambled packets

A GUI would clearly make all of this easier to see. But building one takes a lot of work, and I did not have enough free time outside my job, so I had given up on it. With AI, I could build it in a short time, in Rust with egui, an immediate-mode GUI library for Rust that runs on macOS, Windows and Linux. The GUI itself took only about one week of actual work.

What it can do now

The CLI shows all the checks listed above in one report. The same results are available as JSON with a published schema, so you can use it in CI. The old dumps are still there, too.

Bit-level inspection in a GUI

The GUI runs the CLI and shows the result in four linked views: Overview, Timeline, Bitrate and Packets.

The GUI with its Overview, Timeline, Bitrate and Packets views

The part I like most is Detailed inspection. It lays out one packet bit by bit, in the same way ISO/IEC 13818-1 draws its syntax tables. This is the view I wanted back when I was writing those field-by-field dumps.

Detailed inspection of a PMT packet

You can see every bit of the packet, including reserved bits, marker bits, prefixes, stuffing, and bytes the analyzer does not decode. So you can check the data side by side with the specification. I think this makes checking much easier while you are still learning the specification.

For monitoring a whole service or deep conformance work, TSDuck is the better tool. This tool is for looking closely at a small part of a stream.

Try it

I recommend starting with the GUI. There are installers and binaries for macOS, Windows and Linux on the releases page, and the CLI is available there as well. See the installation guide for the details. To learn how to use it, see the GUI guide and the CLI usage guide.

If you do not have a TS file at hand, the repository has sample streams, including some with intentional errors.

Feedback welcome

If this tool helps someone who has the same kind of problem I had, I will be happy. I would especially like to hear from people who work with transport streams. If you find a wrong result, or have an idea for a useful check or feature, please open an issue on GitHub.

mpeg-ts-analyzer on GitHub