We help IT Professionals succeed at work.

Calculate the Kbps (kilobits per second) of a MP3 file.

cunoc
cunoc used Ask the Experts™
on
Hi all Experts,
Please help me with the math problem I have and thank you very much in advance.
---------------
Calculate the Kbps (kilobits per second) of a MP3 file.

If you have a 5 MB file with a length of 3 minutes, you would use the formular below to find out the Kbps (kilobits per second) of a MP3 file.

1K = 1024 bytes
1M = 1024K

1 hour   = 60 minutes
1 minute = 60 seconds

5MB = 5 * 1024 * 1024 bytes = 5242880 bytes

5242880 * 8 / 180 / 1000 = ~233 kbps
------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE></TITLE>
<script type="text/javascript">
var Hour = 0;
var Minute = 3;
var Second = 0;

function F_Calculate_MP3_Bitrate_Forward ()
{
   var i = parseFloat(document.getElementById('Calculate_MP3_Bitrate_Forward').value);
   var m = "";

   if (!isNaN(i)) m = Math.ceil(((i * 1024 * 1024) * 8 / ((Hour * 3600) + (Minute * 60) + (Second * 1)) / 1000) / 1024 / 1024);
      document.getElementById('Calculate_MP3_Bitrate_Reversed').value = m;
}

function F_Calculate_MP3_Bitrate_Reversed ()
{
   var i = parseFloat(document.getElementById('Calculate_MP3_Bitrate_Reversed').value);
   var m = "";
   if (!isNaN(i)) m = Math.ceil(((i * 1024 * 1024) / 8 * ((Hour * 3600) + (Minute * 60) + (Second * 1)) * 1000) / 1024 / 1024);
      document.getElementById('Calculate_MP3_Bitrate_Forward').value = m;
}

function setbitrate_Hour(in_hour)
{
   Hour = in_hour;
}

function setbitrate_Minute(in_minute)
{
   Minute = in_minute;
}

function setbitrate_Second(in_second)
{
   Second = in_second;
}

</script>

</head>

<body>
<br />
<center>

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="98%">
<tr>
<td width="40%">
  <label><input id="Calculate_MP3_Bitrate_Forward" onKeyUp="F_Calculate_MP3_Bitrate_Forward()"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;File Size (MB)</font></label>
</td>

<td <td width="20%"><select name="Hour" id="Hour" onchange="setbitrate_Hour(this.options[this.selectedIndex].value);">
<option value="0" selected="selected">0 hour</option>
<option value="1">1 hour</option>
<option value="2">2 hours</option>
</select>
</td>


<td <td width="20%"><select name="Minute" id="Minute" onchange="setbitrate_Minute(this.options[this.selectedIndex].value);">
<option value="1">1 minute</option>
<option value="2">2 minutes</option>
<option value="3" selected="selected">3 minutes</option>
<option value="4">4 minutes</option>
<option value="5">5 minutes</option>
<option value="6">6 minutes</option>
<option value="7">7 minutes</option>
<option value="8">8 minutes</option>
<option value="9">9 minutes</option>
<option value="10">10 minutes</option>
<option value="11">11 minutes</option>
</select>
</td>


<td <td width="20%"><select name="Second" id="Second" onchange="setbitrate_Second(this.options[this.selectedIndex].value);">
<option value="0" selected="selected">0 Second</option>
<option value="1">1 Second</option>
<option value="2">2 Seconds</option>
<option value="3">3 Seconds</option>
<option value="4">4 Seconds</option>
<option value="5">5 Seconds</option>
<option value="6">6 Seconds</option>
<option value="7">7 Seconds</option>
<option value="8">8 Seconds</option>
<option value="9">9 Seconds</option>
<option value="10">10 Seconds</option>
<option value="11">11 Seconds</option>
</select>
</td>


<td width="30%">
  <label><input id="Calculate_MP3_Bitrate_Reversed" onKeyUp="F_Calculate_MP3_Bitrate_Reversed()"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;(Kbps)</font></label>
</td>

</tr>
</table>

</center>
</body>
</html>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Here you go, btw 1024 bytes in a KB and only 1000 KB in a MB. not 1024 and 1024 :)
 
 

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE></TITLE>
<script type="text/javascript">
var Hour = 0;
var Minute = 3;
var Second = 0;

function kbits(n)
{
  return n*1024*8;
}
function mbytes(n)
{
  return n/8/1024;
}
function timespan()
{
  return Hour*3600 + Minute*60 + Second;
}
function F_Calculate_MP3_Bitrate_Forward ()
{
   var i = parseFloat(document.getElementById('Calculate_MP3_Bitrate_Forward').value);
   var m = "";
   if (!isNaN(i)) m = Math.ceil(kbits(i)/timespan());
      document.getElementById('Calculate_MP3_Bitrate_Reversed').value = m;
}

function F_Calculate_MP3_Bitrate_Reversed ()
{
   var i = parseFloat(document.getElementById('Calculate_MP3_Bitrate_Reversed').value);
   var m = "";
   if (!isNaN(i)) m = Math.round(mbytes(i)*timespan());
      document.getElementById('Calculate_MP3_Bitrate_Forward').value = m;
}

function setbitrate_Hour(in_hour)
{
   Hour = in_hour;
}

function setbitrate_Minute(in_minute)
{
   Minute = in_minute;
}

function setbitrate_Second(in_second)
{
   Second = in_second;
}

</script>

</head>

<body>
<br />
<center>

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="98%">
<tr>
<td width="40%">
  <label><input id="Calculate_MP3_Bitrate_Forward" onKeyUp="F_Calculate_MP3_Bitrate_Forward()"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;File Size (MB)</font></label>
</td>

<td <td width="20%"><select name="Hour" id="Hour" onchange="setbitrate_Hour(this.options[this.selectedIndex].value);">
<option value="0" selected="selected">0 hour</option>
<option value="1">1 hour</option>
<option value="2">2 hours</option>
</select>
</td>


<td <td width="20%"><select name="Minute" id="Minute" onchange="setbitrate_Minute(this.options[this.selectedIndex].value);">
<option value="1">1 minute</option>
<option value="2">2 minutes</option>
<option value="3" selected="selected">3 minutes</option>
<option value="4">4 minutes</option>
<option value="5">5 minutes</option>
<option value="6">6 minutes</option>
<option value="7">7 minutes</option>
<option value="8">8 minutes</option>
<option value="9">9 minutes</option>
<option value="10">10 minutes</option>
<option value="11">11 minutes</option>
</select>
</td>


<td <td width="20%"><select name="Second" id="Second" onchange="setbitrate_Second(this.options[this.selectedIndex].value);">
<option value="0" selected="selected">0 Second</option>
<option value="1">1 Second</option>
<option value="2">2 Seconds</option>
<option value="3">3 Seconds</option>
<option value="4">4 Seconds</option>
<option value="5">5 Seconds</option>
<option value="6">6 Seconds</option>
<option value="7">7 Seconds</option>
<option value="8">8 Seconds</option>
<option value="9">9 Seconds</option>
<option value="10">10 Seconds</option>
<option value="11">11 Seconds</option>
</select>
</td>


<td width="30%">
  <label><input id="Calculate_MP3_Bitrate_Reversed" onKeyUp="F_Calculate_MP3_Bitrate_Reversed()"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;(Kbps)</font></label>
</td>

</tr>
</table>

</center>
</body>
</html>

Open in new window

Author

Commented:
Hi Experts,
Suppose if I have a 5 MB file with a length of 3 minutes, I would get 233 Kbps (kilobits per second) of a MP3 file.

In this Script, I got 228 kbps instead of 233 kbps and when I changed the value to 5 MB file with a length of 3 minute and 1 second the result of the kbps would be as 23kbps and etc... that mean the calculation is totally wrong.

Go back to the beginning, without using the hour, or second option in the script I have no problem with the result.

I don't see any mistake in the calculation, but still error with the result, thanks
----
5MB = 5 * 1024 * 1024 bytes = 5242880 bytes

5242880 * 8 / 180 / 1000 = ~233 kbps

Author

Commented:
Hi mplungjan,

