Şu sayfaya yeni ve daha kullanışlı bir ID3v2 okuma classı eklendi.
MP3 dosyalarının ID3 taglarını okumak üzere tasarlanmış oldukça kullanışlı ve basit bir PHP Class’ı. Bu class sayesinde mp3 dosyalarının; sanatçı, başlık, tarz, albüm, albüm yılı, parça sırası (track no) ve yorum bilgilerine kolaylıkla ulaşabilirsiniz (elbette dosyada bu tagların tanımlanmış olması gerekiyor).
Yapmanız gereken ilk iş aşağıdaki kodları id3.class.php ismiyle kaydetmek:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | <?php ## PHP - MP3 ID3 Tag Class ## http://www.egonomik.com class ID3{ var $file_name=''; //full path to the file //the sugestion is that this path should be a //relative path var $tags; //array with ID3 tags extracted from the file var $last_error_num=0; //keep the number of the last error ocurred var $tags_count = 0; // the number of elements at the tags array /*********************/ /**private functions**/ /*********************/ function hex2bin($data) { //thankz for the one who wrote this function //If iknew your name I would say it here $len = strlen($data); for($i=0;$i<$len;$i+=2) { $newdata .= pack("C",hexdec(substr($data,$i,2))); } return $newdata; } function get_frame_size($fourBytes){ $tamanho[0] = str_pad(base_convert(substr($fourBytes,0,2),16,2),7,0,STR_PAD_LEFT); $tamanho[1] = str_pad(base_convert(substr($fourBytes,2,2),16,2),7,0,STR_PAD_LEFT); $tamanho[2] = str_pad(base_convert(substr($fourBytes,4,2),16,2),7,0,STR_PAD_LEFT); $tamanho[3] = str_pad(base_convert(substr($fourBytes,6,2),16,2),7,0,STR_PAD_LEFT); $total = $tamanho[0].$tamanho[1].$tamanho[2].$tamanho[3]; $tamanho[0] = substr($total,0,8); $tamanho[1] = substr($total,8,8); $tamanho[2] = substr($total,16,8); $tamanho[3] = substr($total,24,8); $total = $tamanho[0].$tamanho[1].$tamanho[2].$tamanho[3]; $total = base_convert($total,2,10); return $total; } function extractTags($text,&$tags){ $size = -1;//inicializando diferente de zero para não sair do while while ((strlen($text) != 0) and ($size != 0)){ //while there are tags to read and they have a meaning //while existem tags a serem tratadas e essas tags tem conteudo $ID = substr($text,0,4); $aux = substr($text,4,4); $aux = bin2hex($aux); $size = $this->get_frame_size($aux); $flags = substr($text,8,2); $info = substr($text,11,$size-1); if ($size != 0){ $tags[$ID] = $info; $this->tags_count++; } $text = substr($text,10+$size,strlen($text)); } } /********************/ /**public functions**/ /********************/ /**Constructor**/ function ID3($file_name){ $this->file_name = $file_name; $this->last_error_num = 0; } /**Read the file and put the TAGS content on $this->tags array**/ function getInfo(){ if ($this->file_name != ''){ $mp3 = @fopen($this->file_name,"r"); $header = @fread($mp3,10); if (!$header) { $this->last_error_num = 2; return false; die(); } if (substr($header,0,3) != "ID3"){ $this->last_error_num = 3; return false; die(); } $header = bin2hex($header); $version = base_convert(substr($header,6,2),16,10).".".base_convert(substr($header,8,2),16,10); $flags = base_convert(substr($header,10,2),16,2); $flags = str_pad($flags,8,0,STR_PAD_LEFT); if ($flags[7] == 1){ //echo('with Unsynchronisation<br>'); } if ($flags[6] == 1){ //echo('with Extended header<br>'); } if ($flags[5] == 1){//Esperimental tag $this->last_error_num = 4; return false; die(); } $total = $this->get_frame_size(substr($header,12,8)); $text = @fread($mp3,$total); fclose($mp3); $this->extractTags($text,$this->tags); } else{ $this->last_error_num = 1;//file not set return false; die(); } return true; } /************* * PUBLIC * Functions to get information * from the ID3 tag **************/ function getArtist(){ if (array_key_exists('TPE1',$this->tags)){ return $this->tags['TPE1']; }else{ $this->last_error_num = 5; return false; } } function getTrack(){ if (array_key_exists('TRCK',$this->tags)){ return $this->tags['TRCK']; }else{ $this->last_error_num = 5; return false; } } function getTitle(){ if (array_key_exists('TIT2',$this->tags)){ return $this->tags['TIT2']; }else{ $this->last_error_num = 5; return false; } } function getAlbum(){ if (array_key_exists('TALB',$this->tags)){ return $this->tags['TALB']; }else{ $this->last_error_num = 5; return false; } } function getYear(){ if (array_key_exists('TYER',$this->tags)){ return $this->tags['TYER']; }else{ $this->last_error_num = 5; return false; } } function getGender(){ if (array_key_exists('TCON',$this->tags)){ return $this->tags['TCON']; }else{ $this->last_error_num = 5; return false; } } } ?> |
ardından hata mesajlarının tanımlandığı dosyayı error.inc.php ismiyle kaydetmek:
1 2 3 4 5 6 7 8 9 10 | <?php $errors[0] = ""; //hata yok $errors[1] = "Dosya adı tanımlanmadı"; $errors[2] = "MP3 dosyası açılamadı"; $errors[3] = "Dosyada ID3v2 tagları bulunamadı"; $errors[4] = "Tag desteklenmiyor"; $errors[5] = "Tag bulunamadı (getInfo() fonksiyonunu çağırmayı unutmuş olabilirsiniz)"; ?> |
bu iki dosyayı “class” isimli bir klasöre sabit olarak kaydettikten sonra dilediğiniz yerden çağırabilirsiniz.
Örnek uygulama:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <html> <head> <meta http-equiv="Content-Language" content="tr"> <meta http-equiv="Content-Type" content="text/html; charset=windows-1254"> <title>Egonomik.com :: PHP MP3 ID3v2 Tag Class</title> </head> <body> <?php require("class/error.inc.php"); require("class/id3.class.php"); $dosya = "G-Flow - Biz Bize Yeteriz (Demo).mp3"; $myId3 = new ID3($dosya); if ($myId3->getInfo()){ echo ' <b><a href= "'.$dosya.'">'.$myId3->getArtist().' - '.$myId3->getTitle().'</a> ID3v2 bilgileri:</b> <br> <br> <table border="0" width="100%"> <tr> <td width="90"><b>Sanatçı</b></td> <td>: '.$myId3->getArtist().'</td> </tr> <tr> <td><b>Başlık </b> </td> <td>: '.$myId3->getTitle().'</td> </tr> <tr> <td><b>Tarz </b> </td> <td>: '.$myId3->getGender().'</td> </tr> <tr> <td><b>Albüm </b> </td> <td>: '.$myId3->getAlbum().'</td> </tr> <tr> <td><b>Yıl </b> </td> <td>: '.$myId3->getYear().'</td> </tr> <tr> <td><b>Track No </b> </td> <td>: '.$myId3->getTrack().'</td> </tr> <tr> <td><b>Yorumlar</b></td> <td>: '.$myId3->tags['COMM'].'</td> </tr> </table>'; } ?> </body> </html> |
Scriptin çalışan örneği:
Demo
Script dosyalarını indir (330kb):
Download
forzamad
peki illa mp3 mü olmalı yanii text olsun jpeg olsun falan dosyalarının özet bilgilerini nasıl çekebiliriz ?
Caner
YazarEvet bu class sadece mp3 için, daha doğrusu mp3 dosyasının id3v2 taglarını okumak için. Şu an bu bahsettiğin konuyla ilgili bir fikrim yok Ahmet, bir araştırıp bakmak lazım.