How to analyze MP3 files in Laravel?

How to analyze MP3 files in Laravel?

tudip-logo

Tudip

18 June 2020

We can analyze MP3 files in Laravel by using wapmorgan/mp3info package. The fastest PHP library to extract MP3 meta information and tags.

This class extracts information from Mpeg/mp3 audio:

 

Audio CharacteristicsAudio Tags
DurationSongs
Bit RateArtist
Sample RateAlbum
Channels modeYear
Frames countComment
Codec versionTrack
Layer versionGenre

Installation

Composer package: wapmorgan/mp3info.

mp3_files_in_larvel_01

Command to install composer:

  • composer required wapmorgan/mp3info

Usage

Step 1:

After creating an instance of MP3info with passing the filename as the first argument to the constructor, you can retrieve the data from object properties.
There are two ways to use wapmorgan.

  1. Use wapmorgan\Mp3Info\Mp3Info
    To get basic audio information.

    $audio = new Mp3Info(`filename`);

    If you need parse tags, you should set a 2nd argument.

    $audio = new Mp3Info(`filename`, true);
  2. Another way
    Install the composer and add the code in the respective method.

    composer required wapmorgan/mp3info $audio = new \wapmorgan\Mp3Info\Mp3Info($filename, true);

Step 2:

After that access object properties to get audio information.

$audioDuration = ‘Audio Duration’.‘:’.floor($audio->duration); $audioBitRate = ‘Audio Bitrate’.’:’.floor($audio->bitRate);

You can also find other audio characteristics by following step 2.

To find the Audio tags:

Step 3 (If required):

To access id3v1 tags use $tags1 property. To access id3v2 tags use $tags2 property.

For Simple id3v1 tags,

$audioTagSongs = ‘Songs’.’:’.$audio->tags1[‘song’].’from’.$audio->tags1[‘artist’];

For specific id3v2 tags,

$audioTagSongs = ‘Songs’.’:’.$audio->tags2[‘T1T2’].’from’.$audio->tags2[‘TPE1’];

List of API

Audio Informations:

PropertyDescriptionValues
$codeVersionMPEG codec version1 or 2
$layerVersionAudio layer version1 or 2 or 3
$audioSizeAudio size in bytes. Note that this value NOT equals file sizeInt
$durationAudio duration in seconds.microsecondsLike 3603.0171428571 (means 1 hour and 3 sec)
$bitRateAudio bit rate in bpsLike 128000 (means 128kb/s)
$sampleRateAudio sample rate in HzLike 44100 (means 44.1KHz)
$isVbrContain true if audio has a variable bit rateboolean
$channelChannel mode‘Stereo’ or ‘dual_mono’ or ‘joint_stereo’ or ‘mono’
$tags1Audio tags ver.1[“song”] => “song name”, “year” => 2020
$tags2Audio tags ver.2, only text ones.[“TIT2” => “Long song name”,…]
$tagsCombined audio tags (from id3v1 & id3v2). Keys as in tags1.[“song” => “Long song name”,”year” => 2020,…]
$_parsingTimeIt contains time spent to read & extract audio information in sec.msec

Performance

  1. Typically it parses one mp-3 file with the size around 6-7 MB in less than 0.001 sec.
  2. List of 112 files with constant & and variable bitRate with total duration 5:22:28 are parsed in 1.76 sec.

Request a quote