Created
September 10, 2013 05:21
-
-
Save fastcodecoq/6505292 to your computer and use it in GitHub Desktop.
Función php para leer ID3
This file contains 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 | |
function readTags($file) { | |
$fp = fopen($file, "r"); | |
$start = fread($fp, 3); | |
$size = filesize($file); | |
fseek($fp, $size-128); | |
$end = fread($fp, 128); | |
if (strpos($start, "ID3") === 0) { | |
$tags = readID3v2($fp); | |
if (($tags["artist"] == "" || $tags["title"] == "") && strpos($end, "TAG") === 0) { | |
$tags2 = readID3v1($end); | |
if ($tags["artist"] == "" || $tags2["artist"] != "") { | |
$tags["artist"] = $tags2["artist"]; | |
} | |
if ($tags["title"] == "" || $tags2["title"] != "") { | |
$tags["title"] = $tags2["title"]; | |
} | |
} | |
} | |
else if (strpos($end, "TAG") === 0) $tags = readID3v1($end); | |
else $tags = array("artist" => "", "title" => "", "album" => "", "year" => ""); | |
fclose($fp); | |
foreach ($tags as $key=>$value) { | |
if (strlen($tags[$key]) == 1) $tags[$key] = ""; | |
} | |
return $tags; | |
} | |
function readID3v1($data) { | |
$tags = array(); | |
$data = substr($data, 3, 97); | |
$data = str_split($data, 30); | |
$tags = array("artist" => $data[1], "title" => $data[0], "album" => $data[2], "year" => $data[3]); | |
return $tags; | |
} | |
function readID3v2($fp) { | |
$tags = array(); | |
fseek($fp, 0); | |
$data = fread($fp, 2048); | |
$pos = 3; | |
fseek($fp, $pos); | |
while (fread($fp, 1) != "v") { | |
$pos++; | |
fseek($fp, $pos); | |
} | |
$pos++; | |
if ($pos > 12) { | |
$pos = 3; | |
fseek($fp, $pos); | |
while (fread($fp, 1) != "T" && fread($fp, 1) != "C") { | |
$pos++; | |
fseek($fp, $pos); | |
} | |
fseek($fp, $pos); | |
} | |
$header = ""; | |
$a = array(); | |
$c = 0; | |
while($c < 20) { | |
$header = fread($fp, 4); | |
fseek($fp, $pos+7); | |
$len = ord(fread($fp, 1)); | |
fseek($fp, $pos+10); | |
if ($len > 0) $value = fread($fp, $len); | |
else $value = ""; | |
$pos = $pos + 10 + $len; | |
fseek($fp, $pos); | |
//echo $header." - > ".$value."<br>"; | |
$orig = $value; | |
if (preg_match("/[]/", $value)) $value = substr(mb_convert_encoding(substr($value, 1), "windows-1251", "Unicode"), 1); | |
if (preg_match("/[]/", $value)) $value = ""; | |
if (strpos($value, "ÿþ") == 1) $value = substr($value, 3); | |
$a[$header] = $value; | |
$c++; | |
} | |
if (!isset($a["TIT1"]) && isset($a["TIT2"])) $a["TIT1"] = $a["TIT2"]; | |
if (!isset($a["TPE1"]) && isset($a["TPE2"])) $a["TPE1"] = $a["TPE2"]; | |
if (!isset($a["TIT1"])) $a["TIT1"] = ""; | |
if (!isset($a["TPE1"])) $a["TPE1"] = ""; | |
if (!isset($a["TALB"])) $a["TALB"] = ""; | |
if (!isset($a["TYER"])) $a["TYER"] = ""; | |
$tags = array("artist" => $a["TPE1"], "title" => $a["TIT1"], "album" => $a["TALB"], "year" => $a["TYER"]); | |
return $tags; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment