Paul Weiland
Paul Weiland

Reputation: 737

Determine the duration of an audio file without third party components

Description:

I try to determine the duration of an audio file, for example the duration of a song in seconds. I found a lot of articles about that topic but I did not find a solution that is not using a) a third party or b) an old Microsoft library that is already dead. Not finding such a solution does not mean there isn't a solution, it just means I did not dig deep enough or the right person didn't encounter such a question, yet. So I hope I will find somebody who can help me with this problem or refer me to someone/something who/that can.

Upvotes: 0

Views: 199

Answers (1)

jaket
jaket

Reputation: 9341

If it's a wave file you just need to open it up, parse the header and within it you'll find the length of the audio data. There are a lot of questions on SO dealing with the format.

Basically you need the Subchunk2Size which is in bytes divided by NumChannels divided by the number byte per sample (BitsPerSample/8). That will give you the length in samples. If you need the time in seconds divide that by the sample rate.

enter image description here

Upvotes: 2

Related Questions