5 Best PHP Libraries for Audio and Music Manipulation

If you’re a PHP programmer, there’s a good chance that you’ve had to work with audio or music files at some point. Maybe you need to programmatically generate a ringtone, or maybe you need to be able to read and write ID3 tags. Whatever the case may be, there’s a library out there that can help you get the job done. Here are 5 of the best PHP libraries for audio and music manipulation.

PHP-MP3

PHP-MP3 is a class that allows you to manipulate MP3 files in various ways. With this library, you can play, convert, and stream MP3 files. You can also programmatically generate ID3 tags and operate on MPEG frames.

This library does not allow you to change the MP3 properties like bitrate, sample size, and sample rate. However, it is useful for modifying/creating/reading valid MP3 containers. With PHP-MP3, you can add or remove tags from MP3 files, as well as extract information about the audio file such as the length, bitrate, and sample rate.

\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->stripTags()->saveFile("new.mp3");
\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->trim(10, 30)->saveFile("new.mp3");
\falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->append(\falahati\PHPMP3\MpegAudio::fromData(base64_decode("/**BASE64-DATA**/")))->saveFile("new.mp3");
echo \falahati\PHPMP3\MpegAudio::fromFile("old.mp3")->getTotalDuration();

You can also use PHP-MP3 to create custom playlists or to split up a single MP3 file into multiple tracks.

getID3

GetID3() is a PHP script that reads and parses various types of audio, video, and tag files. It can read and parse ID3v1, ID3v2.4, ID3v2.3, ID3v2.2, Lyrics3 v1 & v2, and APE v1 & v2 tags. It can also read and parse audio-lossy, audio-lossless, and audio-video files.

The PHP getID3() library enables you to extract information such as the title, artist, album, genre, year, and track number from MP3 files. In addition, it can also retrieve cover art and lyrics.

GetID3() is written in PHP and uses the popular FFmpeg library for processing MP3 files. GetID3() is released under the GNU GPL v2 license.

// Copy remote file locally to scan with getID3()
$remotefilename = 'http://www.example.com/filename.mp3';
if ($fp_remote = fopen($remotefilename, 'rb')) {
    $localtempfilename = tempnam('/tmp', 'getID3');
    if ($fp_local = fopen($localtempfilename, 'wb')) {
        while ($buffer = fread($fp_remote, 8192)) {
            fwrite($fp_local, $buffer);
        }
        fclose($fp_local);
        // Initialize getID3 engine
        $getID3 = new getID3;
        $ThisFileInfo = $getID3->analyze($localtempfilename);
        // Delete temporary file
        unlink($localtempfilename);
    }
    fclose($fp_remote);
}

audio-type

When it comes to working with audio files, it’s important to be able to detect the file format. This is where the audio-type library comes in. It allows you to quickly and easily detect the format of an audio file, so you can ensure compatibility with your system. The library supports a wide range of popular formats, including MP3, WAV, MIDI, FLAC, OGG, and more. It’s also fast and easy to use, so you can get started working with your audio files right away.

use Selective\AudioType\AudioTypeDetector;
use Selective\AudioType\Provider\DefaultAudioProvider;
use SplFileObject;

$file = new SplFileObject('example.mp3');

$detector = new AudioTypeDetector();

// Add audio detectors
$detector->addProvider(new DefaultAudioProvider());
$audioType = $detector->getAudioTypeFromFile($file);

// Get the audio format
echo $audioType->getFormat(); // mp3

// Get the mime type
echo $audioType->getMimeType(); // audio/mp3

phptabs

PhpTabs is a library that allows you to read and write scores and MIDI files. With PhpTabs, you can easily access information like the song name, a list of instruments, or anything else you might need. PhpTabs also supports a variety of file formats, including Guitar Pro 3 (.gp3), Guitar Pro 4 (.gp4), Guitar Pro 5 (.gp5), and MIDI (.mid, .midi). This makes it an incredibly versatile tool for anyone who needs to work with musical notation files.

require_once 'src/PhpTabs/bootstrap.php';

use PhpTabs\PhpTabs;

// Instanciates a tablature
$tablature = new PhpTabs("mytabs.gp3");

// Reads information
echo $tablature->getName();

music-codes

Music-codes is a library that provides handy tools for codes in the music industry. The library includes functions for working with code safety, code validation, and code generation. The library also includes a number of helper functions for working with code in the context of music performance and composition.

$isrc = new Isrc();
$isrc->setCountryCode('GB');
$isrc->setIssuerCode('A1B');
$isrc->setYear(11);
$isrc->setId(3);

// Prefix is the combination of country code and issuer code
// and is now adopted as standard wording in ISRC specification
// in order to allow allocation of ranges of ISRCs
$isrc->setPrefix('GB-A1B'); // can be used with or without the dash   

// will output 'Valid'
echo ($isrc->isValid() ? 'Valid' : 'Not valid') . PHP_EOL;

// By default, the ids equal to zero are not considered as valid
// so to enable this globally you should call the following method:
Isrc::treatZeroIdsAsValid(true);

$isrc = new Isrc('GB-A1B-11-00000')
// will output 'Valid'
echo ($isrc->isValid() ? 'Valid' : 'Not valid') . PHP_EOL; 

In addition, the library provides a number of helpful resources for learning about code in the music industry. The music-codes library is a valuable resource for anyone who needs to work with codes in the music industry.

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close