analyze($absolutePath); $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, 'date') ?? $this->firstTag($tags, 'recording_time') ?? $this->firstTag($tags, 'creation_date'); $duration = isset($info['playtime_seconds']) ? (int) round((float) $info['playtime_seconds']) : null; $recordedAt = null; if (filled($year) && preg_match('/^\d{4}/', $year)) { try { $recordedAt = Carbon::parse($year)->toDateTimeString(); } catch (\Throwable) { $recordedAt = null; } } return [ 'title' => $title, 'artist' => $artist, 'album' => $album, 'duration_seconds' => $duration, 'recorded_at' => $recordedAt, ]; } /** * Pick the richest tag set getID3 found for this format. * * @param array $info * @return array */ 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 $tags */ private function firstTag(array $tags, string $key): ?string { if (! isset($tags[$key])) { return null; } $value = $tags[$key]; if (is_array($value)) { $value = $value[0] ?? null; } return filled($value) ? (string) $value : null; } }