Search code examples
pascalmpeg-2timecodes

Read MPEG-2 timecode


I'm using some old code from many years ago to calculate the duration of MPEG-2 videos. It seems something has changed as I'm getting 1 hour and 49 seconds for a 49 second video.

The last GOP Header is found in the file and the 4 byte TimeCode is extracted "040E2AC0". In a 32 bit LongWord, this reads in little endian as 3223981572.

The endian is then swapped using an assembler function bswap eax. The result is 68037312. Some Pascal code then extracts the duration like this...

Hours   := (TimeCode shr 26) and $1F;
Minutes := (TimeCode shr 20) and $3F;
Seconds := (TimeCode shr 13) and $3F;

Hours is 1 but should be 0. Minutes is correct at 0. Seconds is correct at 49.

I'm not good with bit manipulation to debug the issue. Is there something wrong with this? I could arrange a link to the video if that would help.

Many thanks, Ross.


Solution

  • Posting your file was a good idea.

    If you look at the first GOP in the file, it has byte values of 04 08 00 40 (and the last GOP has 04 0E 2A C0 as you said)

    When the first GOP is run through your procedure the result is 01:00:00, that is 1 hour exactly.

    I did not find any spec for this but obviously the length is the difference between first and last GOP.