Search code examples
phpxampppeclid3id3v2

PECL extension installation on XAMPP (Windows)


I need some help please, have tried but am not even close to thinking about winning.

I need to get the ID3 PECL running on XAMPP(v7.4.5) for Windows.

I have spent the day yesterday trying to work through the PECL articles on the PHP.net website and it unfortunately does not make much sense to me, as my knowledge is not on that level yet. I have also tried looking for a dll file for the ID3 extension, but cannot find any. Only the source files are available for download for the ID3 PECL page.

Is there perhaps someone with enough patience who can explain the process in more layman's terms... dumb it down for me as it were?

Any and all help would be greatly appreciated.

EDIT

Can someone be of assistance?

With the help of Lelio I now have a way to extract info from an MP3, I have added two lines to access the Genre as well as Attached Picture blocks. The Genre block works. The Attached Picture block however, does not.

Here is the updated code which is saved as a separate php file called script-mp3-tag-reader.php:

<?php
    function tagReader($file)
    {
        $id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT","TCON","APIC");
        $id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT","TCO");
        $fsize = filesize($file);
        $fd = fopen($file,"r");
        $tag = fread($fd,$fsize);
        $tmp = "";
        fclose($fd);
        if (substr($tag,0,3) == "ID3")
        {
            $result['FileName'] = $file;
            $result['TAG'] = substr($tag,0,3);
            $result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1)));
        }
        if($result['Version'] == "4.0" || $result['Version'] == "3.0")
        {
            for ($i=0;$i<count($id3v23);$i++)
            {
                if (strpos($tag,$id3v23[$i].chr(0))!= FALSE)
                {
                    $pos = strpos($tag, $id3v23[$i].chr(0));
                    $len = hexdec(bin2hex(substr($tag,($pos+5),3)));
                    $data = substr($tag, $pos, 9+$len);
                    for ($a=0;$a<strlen($data);$a++)
                    {
                        $char = substr($data,$a,1);
                        if($char >= " " && $char <= "~")
                        {
                            $tmp.=$char;
                        }
                    }
                    if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TPE1") $result['Artist'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TRCK") $result['TrackNo'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7);
                    if(substr($tmp,0,4) == "TCON") $result['Genre'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "APIC") $result['AttachedPicture'] = substr($tmp,4);
                    $tmp = "";
                }
            }
        }
        if($result['Version'] == "2.0")
        {
            for ($i=0;$i<count($id3v22);$i++)
            {
                if (strpos($tag,$id3v22[$i].chr(0))!= FALSE)
                {
                    $pos = strpos($tag, $id3v22[$i].chr(0));
                    $len = hexdec(bin2hex(substr($tag,($pos+3),3)));
                    $data = substr($tag, $pos, 6+$len);
                    for ($a=0;$a<strlen($data);$a++)
                    {
                        $char = substr($data,$a,1);
                        if($char >= " " && $char <= "~")
                        {
                            $tmp.=$char;
                        }
                    }
                    if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TP1") $result['Artist'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TRK") $result['TrackNo'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6);
                    if(substr($tmp,0,3) == "TCO") $result['Genre'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "PIC") $result['AttachedPicture'] = base64_encode(substr($tmp,3));
                    $tmp = "";
                }
            }
        }
        return $result;
    }
?>

In my index.php file I then load and use the above PHP script as follows:

<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/script/php/script-mp3-tag-reader.php');
    $mp3 = $_SERVER['DOCUMENT_ROOT'].'/media/audio/mp3/test_audio.mp3';
    $myResult = tagReader($mp3);
    print_r($myResult);
?>

When looking at print_r($myresult); one can see that the information is indeed there and correct... just not the Attached Image part. I seem to be extracting far more than is needed and do not know how to access only what I need.


Solution

  • Something like this should do the trick. It is not recently tested but should work to read the data you are looking for from a mp3 file.

    function tagReader($file){
        $id3v23 = array("TIT2","TALB","TPE1","TRCK","TDRC","TLEN","USLT");
        $id3v22 = array("TT2","TAL","TP1","TRK","TYE","TLE","ULT");
        $fsize = filesize($file);
        $fd = fopen($file,"r");
        $tag = fread($fd,$fsize);
        $tmp = "";
        fclose($fd);
        if (substr($tag,0,3) == "ID3") {
            $result['FileName'] = $file;
            $result['TAG'] = substr($tag,0,3);
            $result['Version'] = hexdec(bin2hex(substr($tag,3,1))).".".hexdec(bin2hex(substr($tag,4,1)));
        }
        if($result['Version'] == "4.0" || $result['Version'] == "3.0"){
            for ($i=0;$i<count($id3v23);$i++){
                if (strpos($tag,$id3v23[$i].chr(0))!= FALSE){
                    $pos = strpos($tag, $id3v23[$i].chr(0));
                    $len = hexdec(bin2hex(substr($tag,($pos+5),3)));
                    $data = substr($tag, $pos, 9+$len);
                    for ($a=0;$a<strlen($data);$a++){
                        $char = substr($data,$a,1);
                        if($char >= " " && $char <= "~") $tmp.=$char;
                    }
                    if(substr($tmp,0,4) == "TIT2") $result['Title'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TALB") $result['Album'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TPE1") $result['Author'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TRCK") $result['Track'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TDRC") $result['Year'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "TLEN") $result['Lenght'] = substr($tmp,4);
                    if(substr($tmp,0,4) == "USLT") $result['Lyric'] = substr($tmp,7);
                    $tmp = "";
                }
            }
        }
        if($result['Version'] == "2.0"){
            for ($i=0;$i<count($id3v22);$i++){
                if (strpos($tag,$id3v22[$i].chr(0))!= FALSE){
                    $pos = strpos($tag, $id3v22[$i].chr(0));
                    $len = hexdec(bin2hex(substr($tag,($pos+3),3)));
                    $data = substr($tag, $pos, 6+$len);
                    for ($a=0;$a<strlen($data);$a++){
                        $char = substr($data,$a,1);
                        if($char >= " " && $char <= "~") $tmp.=$char;
                    }
                    if(substr($tmp,0,3) == "TT2") $result['Title'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TAL") $result['Album'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TP1") $result['Author'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TRK") $result['Track'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TYE") $result['Year'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "TLE") $result['Lenght'] = substr($tmp,3);
                    if(substr($tmp,0,3) == "ULT") $result['Lyric'] = substr($tmp,6);
                    $tmp = "";
                }
            }   
        }
        return $result;
    }