What do you mean (write proper titles of your questions) and (put code snippets in the CODE field) I am confused, please dictate so next time I won't repeat the mistake again and please accept my errors in that...

Thanks for your advices..

Author

Commented:
Hi Michel,
I got it and thank for your good advice - please accept my mistake and by the way - do you have any idea what is wrong with my calculation in my question (MP3 Bitrate), any help from you, I don't mean to force you on this but if you have time please take a look to see what wrong. thanks in advance..
Michel PlungjanIT Expert
Top Expert 2009
Commented:
This looks better - I get 23.3.... before the ceil:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<TITLE></TITLE>
<script type="text/javascript">

function calc(sizeInMB,Hour,Minute,Second) {
  if (isNaN(sizeInMB) || sizeInMB == "") return 0;
  var sizeInBytes = sizeInMB*(1024*1024);
  var sizeInBits = sizeInBytes*8;
  var sizeInKb = sizeInBits/1000;
  var timeInSeconds = (Hour * 3600) + (Minute * 60) + (Second)
  return Math.ceil(sizeInKb/timeInSeconds)
}

function F_Calculate_MP3_Bitrate_Forward(theForm) {
   var sizeInMB = parseFloat(theForm.Calculate_MP3_Bitrate_Forward.value);
   var Hour   = theForm.Hour.options[theForm.Hour.selectedIndex].value;
   var Minute = theForm.Minute.options[theForm.Minute.selectedIndex].value;
   var Second = theForm.Second.options[theForm.Second.selectedIndex].value;
   var Hour = theForm.Hour.options[theForm.Hour.selectedIndex].value;
   document.getElementById('Calculate_MP3_Bitrate_Reversed').value = calc(sizeInMB,Hour,Minute,Second)   
}

function F_Calculate_MP3_Bitrate_Reversed (theForm) {
}
window.onload=function() {
  F_Calculate_MP3_Bitrate_Forward(document.forms[0])
}

</script>

</head>

<body>
<br />
<center>
<form>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" width="98%">
<tr>
<td width="40%">
  <label for="Calculate_MP3_Bitrate_Forward">Calculate MP3 Bitrate Forward</label><br /><input name="Calculate_MP3_Bitrate_Forward" id="Calculate_MP3_Bitrate_Forward" onKeyUp="F_Calculate_MP3_Bitrate_Forward(this.form)"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;File Size (MB)</font>
</td>

<td <td width="20%"><select name="Hour" id="Hour" onchange="F_Calculate_MP3_Bitrate_Forward(this.form)">
<option value="0" selected="selected">0 hours</option>
<option value="1">1 hour</option>
<option value="2">2 hours</option>
</select>
</td>


<td <td width="20%"><select name="Minute" id="Minute" onchange="F_Calculate_MP3_Bitrate_Forward(this.form)">
<option value="1">1 minute</option>
<option value="2">2 minutes</option>
<option value="3" selected="selected">3 minutes</option>
<option value="4">4 minutes</option>
<option value="5">5 minutes</option>
<option value="6">6 minutes</option>
<option value="7">7 minutes</option>
<option value="8">8 minutes</option>
<option value="9">9 minutes</option>
<option value="10">10 minutes</option>
<option value="11">11 minutes</option>
</select>
</td>


<td <td width="20%"><select name="Second" id="Second" onchange="F_Calculate_MP3_Bitrate_Forward(this.form)">
<option value="0" selected="selected">0 Second</option>
<option value="1">1 Second</option>
<option value="2">2 Seconds</option>
<option value="3">3 Seconds</option>
<option value="4">4 Seconds</option>
<option value="5">5 Seconds</option>
<option value="6">6 Seconds</option>
<option value="7">7 Seconds</option>
<option value="8">8 Seconds</option>
<option value="9">9 Seconds</option>
<option value="10">10 Seconds</option>
<option value="11">11 Seconds</option>
</select>
</td>


