How to analyze MP3 files in Laravel?

How to analyze MP3 files in Laravel?

Tudip

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 Characteristics Audio Tags
Duration Songs
Bit Rate Artist
Sample Rate Album
Channels mode Year
Frames count Comment
Codec version Track
Layer version Genre

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:

Property Description Values
$codeVersion MPEG codec version 1 or 2
$layerVersion Audio layer version 1 or 2 or 3
$audioSize Audio size in bytes. Note that this value NOT equals file size Int
$duration Audio duration in seconds.microseconds Like 3603.0171428571 (means 1 hour and 3 sec)
$bitRate Audio bit rate in bps Like 128000 (means 128kb/s)
$sampleRate Audio sample rate in Hz Like 44100 (means 44.1KHz)
$isVbr Contain true if audio has a variable bit rate boolean
$channel Channel mode ‘Stereo’ or ‘dual_mono’ or ‘joint_stereo’  or ‘mono’
$tags1 Audio tags ver.1 [“song”] => “song name”, “year” => 2020
$tags2 Audio tags ver.2, only text ones. [“TIT2” => “Long song name”,…]
$tags Combined audio tags (from id3v1 & id3v2). Keys as in tags1. [“song” => “Long song name”,”year” => 2020,…]
$_parsingTime It 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.

search

Blog Categories

Request a quote