Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: Calculate bitrate in PHP? (Read 5320 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

Calculate bitrate in PHP?

... It's a bit off for some bitrates

I didn't really know what I was doing (working with sounds isn't my thing) so I got this equation:
Code: [Select]
$kbps = floor((filesize($file) / 1024) / (7.328125 * ($seconds / 60)));


Now for those who don't know PHP, the floor() function takes any number and automatically rounds it down
filesize() returns the filesize of the given file in bites
The rest is just normal math

But on some files that are 128 kbps, it shows up as 129, or 127, due to lack of percision in the equation
My guess is it's the 7.328125?

Calculate bitrate in PHP?

Reply #1
Try:

Code: [Select]
$kbps = floor((filesize($file) * 8) / (1024 * $seconds));


which simplifies to:

Code: [Select]
$kbps = floor(filesize($file) / (128 * $seconds));

Calculate bitrate in PHP?

Reply #2
Where do you get the $seconds from?

Calculate bitrate in PHP?

Reply #3
Presumably, if the filesize is the total file size, and the seconds is the length of music, this will not take tags into account, or any other additional data... or I could be completely wrong.

Calculate bitrate in PHP?

Reply #4
Quote
Try:

Code: [Select]
$kbps = floor((filesize($file) * 8) / (1024 * $seconds));


which simplifies to:

Code: [Select]
$kbps = floor(filesize($file) / (128 * $seconds));

Isn't "kbit" when it comes to bitrates of audio streams usually 1000 bits? Then the constant in the second eq should be 125.

Calculate bitrate in PHP?

