问题
My Chrome extension shows a list of links to .mp3
files and I need to get the bitrate for each element. How can I calculate this?
UPDATE
Solved it.
回答1:
Solved it with this simple AJAX request:
var audioLink = document.querySelector('.my_audio_link_class');
var durationBlock = document.querySelector('.element_with_duration_in_text_class').innerText.split(':'); //it has string '1:36' for example and i create new array with minutes and seconds
var duration = durationBlock[0]*60 + +durationBlock[1]; //convert minutes into seconds and convert string with second into integer, then summarize them
function (audioLink, duration) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType('text/xml');
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
var size = xmlhttp.getResponseHeader('Content-Length');//get file size
var kbit=size/128;//calculate bytes to kbit
var kbps= Math.ceil(Math.round(kbit/duration)/16)*16;
console.log(kbps);
}
};
xmlhttp.open("HEAD", audioLink, true);
xmlhttp.send();
}
Hope it helps someone and sorry for my bad english.
来源:https://stackoverflow.com/questions/21634091/is-it-possible-to-get-bitrate-of-mp3