Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created August 7, 2019 16:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/473a7929a41a16cafcf1b39dcffc46f7 to your computer and use it in GitHub Desktop.
Save parzibyte/473a7929a41a16cafcf1b39dcffc46f7 to your computer and use it in GitHub Desktop.
<?php
include_once __DIR__ . "/src/Mp3Info.php";
use wapmorgan\Mp3Info\Mp3Info;
function endsWith($haystack, $needle)
{
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
function convertirAMinutosYSegundos($segundos)
{
$minutos = floor($segundos / 60);
$segundos = $segundos % 60;
return sprintf("%02d:%02d", $minutos, $segundos);
}
$archivos = scandir(__DIR__);
$contenido = "";
foreach ($archivos as $archivo) {
if (endsWith($archivo, ".mp3")) {
$rutaDeLaCancion = __DIR__ . DIRECTORY_SEPARATOR . $archivo;
$informacion = new Mp3Info($rutaDeLaCancion, true);
$tags1 = $informacion->tags1;
$tags2 = $informacion->tags2;
$titulo = $tags1["song"];
$album = $tags1["album"];
$artista = $tags1["artist"];
$duracion = convertirAMinutosYSegundos($informacion->duration);
$kbps = $informacion->bitRate / 1000;
/**
* Ya extrajimos las etiquetas v1, pero ¿qué tal si hay etiquetas mejoradas en la versión 2?
*/
if (!empty($tags2["TIT2"])) {
$titulo = $tags2["TIT2"];
}
if (!empty($tags2["TALB"])) {
$album = $tags2["TALB"];
}
if (!empty($tags2["TPE1"])) {
$artista = $tags2["TPE1"];
}
# Al final, de todos modos imprimimos los datos
printf("Título: %s, Álbum: %s, Artista: %s, Duración: %s, KBPS: %d\n",
$titulo, $album, $artista, $duracion, $kbps);
}
}
echo $contenido;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment