This is how to extract the play time (min:sec).. - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# This is how to extract the play time (min:sec)..

Guest
Reading through the boards for this very question, I wasn't able to find this answer. So I dove in, and found it very easy to extract.

So here's the underworkings.

Total_Seconds = File_Length / (bitrate / 8) / 1000;

To retrieve the bitrate, you'll need to read the first 4 bytes of the file, and mask out a bitindex value. This is stored in the upper 4 bits of the 3rd byte in the file.

BitIndex = (Byte3 AND 240) / 16

The division just shifts the bits over so we can use the result for the bitrate lookup table.

This value will be used in a lookup table.

For standard mpeg 1 layer 3, use this table

0: BitRate = -1 (currupt file?)

1: BitRate = 32

2: BitRate = 40

3: BitRate = 48

4: BitRate = 56

5: BitRate = 64

6: BitRate = 80

7: BitRate = 96

8: BitRate = 112

9: BitRate = 128

10: BitRate = 160

11: BitRate = 192

12: BitRate = 224

13: BitRate = 256

14: BitRate = 320

(Other MPEG tables exist, try

Now once you have the Bitrate, you can use the formula:

Total_Seconds = File_Length / (bitrate / 8) / 1000;

And you got the length in seconds. (simple math to calc Min:Sec).

NOTES: This does not account for variable bitrate files, or files with different Khz, Mono, etc.. So you may have to adjust accordingly.

Hope this helps someone.

Jikg