2

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.

Ross
  • 157
  • 9
  • It would be helpful to see 1) the actual value the file contains; 2) the code that results in *the endian being reversed*; and 3) the value after that reversal (`TimeCode`). It also almost always helps to include a tag for the specific language you're using when asking about why code isn't working. A link to the video would be irrelevant; if that link disappears in the future, it wouldn't have any value to the question. – Ken White Jun 30 '16 at 02:33
  • OK. Keep in mind that you've not made an [edit] to include the information I asked you to provide as well. :-) – Ken White Jul 02 '16 at 04:25
  • I was making the edits as you made your comment. That's done. Please let me know what you think. – Ross Jul 02 '16 at 04:44
  • With a time code of `040E2AC0` the hour value is pretty clearly `1`, and your calculation looks correct (at least according to [this documentation](http://dvd.sourceforge.net/dvdinfo/mpeghdrs.html)). Why do you say it should be `0`? – lurker Jul 02 '16 at 11:16
  • The file appears as 00:49 in VLC and Windows media player, and when viewing in a folder in Windows Explorer. I have a separate decade old MPG video which calculates correctly at 00:40. – Ross Jul 02 '16 at 13:46
  • You mean your code calculates the old MPG video correctly? Not sure what to say about the 00:49 video. Your code is extracting the fields correctly from the time code. Is 00:49 really 00:00:49 (it appears to be missing the hours position)? Are you sure the 00:49 video is mpeg2? – lurker Jul 02 '16 at 15:48
  • I do mean it calculates in my software as 01:00:49 for the newer MPG and 00:00:40 for the older MPG. I can still offer the MPG file to download if someone wants to check it out themselves. I guess other media software are calculating the duration differently somehow. Or is there an extension to MPEG-2 that I don't know about? – Ross Jul 03 '16 at 00:25
  • It wouldn't hurt to post the link to the mpeg file. – lurker Jul 03 '16 at 11:23
  • http://stationplaylist.com/airtel.mpg @Ken White – Ross Jul 03 '16 at 23:48

1 Answers1

2

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.

Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54
  • I never thought to check that, but that sounds like a good solution. Thanks for that! – Ross Jul 06 '16 at 02:52