Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Mp3 duration based on bitrate and file size

Tags:

mp3

I try to calculate mp3 duration by using bitrate and file size , after some search i found this formula:

(mp3sizeInByte*0.008)/bitrate

i am using mp3sizeInByte*0.008 to convert byte to Kbits.

but its not so accurate , in result there is couple second different compare to actual mp3 duration.

i want know this right formula ?

like image 658
alireza Avatar asked Apr 07 '15 15:04

alireza


People also ask

How do you calculate the length of an audio file?

To determine the file size of an audio file, we have to multiply the bit rate of the audio by its duration in seconds. As a result, we get file size values in terms of kilobits and megabits.

How big is 1 minute of an audio file as a MP3?

The typical encoding standard for MP3 files is 128 kilobits per second (kbps, kb/s or kbit/s). This works out to about 1 megabyte (MB) per minute of sound.

How big is a 60 minute audio file?

MP3, 2 tracks, 192 kb/s A good rule of thumb to remember is that 60 minutes of 2 track 24-bit 48 kHz BWAV audio requires about 1 GB of storage. From there, you can easily add or subtract how much storage you need when using the same sample rate. If recording a single track of audio, your storage will double.


2 Answers

If anyone else comes across trying to calculate bitrate in JavaScript with Web Audio API this is how I accomplished it:

<input type="file" id="myFiles" onchange="parseAudioFile()"/>
function parseAudioFile(){
  const input = document.getElementById('myFiles');
  const files = input.files;
  const file = files && files.length ? files[0] : null;
  if(file && file.type.includes('audio')){
    const audioContext = new (window.AudioContext || window.webkitAudioContext)();
    const reader = new FileReader();
    reader.onload = function(e){
      const arrayBuffer = e.target.result;
      audioContext.decodeAudioData(arrayBuffer)
        .then(function(buffer){
          const duration = buffer.duration || 1;
          const bitrate = Math.floor((file.size * 0.008) / duration);
          // Do something with the bitrate
          console.log(bitrate);
        });
    };
    reader.readAsArrayBuffer(file);
  }
}
like image 65
Logus Graphics Avatar answered Sep 21 '22 00:09

Logus Graphics


You can calculate the size using the following formula:

x = length of song in seconds

y = bitrate in kilobits per second

(x * y) / 8

We divide by 8 to get the result in kilobytes(kb).

So for example if you have a 3 minute song

3 minutes = 180 seconds

128kbps * 180 seconds = 23,040 kilobits of data 23,040 kilobits / 8 = 2880 kb

You would then convert to Megabytes by dividing by 1024:

2880/1024 = 2.8125 Mb

If all of this was done at a different encoding rate, say 192kbps it would look like this:

(192 * 180) / 8 = 4320 kb / 1024 = 4.21875 Mb

like image 25
Yogesh Nikam Avatar answered Sep 19 '22 00:09

Yogesh Nikam