PHP Classes
elePHPant
Icontem

PHP MPEG Audio Data Extractor: Parse and extract metadata from MP3 files

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (13)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2014-05-26 (1 year ago) RSS 2.0 feedNot enough user ratingsTotal: 316 All time: 6,637 This week: 1,067Up
Version License PHP version Categories  
mpeg-audio-tags-meta 1.0.5GNU Lesser Genera...5.2.1PHP 5, Audio
Description Author  

This class can parse and extract metadata information from MPEG files.

It can parse MPEG video and audio files and extract information about the stream and tags.

The class supports MPEG 1 and MPEG 2 formats with tags in the formats id3v1, id3v2, VBR, CBR. It supports audio layer 1, layer 2 and layer 3 (MP3).

It can extract tags of metadata like song name, duration, artist name, bit rate, album, sample rate, year, mpeg version, comment, layer version, genre channels, mode, etc..

Recommendations  

I want to get audio file sample rate and properties info after
I want to get audio file sample rate and properties info after

Picture of wapmorgan
Name: wapmorgan is available for providing paid consulting. Contact wapmorgan .
Classes: 20 packages by
Country: Russian Federation Russian Federation
Age: ???
All time rank: 124938 in Russian Federation Russian Federation
Week rank: 56 Up6 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

Details provided by the author  
Mpeg Audio Data Extractor - the fastest PHP library that retrieves mp3 tags&meta.
---

This class extracts information about an mpeg audio (particularly, **mp3**).

| MPEG-1        | MPEG-2        | id3v1 | id3v2 | VBR | CBR |
| ------------- | ------------- | ----- | ----- | --- | --- |
| Layer 1       | Layer 1       | + | + | + | + |
| Layer 2       | Layer 2       | + | + | + | + |
| Layer 3       | Layer 3       | + | + | + | + |

This class is very fast!

* It can parse a bunch of mp3 files in less than a half of second (without tags).
* It can parse a bunch of mp3 files with their tags in two seconds or less (with both id3v1 and id3v2).

A bunch - it my case it was **878 megabytes** of mp3 files (**33 tracks** with a total length **8:37:42**).

Note that the audios with variable bit rate (aka VBR) are supported now!

It extracts:

| Tags (id3v1&id3v2) | Meta |
| ---- | ---- |
| song name | duration |
| artist name | bit rate |
| album | sample rate |
| year | mpeg version |
| comment | layer version |
| genre | channels mode |
| other ... | other ... |

* All tags stored in file (**id3v2 and id3v1**). id3v2.4.0 and id3v2.2.0 are not supported, only the most popular id3v2.3.0 is supported.
* Audio parameters:
  - Total duration (in seconds)
  - BitRate (in bps)
  - SampleRate (in Hz)
  - MPEG version
  - Layer version
  - Channel
  - ... and other information

Used sources:
* [mpeg header description](http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm)
* [id3v2 tag specifications](http://id3.org/Developer%20Information). –°oncretely: [id3v2.3.0](http://id3.org/id3v2.3.0), [id3v2.2.0](http://id3.org/id3v2-00), [id3v2.4.0](http://id3.org/id3v2.4.0-changes)
* [Xing, Info and Lame tags specifications](http://gabriel.mp3-tech.org/mp3infotag.html)

## All extractable data
```php
	/**
	 * MPEG codec version (1 or 2)
	 * @var int
	 */
	public $codecVersion;
	/**
	 * Audio layer version (1 or 2 or 3)
	 * @var int
	 */
	public $layerVersion;

	/**
	 * Audio size in bytes. Note that this value is NOT equals file size.
	 * @var int|long
	 */
	public $audioSize;
	/**
	 * Contains audio file name
	 * @var string
	 */
	public $_fileName;
	/**
	 * Contains file size
	 * @var long
	 */
	public $_fileSize;
	/**
	 * Audio duration in seconds.microseconds (e.g. 3603.0171428571)
	 * @var float
	 */
	public $duration;
	/**
	 * Audio bit rate in bps (e.g. 128000)
	 */
	public $bitRate;
	/**
	 * Audio sample rate in Hz (e.g. 44100)
	 * @var int
	 */
	public $sampleRate;
	/**
	 * Contains true if audio has variable bit rate
	 * @var boolean
	 */
	public $isVbr = false;
	/**
	 * Channel mode (stereo or dual_mono or joint_stereo or mono)
	 * @var string
	 */
	public $channel;
	/**
	 * Number of audio frames in file
	 * @var int
	 */
	public $framesCount = 0;
	/**
	 * Contains extra flags
	 * @var array
	 */
	public $extraFlags = array();
	/**
	 * Audio tags ver. 1 (aka id3v1)
	 * @var array
	 */
	public $tags1 = array();
	/**
	 * Audio tags ver. 2 (aka id3v2)
	 * @var array
	 */
	public $tags2 = array();
	/**
	 * Major version of id3v2 tag (if id3v2  present) (2 or 3 or 4)
	 * @var int
	 */
	public $id3v2MajorVersion;
	/**
	 * Minor version of id3v2 tag (if id3v2  present)
	 * @var int
	 */
	public $id3v2MinorVersion;
	/**
	 * List of id3v2 header flags (if id3v2  present)
	 * @var array
	 */
	public $id3v2Flags = array();
	/**
	 * List of id3v2 tags flags (if id3v2 present)
	 * @var array
	 */
	public $id3v2TagsFlags = array();
	/**
	 * Contains time spent to read&extract audio information.
	 * @var float
	 */
	public $_parsingTime;
```


## Usage
After creating an instance of MpegAudioDataExtractor with passing filename as the first argument to the constructor, you can retrieve data from object properties (listed above) and use method toString($newLineStringOrSymbol = "<br/>") which produces (actually, returns) text like this:
```
Size: 375,818 byte(s)
Duration: 0:37.172244897959 second(s)
Bit rate: 80 kbps
Sample rate: 44100 Hz
MPEG-1 Layer 3
```

If you need parse tags, you should create instance this way:
```php
$audio = new MpegAudioDataExtractor($fileName, MpegAudioDataExtractor::META | MpegAudioDataExtractor::TAGS);
```

## ToDo for me (or for you, if you want improve this php-library)
1. ~~Correctly parsing VBR-files~~
2. ~~Improve performance~~
3. Complete parsing id3v2.3 frames.
4. May be implement methods to parse id3v2.2 and id3v2.4 frames.
5. ~~Tests~~ partially (./test.php browser script)
  Files folder image Files  
File Role Description
Files folder imagesrc (4 files)
Accessible without login Plain text file bitRateTable.php Aux. Class source
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Unit test script
Accessible without login Plain text file LICENSE Lic. Auxiliary data
Plain text file MpegAudioDataExtractor.php Class Class source
Plain text file MpegAudioParserException.php Class Class source
Accessible without login Plain text file README.md Doc. Auxiliary data
Accessible without login Plain text file sampleRateTable.php Aux. Class source
Accessible without login Plain text file test.php Example Unit test script

  Files folder image Files  /  src  
File Role Description
  Plain text file bitRateTable.php Class Class source
  Plain text file MpegAudioDataExtractor.php Class Class source
  Plain text file MpegAudioParserException.php Class Class source
  Plain text file sampleRateTable.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 0%Total:316All time:6,637
 This week:0This week:1,067Up