Created
September 15, 2024 08:59
-
-
Save hctilg/8db36b513b68dda7fc708f27c7866b5f to your computer and use it in GitHub Desktop.
Get audio tags in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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