resolutionh.264frame-ratempeg2-ts

How to find resolution and framerate values in H.264 MPEG-2 TS?


I'm working on a MPEG-2 TS video containing a H.264 stream, and I'm looking for video properties stored in the stream, by scanning PAT, PMT, PES, etc.

I'm able to read PAT, PMT, and elementary streams type and PID. Here I would like to find the resolution and the framerate (fps). Are they located in the PES header, or elsewhere? They are not in PAT or PMT.

Below, Transport Stream Packet Editor is able to find two different informations, one itself and the other from the Haali Media Decoder helper codec. How to get the first one:

enter image description here

Pseudo-code is welcomed.


Solution

  • I am not sure about the availability of the height width information in the MPEG2TS header. Because TS file can have multiple programs. But if you are targeting only TS files made of H.264 elementary stream then you an get these informations from the SPS of the H.264 elementary stream.

    Every H.264 frame starts with four or three bytes sequence header 0x00 0x00 0x01 or 0x00 0x00 0x00 0x01. The frame is an SPS frame if doing the AND operation with next byte after start headers is equal to 0x07.

    E.g. SPS frame 0x00 0x00 0x00 0x01 0x67 ... Doing AND operation (0x67 & 0x1F) = 0x07

    Parsing the SPS header is also not an easy task but you can find the details in ffmpeg source code.

    Hope this helps.