Support common audio formats beyond MP3.
Accept WAV, OGG, FLAC, M4A, and related uploads, and read metadata from non-ID3 tag formats.
This commit is contained in:
@@ -8,7 +8,7 @@ use getID3;
|
||||
class Mp3MetadataService
|
||||
{
|
||||
/**
|
||||
* Extract ID3 and audio metadata from an MP3 file on disk.
|
||||
* Extract tags and duration from a common audio file (MP3, WAV, OGG, FLAC, M4A, etc.).
|
||||
*
|
||||
* @return array{
|
||||
* title: ?string,
|
||||
@@ -23,17 +23,15 @@ class Mp3MetadataService
|
||||
$analyzer = new getID3;
|
||||
$info = $analyzer->analyze($absolutePath);
|
||||
|
||||
$tags = [];
|
||||
if (isset($info['tags']['id3v2'])) {
|
||||
$tags = $info['tags']['id3v2'];
|
||||
} elseif (isset($info['tags']['id3v1'])) {
|
||||
$tags = $info['tags']['id3v1'];
|
||||
}
|
||||
$tags = $this->preferredTags($info);
|
||||
|
||||
$title = $this->firstTag($tags, 'title');
|
||||
$artist = $this->firstTag($tags, 'artist');
|
||||
$album = $this->firstTag($tags, 'album');
|
||||
$year = $this->firstTag($tags, 'year') ?? $this->firstTag($tags, 'recording_time');
|
||||
$year = $this->firstTag($tags, 'year')
|
||||
?? $this->firstTag($tags, 'date')
|
||||
?? $this->firstTag($tags, 'recording_time')
|
||||
?? $this->firstTag($tags, 'creation_date');
|
||||
|
||||
$duration = isset($info['playtime_seconds'])
|
||||
? (int) round((float) $info['playtime_seconds'])
|
||||
@@ -57,6 +55,38 @@ class Mp3MetadataService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Pick the richest tag set getID3 found for this format.
|
||||
*
|
||||
* @param array<string, mixed> $info
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function preferredTags(array $info): array
|
||||
{
|
||||
$priority = [
|
||||
'id3v2',
|
||||
'id3v1',
|
||||
'vorbiscomment',
|
||||
'quicktime',
|
||||
'riff',
|
||||
'asf',
|
||||
'ape',
|
||||
'matroska',
|
||||
];
|
||||
|
||||
foreach ($priority as $format) {
|
||||
if (! empty($info['tags'][$format]) && is_array($info['tags'][$format])) {
|
||||
return $info['tags'][$format];
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($info['comments']) && is_array($info['comments'])) {
|
||||
return $info['comments'];
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $tags
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user