how to get the duration of an mp3 file
-
Hi, how to get the duration of an mp3 file using c#
Regards, Sivaprasad
-
Hi, how to get the duration of an mp3 file using c#
Regards, Sivaprasad
This isn't something you can do natively in the .NET framework. You'll need to use a 3rd party library to do this. For example, Managed DirectX has APIs to do this.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: Check out this cutie The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi, how to get the duration of an mp3 file using c#
Regards, Sivaprasad
If you know the samplingsfrequency Fs and number of samples N: duration = N/Fs [sec] Cheers Al
-
Hi, how to get the duration of an mp3 file using c#
Regards, Sivaprasad
If I oversimplify this or you all ready know parts of this, I appologize. But here goes... MP3 files are made up of a bunch of music sections, each having a header. At the end of the file is a TAG 128 bytes long. The Gist of getting the duration of an MP3 is to first get the size of the file, then subtract 128 bytes for the TAG. Divide the number of remaining "Bits" in the file by the bitrate. So to get the file length: . . using System.IO; . . FileStream MP3File; . MP3File = new FileStream("C:\\mySong.mp3", FileMode.Open); . FileLength = MP3File.Length; // gives file length in bytes . And for the duration: . Duration = ((FileLength - 128) * 8) / bitrate; //gives duration in seconds . The problem now lies in getting the bitrate. If you know all your files are say 128kbits/sec you can cheat an get a pretty good estimate. So your formula would be. Duration = ((FileLength - 128) * 8) / (1024*128); The problem is real MP3 files can have different bitrates within individiual song blocks. So it would be neccessary to read each block header for the bitrate and then add everything up at the end. Rather a tedious exercise. Hope that helps a bit greycrow