Installation
composer require debach/zend-mp3
Ensure ext-zip is enabled in your php.ini (required for MP3 metadata operations).
First Use Case: Reading Metadata
use Debach\ZendMp3\Mp3;
$mp3 = new Mp3('path/to/song.mp3');
$metadata = $mp3->getMetadata();
// Access fields
echo $metadata['title']; // Song title
echo $metadata['artist']; // Artist name
echo $metadata['album']; // Album name
Where to Look First
src/Mp3.php for supported metadata fields (e.g., id3v1, id3v2).genre, year, track).Batch Metadata Extraction
$files = glob('storage/audio/*.mp3');
$metadata = [];
foreach ($files as $file) {
$mp3 = new Mp3($file);
$metadata[$file] = $mp3->getMetadata();
}
Conditional Metadata Handling
$mp3 = new Mp3('song.mp3');
$metadata = $mp3->getMetadata();
if (isset($metadata['albumart'])) {
// Handle embedded album art (base64 or file path)
Storage::put('public/album-art/' . basename($file), base64_decode($metadata['albumart']));
}
Integration with Laravel Filesystem
use Illuminate\Support\Facades\Storage;
$file = Storage::path('audio/song.mp3');
$mp3 = new Mp3($file);
$metadata = $mp3->getMetadata();
// Store metadata in database
Audio::updateOrCreate(['path' => $file], $metadata);
$metadata = Cache::remember("mp3_{$file}", now()->addHours(1), function() use ($file) {
return (new Mp3($file))->getMetadata();
});
try-catch for corrupted files.
try {
$mp3 = new Mp3($file);
} catch (\Exception $e) {
Log::error("Failed to read {$file}: " . $e->getMessage());
}
$mp3 = new Mp3('song.mp3');
$mp3->setMetadata(['title' => 'New Title', 'artist' => 'New Artist']);
$mp3->save();
Unsupported Formats
.flac, .ogg, etc., to confirm compatibility (likely unsupported).finfo or getid3 for broader format support.Metadata Field Inconsistency
lyrics, comment).$normalized = array_intersect_key($metadata, array_flip(['title', 'artist', 'album', 'year']));
Performance with Large Files
ini_set('max_execution_time', 30) or queue the task.ID3v1 vs. ID3v2
$mp3->getId3Version() to handle version-specific logic.Filesystem Permissions
file_exists($file) and is_readable($file) before processing.Log::debug('MP3 Metadata', $mp3->getMetadata());
if (!$mp3->isValid()) {
Log::error("Invalid MP3 file: {$file}");
}
RuntimeException (corrupt files), InvalidArgumentException (invalid paths).Custom Metadata Fields
Mp3 class to parse non-standard tags (e.g., custom TXXX frames in ID3v2).class CustomMp3 extends Mp3 {
public function getCustomField($key) {
return $this->getId3v2Tag()->getFrame($key) ?: null;
}
}
Integration with Laravel Scout
class Mp3Search extends ScoutEngine {
public function toArray($model) {
return [
'title' => $model->metadata['title'] ?? '',
'artist' => $model->metadata['artist'] ?? '',
];
}
}
Batch Processing with Laravel Queues
class ProcessMp3Job implements ShouldQueue {
public function handle() {
$files = glob(storage_path('audio/*.mp3'));
foreach ($files as $file) {
$this->processFile($file);
}
}
}
How can I help you explore Laravel packages today?