<td width="30%">
  <label for="Calculate_MP3_Bitrate_Reversed">Calculate MP3 Bitrate Reversed</label><br/><input name="Calculate_MP3_Bitrate_Reversed" id="Calculate_MP3_Bitrate_Reversed" onKeyUp="F_Calculate_MP3_Bitrate_Reversed(this.form)"/><font color="#000000" face="Tahoma" style="font-size: 8pt">&nbsp;(Kbps)</font>
</td>

</tr>
</table>
</form>
</center>
</body>
</html>

Open in new window

Michel PlungjanIT Expert
Top Expert 2009

Commented:
Do you need help with the reverse too?

Author

Commented:
Hi Experts,
I am not trying to say you are wrong but when tried many different values, such as a 500 MB, 20 minutes, 0 second, I got 350Kbps and it make sense.

And when I changed to 500 MB, 20 minutes, 10 second, the result would be 35kbps and I believed it errors somewhere because 11 second is very small value and the result of the (Kbps) is dropped down to much in comparison.

Do you have any clue what wrong, Again - I am not saying you are wrong - Thanks for viewing.

Author

Commented:
Hi mplungjan:
Yes, I do
Thanks so much for asking..
Michel PlungjanIT Expert
Top Expert 2009
Commented:
500MB, 20 min 10 sec gives me 35Kbps or 4194304/120010

in my browser with the above code

Author

Commented:
Hi Experts,

I have a MP3 file, when view in Details in Windows and Nero and others Apps, it shown me that:
Size:     8,36 MB
Duration: 00:06:05
Bitrate : 192kbps

When I run the script:
I entered 8.36 as for the File Size
I entered 6 as for the Minute
I entered 5 as for the Second
The result was 20kbps

20kbps is far below the value of 192kbps and I have tested other MP3 files as well and the result came out very low ...

Thanks
Michel PlungjanIT Expert
Top Expert 2009
Commented:
Sorry. I suck at math, but in any case you need the correct calculation
I do not think your original calculation is correct

http://www.eggheadcafe.com/software/aspnet/29078796/how-to-calculate-kbps-val.aspx

http://en.wikipedia.org/wiki/Bit_rate

function calc(sizeInMB,Hour,Minute,Second) {
  if (isNaN(sizeInMB) || sizeInMB == "") return 0;
  var sizeInBytes = sizeInMB*(1024*1024);
  var sizeInBits = sizeInBytes*8;
  var sizeInKb = sizeInBits/1000; // if kb then 1000 re: http://en.wikipedia.org/wiki/Data_rate_units
  var timeInSeconds = (Hour * 3600) + (Minute * 60) + (Second);
  document.forms[0].Calculate_MP3_Bitrate_Reversed.title=sizeInKb+'Kb/'+timeInSeconds +'sec';
  return Math.ceil(sizeInKb/timeInSeconds);
}

Open in new window

Author

Commented:
Hi Experts,
I know it bother you but the result is still mess up, I have no clue why, if I take out all (hour and second option) except the minute, then the code work fine - no problem at all.
Thanks
IT Expert
Top Expert 2009
Commented:
SO sorry

Missed the selects are STRINGs

function F_Calculate_MP3_Bitrate_Forward(theForm) {
   var sizeInMB = parseFloat(theForm.Calculate_MP3_Bitrate_Forward.value);
   var Hour   = parseInt(theForm.Hour.options[theForm.Hour.selectedIndex].value,10);
   var Minute = parseInt(theForm.Minute.options[theForm.Minute.selectedIndex].value,10);
   var Second = parseInt(theForm.Second.options[theForm.Second.selectedIndex].value,10);
   document.getElementById('Calculate_MP3_Bitrate_Reversed').value = calc(sizeInMB,Hour,Minute,Second)   
}

Open in new window

Author

Commented:
Hi experts,
What I did wrong, I assigned the points and it should closed.. ?? please let me know what I did wrong..

Author

Commented:
Hi Experts,
Please forgive me, I left out one Expert for the point, how I can fix this ... and divided the point for him. thanks
Michel PlungjanIT Expert
Top Expert 2009
Commented:
Just click "Accpet multiple solutions" and remember to check the "Accept this as solution" for the one that helped you the most

Author

Commented:
Hi all Experts
Thank you again very much.