Skip to content

Instantly share code, notes, and snippets.

@hctilg
Created September 15, 2024 08:59
Show Gist options
  • Save hctilg/8db36b513b68dda7fc708f27c7866b5f to your computer and use it in GitHub Desktop.
Save hctilg/8db36b513b68dda7fc708f27c7866b5f to your computer and use it in GitHub Desktop.
Get audio tags in PHP
<?php
require_once './getid3/getid3.php';
/**
* @param string $audio URL or local Path
* @return array
*/
function get_audio_tags($audio) {
// Initialize getID3 engine
$getID3 = new \getID3;
if (filter_var($audio, FILTER_VALIDATE_URL) !== false) {
if ($fp_remote = fopen($audio, 'rb')) {
$local_temp_path = tempnam('/tmp', 'getID3');
if ($fp_local = fopen($local_temp_path, 'wb')) {
while ($buffer = fread($fp_remote, 8192)) {
fwrite($fp_local, $buffer);
}
fclose($fp_local);
$info = $getID3->analyze($local_temp_path);
unlink($local_temp_path); // Delete temporary file
} fclose($fp_remote);
}
} else {
$info = $getID3->analyze($audio);
}
return [
"title"=> $info['tags']['id3v2']['title'][0],
"artist"=> $info['tags']['id3v2']['artist'][0],
"album"=> $info['tags']['id3v2']['album'][0],
"genre"=> join(', ', $info['tags']['id3v2']['genre']),
"publisher"=> $info['tags']['id3v2']['publisher'][0],
"cover"=> $info['comments']['picture'][0]['data'],
"year"=> $info['tags']['id3v2']['year'][0]
];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment