Getting Mp3 Data Using PHP
If you look at the options that are out there for processing MP3 data in any language, you’ll find that most of the libraries that do the things that you want are pretty sizable because processing MP3 files can be an involved process. However, I wanted to find something that would give me a way to do simple processing of MP3s (given a link to the track on the internet) without having to load up a large library to do it. Since there wasn’t necessarily anything out there that would do exactly what I was looking for, I decided to write my own utility in PHP. PHP of course has the getID3 library which fits the aforementioned criteria, but I wanted something a little bit more lightweight.
At the outset, I would say it may not be the best idea to rely on what follows for much beyond the simple processing of files (especially if these are files are significant in size). Because we are pulling the file down using HTTP, processing can potentially be really SLOOOOW if you are handling multiple files of significant size. This method also assumes you know the bitrate of the file you are evaluating (and uses 128 Kbps by default). This class we’re going to write also requires at least PHP 5.3. So if you use anything below this, it will not work.
With all of that out of the way, let’s get started. We’ll begin by creating a class we’ll call MP3Data
<?php
class Mp3Data {
}
?>
Not too difficult. Of course, we’ll need to add our constructor and some class variables in. We’ll write the get_size_of_file function and the set_data a bit later on. For the purposes of this tutorial, we are going to assume a constant bitrate of 128. We’ll add an optional parameter to pass it in if you wanted to change this.
<?php
class Mp3Data
{
protected $mp3data;
protected $fileDirectory;
protected $bitRate;
protected $blockMax;
public function __construct($filename, $bitrate = null)
{
$this->mp3data = array();
$this->mp3data['filesize'] = $this->get_size_of_file($filename);
$this->fileDirectory = fopen($filename, "r");
$this->blockMax = 1024;
if($bitrate)
$this->bitRate = $bitrate;
else
$this->bitRate= 128;
$this->set_data();
}
}
We’ll use PHP’s ftell function to handle processing of our file and getting the current position of the pointer when the files is read. If you are not entirely familiar with ftell, not to worry. Just know that it will be very useful in determining the file duration from which we can figure out the track length.
We’ll also need a few other methods for our class…
<?php
class Mp3Data
{
protected $mp3data;
protected $fileDirectory;
protected $bitRate;
protected $blockMax;
public function __construct($filename, $bitrate = null)
{
$this->mp3data = array();
$this->mp3data['filesize'] = $this->get_size_of_file($filename);
$this->fileDirectory = fopen($filename, "r");
$this->blockMax = 1024;
if($bitrate)
$this->bitRate = $bitrate;
else
$this->bitRate= 128;
$this->set_data();
}
public function get_mp3_duration() {
return $this->mp3data['duration'];
}
public function get_mp3_filesize() {
return $this->mp3data['filesize'];
}
protected function get_size_of_file($url) {
if (substr($url,0,4)=='http') {
$x = array_change_key_case(get_headers($url, 1),CASE_LOWER);
if ( strcasecmp($x[0], 'HTTP/1.1 200 OK') != 0 ) {
$x = $x['content-length'][1];
}
else {
$x = $x['content-length'];
}
}
else {
$x = @filesize($url);
}
return $x;
}
protected function set_data() {
$this->mp3data['length'] = $this->get_duration($this->mp3data, $this->tell(), $this->bitRate);
$this->mp3data['duration'] = $this->get_formatted_time($this->mp3data['length']);
}
protected function tell()
{
return ftell($this->fileDirectory)-$this->blockMax - 1;
}
protected function get_duration(&$mp3,$startat, $bitrate)
{
if ($bitrate > 0)
{
$KBps = ($bitrate * 1000)/8;
$datasize = ($mp3['filesize'] - ($startat/8));
$length = $datasize / $KBps;
return sprintf("%d", $length);
}
return "";
}
protected function get_formatted_time($duration)
{
return sprintf("%d:%02d", ($duration /60), $duration %60 );
}
}
Now to get information from an MP3 file over the internet all we have to do is create an instance of this class and call it. This will return an associative array with all of the information that we need.
$dataArray = new Mp3Data("http://www.path/to/file.mp3");
echo $dataArray['length']; // filesize
echo $dataArray['duration']; // track time
Pretty neat, eh?






0 Responses to Getting Mp3 Data Using PHP