Reply #5
Just another concern: Does it handle VBR files well?
It all comes down to how it calculates the $seconds, I guess.
[span style=\'font-size:10pt;line-height:100%\']Ib[/span]

Calculate bitrate in PHP?

Reply #6
Yes, kbps is 1000 bits per second, not 1024.
On VBR files this will give average bitrate - the best solution if you need one number, IMHO.
And tags are not problem usually, since their size is less than 1% of the file size.

-Eugene
The  greatest  programming  project of all took six days;  on the seventh  day  the  programmer  rested.  We've been trying to debug the !@#$%&* thing ever since. Moral: design before you implement.

Calculate bitrate in PHP?

Reply #7
Take a look at getid3().  It has all sorts of functions that can go out and get metadata about mp3s and various other audio/video formats.

Also, the author participates in this forum, which is always nice.

- Agent 86

Calculate bitrate in PHP?

Reply #8
Quote
Yes, kbps is 1000 bits per second, not 1024.
On VBR files this will give average bitrate - the best solution if you need one number, IMHO.
And tags are not problem usually, since their size is less than 1% of the file size.

-Eugene

No, it actually depends on which format you're talking about.  IIRC, WMA uses 1024, for example.

Calculate bitrate in PHP?

Reply #9
Quote
Take a look at getid3()

I was going to suggest that

Calculating MP3 bitrate & playtime can be fairly simple on a rough-guess level, but there are a huge number of things that can throw your calculations off, including (but not limited to)
  • VBR (with Xing/LAME header)
  • VBR (with Fraunhofer header)
  • VBR (with no header)
  • Incorrect Xing/LAME or Fraunhofer VBR header
  • Garbage data at beginning of file giving false synch
  • free-format bitrate (just added support for this today )
So there's plenty of things that can throw your calculations way off, before you even get to taking ID3v2, APE, Lyrics3 and ID3v1 tags into account.

getID3() is there to save you all the trouble of handling these problems...
Code: [Select]
$fileinfo = GetAllFileInfo($filename);
echo $fileinfo['playtime_seconds']; // as a float,  ex: 210.93875
echo $fileinfo['playtime_string'];  // as a string, ex: 3:31

Oh, and it can handle other audio/video formats too, like Ogg Vorbis, WAV, AVI, MPEG video, ASF, WMA, WMV, Real, Monkey's Audio, FLAC, OggFLAC, VQF, Speex, MPC, AAC, AU, VOC, AIFF, OptimFROG, WavPack, LPAC, Quicktime, NSV, MIDI. And get metadata from ID3v1, ID3v2, APE, Lyrics3, Vorbis comment, etc. 

Calculate bitrate in PHP?

Reply #10
Sorry guys, I forgot I asked this question 
It's just a script that parses Winamps m3u and b4s playlists, which already have the lngth of the songs stored in them
So that's where I get the length in seconds

The only time I call the files is to check their size and for the bitrate equation, other than that I just use what the winamp playlist already has stored

Calculate bitrate in PHP?

Reply #11
If you have playtime in seconds, and filesize in bytes, then

bitrate = kilobits / second
bitrate = (($filesize * 8) / 1000) / $seconds
bitrate = $filesize / (125 * $seconds)

Which is what Chun-Yu and ErikS said.

Calculate bitrate in PHP?

Reply #12
Quote
Quote
Yes, kbps is 1000 bits per second, not 1024.
On VBR files this will give average bitrate - the best solution if you need one number, IMHO.
And tags are not problem usually, since their size is less than 1% of the file size.

-Eugene

No, it actually depends on which format you're talking about.  IIRC, WMA uses 1024, for example.

"Microsoft always breaks standards" ™
The  greatest  programming  project of all took six days;  on the seventh  day  the  programmer  rested.  We've been trying to debug the !@#$%&* thing ever since. Moral: design before you implement.

Calculate bitrate in PHP?

Reply #13
Hmmmm, still not perfect[/], they're about the same
I went with the 'bitrate = $filesize / (125 * $seconds)' simply because it took up less room, but all 3 equation gave the same result

Thanks guys!

Calculate bitrate in PHP?

Reply #14
It'll only be perfect if there are no tags, and most importantly if your playtime data is correct.  Winamp doesn't always get the playtime correct for various reasons:
  • headerless VBR (assumes entire song is CBR at first bitrate encountered, which could potentially be 32kbps for a ~200bps VBR file and Winamp would display a playtime 6x too long)
  • Fraunhofer-header VBR (Winamp assumes entire song is 160kbps, although v2.92 supposedly fixes this)
  • files with corrupted beginnings - anything goes here...
  • ID3v2 tags - typically adds only ~1-4kB to filesize (can theoretically be up to ~250MB, but anyone who does that is stupid), a ~100kB tag isn't inconceivable if cover art is embedded which would put a typical song playtime off by 2-4%
  • APE tags - typically small, less than 1kB
  • Lyrics3 tags - typically between 1kB and 10kB
  • ID3v1 - 128 bytes, immaterial in terms of playtime

Calculate bitrate in PHP?

Reply #15
Quote
It'll only be perfect if there are no tags, and most importantly if your playtime data is correct.  Winamp doesn't always get the playtime correct for various reasons:
  • headerless VBR (assumes entire song is CBR at first bitrate encountered, which could potentially be 32kbps for a ~200bps VBR file and Winamp would display a playtime 6x too long)

  • Fraunhofer-header VBR (Winamp assumes entire song is 160kbps, although v2.92 supposedly fixes this)

  • files with corrupted beginnings - anything goes here...

  • ID3v2 tags - typically adds only ~1-4kB to filesize (can theoretically be up to ~250MB, but anyone who does that is stupid), a ~100kB tag isn't inconceivable if cover art is embedded which would put a typical song playtime off by 2-4%

  • APE tags - typically small, less than 1kB

  • Lyrics3 tags - typically between 1kB and 10kB

  • ID3v1 - 128 bytes, immaterial in terms of playtime

Most important reason why winamp shows inaccurate time for mp3:s is rounding. One half second typically corresponds to 10 kB data.

Calculate bitrate in PHP?

Reply #16
$kbps = floor(filesize($file) / (125 * $seconds)) seems to work a LOT better with Winamp3's b4s playlists
They store milliseconds instead of just seconds