I'm writing an extension for google chrome. And I have a situation: I've got a link for mp3 file, and I could play it. I could extract duration of it..
But how could I calculate bitrate? Is it possible through some properties? Or through some calculations using file size? If second - how to get filesize?
Thanks.
I'm writing an extension for google chrome. And I have a situation: I've got a link for mp3 file, and I could play it. I could extract duration of it..
But how could I calculate bitrate? Is it possible through some properties? Or through some calculations using file size? If second - how to get filesize?
Thanks.
Share Improve this question asked Jul 20, 2012 at 21:43 ValeriiVasinValeriiVasin 8,72611 gold badges59 silver badges79 bronze badges 4- Any updates? I did some research and shared it, below. – David J. Commented Jul 25, 2012 at 3:01
-
I found only solution to make Ajax request with
HEAD
type and calculate bitrate after.. it's easy after you know filesize... But seems like it's awful solution. And of course it works with domain restrictions – ValeriiVasin Commented Jul 25, 2012 at 6:54 - Did you use the Content-Length header from the HEAD request? I updated my answer, below. – David J. Commented Jul 25, 2012 at 14:09
- yep, it's exactly what i've used. – ValeriiVasin Commented Jul 27, 2012 at 8:31
2 Answers
Reset to default 6Update:
Do a HEAD request and get the filesize from the Content-Length header. Since you know the duration, you can then calculate the bitrate.
Research:
Here I want to share some of my research.
You probably already know this -- the bitrate is stored inside according to the MP3 format specification. But I'm sure you would rather not read the file and deal with all that yourself! Hopefully you can find tools or API's to help. With that in mind...
I looked around for public web APIs that will extract info for MP3's but didn't find anything that jumped out at me.
If you can find a version of ExifTool that you can access from Chrome, it has an API to calculate bitrate.
There is a Javascript library called SoundManager that looked promising, but it does not accurately calculate bitrate.
I looked at the Web Audio API but did not see any mention of reading MP3 files in particular, and therefore no way to extract bitrate.
The bit-rate is the audio_size_in_bits / duration_in_seconds.
If we talk about an MP3 file recording in 128 kb/s (kilo-bits per second), it refers to the target average or constant bit rate that the encoder was aiming for. This bit-rate is defined in each individual frame MPEG frame. For MPEG Layer III (MP3) with 1152 samples at 44.1 kHz, each frame is 25–26 ms.
MP3 files can be encoded with a Constant Bit Rate (CBR), or a Variable Bit Rate (VBR). In CBR mode each frame is encoded with the same bit-rate in each frame, with VBR this is variable. In VBR mode, the bit-rate varies per frame, but on average it should get close to the defined target encoding bit-rate.
Some MP3's are encoded with the presence of a Lame (Xing) Header Information, which can be used to extract some of the encoding settings. Especially with a VBR encoded MP3, without a Lame (Xing) Header, it is difficult to determine what the encoded bit-rate setting was.
To use just the file size to calculate the bitrate in not a reliable method, as audio files may carry non audio date, like metadata (which can include lengthy album art), and may include padding (unused / reserved space).
To determine the actual encoding bit-rate, requires to decode relevant portions of the MP3.
I wrote a JavaScript module music-metadata, which can be used to extract the bit-rate and other information from MP3 files, as well as from other audio files. An example how to do that I provided in this answer.