Itunes Podcasts PHP


SUBMITTED BY: Guest

DATE: July 23, 2014, 12:01 p.m.

FORMAT: Text only

SIZE: 13.1 kB

HITS: 842

  1. <?php
  2. header('Content-type: text/xml', true);
  3. $rootMP3URL = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
  4. $rootMP3URL = substr($rootMP3URL, 0, strrpos ($rootMP3URL, "/"));
  5. $folder="mp3"; //folder, where mp3s are
  6. $title = "vital's podcast";
  7. $author = "Vitali Virulaine";
  8. $link = "http://vital.pri.ee";
  9. $subtitle = "MP3 Podcasting";
  10. $summary = "MP3 feed";
  11. $language = "en-us";
  12. $copyright = "Vitali Virulaine";
  13. $owner_name = "Vitali Virulaine";
  14. $owner_email = "lequal@gmail.com";
  15. $image = "http://www.slava.pri.ee/podcast/doppler.jpg";
  16. $category = "MP3";
  17. $subcategory = "";
  18. print"<?xml version='1.0' encoding='UTF-8'?>\n";
  19. print"<rss xmlns:itunes='http://www.itunes.com/DTDs/Podcast-1.0.dtd' version='2.0'>\n";
  20. print"<channel>\n";
  21. print"<title>$title</title>\n";
  22. print"<itunes:author>$author</itunes:author>\n";
  23. print"<link>$link</link>\n";
  24. print"<itunes:subtitle>$subtitle</itunes:subtitle>\n";
  25. print"<itunes:summary>$summary</itunes:summary>\n";
  26. print"<language>$language</language>\n";
  27. print"<copyright>$copyright</copyright>\n";
  28. print"<itunes:owner>\n";
  29. print" <itunes:name>$owner_name</itunes:name>\n";
  30. print" <itunes:email>$owner_email</itunes:email>\n";
  31. print"</itunes:owner>\n";
  32. print"<itunes:image href='$image' />\n";
  33. print"<itunes:category text='$category'>\n";
  34. print"</itunes:category>\n";
  35. $dirArray = getDir("mp3/.");
  36. while (list($filename, $filedate) = each($dirArray)) {
  37. $id3tag=mp3_id("mp3/".$filename);
  38. $mp3_title = $id3tag["title"];
  39. $mp3_album = $id3tag["album"];
  40. $mp3_year = $id3tag["year"];
  41. $mp3_artist = $id3tag["artist"];
  42. $mp3_comment = $id3tag["comment"];
  43. $mp3_genre = $genres[$id3tag["genreid"]];
  44. $mp3_lenght = $id3tag["lenght"];
  45. print "<item>\n";
  46. echo ("<title>$mp3_artist - $mp3_title</title>\n");
  47. echo ("<itunes:author>$mp3_artist</itunes:author>\n");
  48. echo ("<itunes:subtitle>$mp3_subtitle</itunes:subtitle>\n");
  49. echo ("<itunes:summary>$mp3_artist - $mp3_title, album: $mp3_album (year $mp3_year)</itunes:summary>\n");
  50. echo ("<enclosure url=\"".htmlentities($rootMP3URL)."/$folder/". htmlentities(str_replace(" ", "%20", $filename)) ."\" length=\"");
  51. echo filesize($filename);
  52. echo ("\" type=\"audio/mpeg\"/>\n");
  53. echo ("<guid>$rootMP3URL/". htmlentities(str_replace(" ", "%20", $filename)) ."</guid>\n");
  54. echo ("<pubDate>".date("r",$filedate)."</pubDate>\n");
  55. echo ("<itunes:category text='$mp3_genre'>\n");
  56. echo ("</itunes:category>\n");
  57. echo ("<itunes:duration>$mp3_lenght</itunes:duration>\n");
  58. echo ("<itunes:keywords>$mp3_comment</itunes:keywords>\n");
  59. print "</item>\n";
  60. $maxFeed--;
  61. }
  62. print"</channel>\n";
  63. print"</rss>\n";
  64. function getDir($mp3Dir) {
  65. $dirArray = array();
  66. $diskdir = "./$mp3Dir/";
  67. if (is_dir($diskdir)) {
  68. $dh = opendir($diskdir);
  69. while (($file = readdir($dh)) != false ) {
  70. if (filetype($diskdir . $file) == "file" && $file[0] != ".") {
  71. if (strrchr(strtolower($file), ".") == ".mp3") {
  72. $ftime = filemtime($mp3Dir."/".$file);
  73. $dirArray[$file] = $ftime;
  74. }
  75. }
  76. }
  77. closedir($dh);
  78. }
  79. asort($dirArray);
  80. $dirArray = array_reverse($dirArray);
  81. return $dirArray;
  82. }
  83. $genres = Array('Blues','Classic Rock','Country','Dance','Disco','Funk','Grunge','Hip-Hop','Jazz',
  84. 'Metal','New Age','Oldies','Other','Pop','R&B','Rap','Reggae','Rock','Techno','Industrial','Alternative',
  85. 'Ska','Death Metal','Pranks','Soundtrack','Euro-Techno','Ambient','Trip-Hop','Vocal','Jazz+Funk','Fusion',
  86. 'Trance','Classical','Instrumental','Acid','House','Game','Sound Clip','Gospel','Noise','AlternRock',
  87. 'Bass','Soul','Punk','Space','Meditative','Instrumental Pop','Instrumental Rock','Ethnic','Gothic',
  88. 'Darkwave','Techno-Industrial','Electronic','Pop-Folk','Eurodance','Dream','Southern Rock','Comedy',
  89. 'Cult','Gangsta','Top 40','Christian Rap','Pop/Funk','Jungle','Native American','Cabaret','New Wave',
  90. 'Psychadelic','Rave','Showtunes','Trailer','Lo-Fi','Tribal','Acid Punk','Acid Jazz','Polka','Retro',
  91. 'Musical','Rock & Roll','Hard Rock','Folk','Folk-Rock','National Folk','Swing','Fast Fusion','Bebob',
  92. 'Latin','Revival','Celtic','Bluegrass','Avantgarde','Gothic Rock','Progressive Rock','Psychedelic Rock',
  93. 'Symphonic Rock','Slow Rock','Big Band','Chorus','Easy Listening','Acoustic','Humour','Speech','Chanson',
  94. 'Opera','Chamber Music','Sonata','Symphony','Booty Bass','Primus','Porn Groove','Satire','Slow Jam','Club',
  95. 'Tango','Samba','Folklore','Ballad','Power Ballad','Rhythmic Soul','Freestyle','Duet','Punk Rock','Drum Solo',
  96. 'Acapella','Euro-House','Dance Hall'
  97. );
  98. $genreids = Array(
  99. "Blues" => 0,"Classic Rock" => 1,"Country" => 2,"Dance" => 3,"Disco" => 4,"Funk" => 5,"Grunge" => 6,"Hip-Hop" => 7,
  100. "Jazz" => 8,"Metal" => 9,"New Age" => 10,"Oldies" => 11,"Other" => 12,"Pop" => 13,"R&B" => 14,"Rap" => 15,"Reggae" => 16,
  101. "Rock" => 17,"Techno" => 18,"Industrial" => 19,"Alternative" => 20,"Ska" => 21,"Death Metal" => 22,"Pranks" => 23,
  102. "Soundtrack" => 24,"Euro-Techno" => 25,"Ambient" => 26,"Trip-Hop" => 27,"Vocal" => 28,"Jazz+Funk" => 29,"Fusion" => 30,
  103. "Trance" => 31,"Classical" => 32,"Instrumental" => 33,"Acid" => 34,"House" => 35,"Game" => 36,"Sound Clip" => 37,"Gospel" => 38,
  104. "Noise" => 39,"AlternRock" => 40,"Bass" => 41,"Soul" => 42,"Punk" => 43,"Space" => 44,"Meditative" => 45,"Instrumental Pop" => 46,
  105. "Instrumental Rock" => 47,"Ethnic" => 48,"Gothic" => 49,"Darkwave" => 50,"Techno-Industrial" => 51,"Electronic" => 52,
  106. "Pop-Folk" => 53,"Eurodance" => 54,"Dream" => 55,"Southern Rock" => 56,"Comedy" => 57,"Cult" => 58,"Gangsta" => 59,
  107. "Top 40" => 60,"Christian Rap" => 61,"Pop/Funk" => 62,"Jungle" => 63,"Native American" => 64,"Cabaret" => 65,"New Wave" => 66,
  108. "Psychadelic" => 67,"Rave" => 68,"Showtunes" => 69,"Trailer" => 70,"Lo-Fi" => 71,"Tribal" => 72,"Acid Punk" => 73,
  109. "Acid Jazz" => 74,"Polka" => 75,"Retro" => 76,"Musical" => 77,"Rock & Roll" => 78,"Hard Rock" => 79,"Folk" => 80,
  110. "Folk-Rock" => 81,"National Folk" => 82,"Swing" => 83,"Fast Fusion" => 84,"Bebob" => 85,"Latin" => 86,"Revival" => 87,
  111. "Celtic" => 88,"Bluegrass" => 89,"Avantgarde" => 90,"Gothic Rock" => 91,"Progressive Rock" => 92,"Psychedelic Rock" => 93,
  112. "Symphonic Rock" => 94,"Slow Rock" => 95,"Big Band" => 96,"Chorus" => 97,"Easy Listening" => 98,"Acoustic" => 99,
  113. "Humour" => 100,"Speech" => 101,"Chanson" => 102,"Opera" => 103,"Chamber Music" => 104,"Sonata" => 105,"Symphony" => 106,
  114. "Booty Bass" => 107,"Primus" => 108,"Porn Groove" => 109,"Satire" => 110,"Slow Jam" => 111,"Club" => 112,"Tango" => 113,
  115. "Samba" => 114,"Folklore" => 115,"Ballad" => 116,"Power Ballad" => 117,"Rhythmic Soul" => 118,"Freestyle" => 119,
  116. "Duet" => 120,"Punk Rock" => 121,"Drum Solo" => 122,"Acapella" => 123,"Euro-House" => 124,"Dance Hall" => 125
  117. );
  118. $version=Array("00"=>2.5, "10"=>2, "11"=>1);
  119. $layer =Array("01"=>3, "10"=>2, "11"=>1);
  120. $crc=Array("Yes", "No");
  121. $bitrate["0001"]=Array(32,32,32,32,8,8);
  122. $bitrate["0010"]=Array(64,48,40,48,16,16);
  123. $bitrate["0011"]=Array(96,56,48,56,24,24);
  124. $bitrate["0100"]=Array(128,64,56,64,32,32);
  125. $bitrate["0101"]=Array(160,80,64,80,40,40);
  126. $bitrate["0110"]=Array(192,96,80,96,48,48);
  127. $bitrate["0111"]=Array(224,112,96,112,56,56);
  128. $bitrate["1000"]=Array(256,128,112,128,64,64);
  129. $bitrate["1001"]=Array(288,160,128,144,80,80);
  130. $bitrate["1010"]=Array(320,192,160,160,96,96);
  131. $bitrate["1011"]=Array(352,224,192,176,112,112);
  132. $bitrate["1100"]=Array(384,256,224,192,128,128);
  133. $bitrate["1101"]=Array(416,320,256,224,144,144);
  134. $bitrate["1110"]=Array(448,384,320,256,160,160);
  135. $bitindex=Array("1111"=>"0","1110"=>"1","1101"=>"2","1011"=>"3","1010"=>"4","1001"=>"5","0011"=>"3","0010"=>4,"0001"=>"5");
  136. $freq["00"]=Array("11"=>44100,"10"=>22050,"00"=>11025);
  137. $freq["01"]=Array("11"=>48000,"10"=>24000,"00"=>12000);
  138. $freq["10"]=Array("11"=>32000,"10"=>16000,"00"=>8000);
  139. $mode=Array("00"=>"Stereo","01"=>"Joint stereo","10"=>"Dual channel","11"=>"Mono");
  140. $copy=Array("No","Yes");
  141. function strip_nulls( $str ) {
  142. $res = explode( chr(0), $str );
  143. return chop( $res[0] );
  144. }
  145. function mp3_id($file) {
  146. global $version, $layer, $crc, $bitrate, $bitindex, $freq, $mode, $copy, $genres;
  147. if(!$f=@fopen($file, "r")) { return -1; break; } else {
  148. $tmp=fread($f,4);
  149. if($tmp=="RIFF") {
  150. $idtag["ftype"]="Wave";
  151. fseek($f, 0);
  152. $tmp=fread($f,128);
  153. $x=StrPos($tmp, "data");
  154. fseek($f, $x+8);
  155. $tmp=fread($f,4);
  156. }
  157. for($y=0;$y<4;$y++) {
  158. $x=decbin(ord($tmp[$y]));
  159. for($i=0;$i<(8-StrLen($x));$i++) {$x.="0";}
  160. $bajt.=$x;
  161. }
  162. if(substr($bajt,1,11)!="11111111111") {
  163. fseek($f, 4);
  164. $tmp=fread($f,2048);
  165. for($i=0;$i<2048;$i++){
  166. if(ord($tmp[$i])==255 && substr(decbin(ord($tmp[$i+1])),0,3)=="111") {
  167. $tmp=substr($tmp, $i,4);
  168. $bajt="";
  169. for($y=0;$y<4;$y++) {
  170. $x=decbin(ord($tmp[$y]));
  171. for($i=0;$i<(8-StrLen($x));$i++) {$x.="0";}
  172. $bajt.=$x;
  173. }
  174. break;
  175. }
  176. }
  177. }
  178. if($bajt=="") {
  179. return -1;
  180. break;
  181. }
  182. $len=filesize($file);
  183. $idtag["version"]=$version[substr($bajt,11,2)];
  184. $idtag["layer"]=$layer[substr($bajt,13,2)];
  185. $idtag["crc"]=$crc[$bajt[15]];
  186. $idtag["bitrate"]=$bitrate[substr($bajt,16,4)][$bitindex[substr($bajt,11,4)]];
  187. $idtag["frequency"]=$freq[substr($bajt,20,2)][substr($bajt,11,2)];
  188. $idtag["padding"]=$copy[$bajt[22]];
  189. $idtag["mode"]=$mode[substr($bajt,24,2)];
  190. $idtag["copyright"]=$copy[$bajt[28]];
  191. $idtag["original"]=$copy[$bajt[29]];
  192. if($idtag["layer"]==1) {
  193. $fsize=(12*($idtag["bitrate"]*1000)/$idtag["frequency"]+$idtag["padding"])*4; }
  194. else {
  195. $fsize=144*(($idtag["bitrate"]*1000)/$idtag["frequency"]+$idtag["padding"]);}
  196. $idtag["lenght_sec"]=round($len/Round($fsize)/38.37);
  197. $idtag["lenght"]=date("i:s",round($len/Round($fsize)/38.37));
  198. if(!$len) $len=filesize($file);
  199. fseek($f, $len-128);
  200. $tag = fread($f, 128);
  201. if(Substr($tag,0,3)=="TAG") {
  202. $idtag["file"]=$file;
  203. $idtag["tag"]=-1;
  204. $idtag["title"]=strip_nulls( Substr($tag,3,30) );
  205. $idtag["artist"]=strip_nulls( Substr($tag,33,30) );
  206. $idtag["album"]=strip_nulls( Substr($tag,63,30) );
  207. $idtag["year"]=strip_nulls( Substr($tag,93,4) );
  208. $idtag["comment"]=strip_nulls( Substr($tag,97,30) );
  209. if ( strlen( $idtag["comment"] ) < 29 ) {
  210. if ( Ord(Substr($tag,125,1)) == chr(0) )
  211. $idtag["track"]=Ord(Substr($tag,126,1));
  212. else
  213. $idtag["track"]=0;
  214. } else {
  215. $idtag["track"]=0;
  216. }
  217. $idtag["genreid"]=Ord(Substr($tag,127,1));
  218. $idtag["genre"]=$genres[$idtag["genreid"]];
  219. $idtag["filesize"]=$len;
  220. } else {
  221. $idtag["tag"]=0;
  222. }
  223. if(!$idtag["title"]) {
  224. $idtag["title"]=Str_replace("\\","/", $file);
  225. $idtag["title"]=substr($idtag["title"],strrpos($idtag["title"],"/")+1, 255);
  226. }
  227. fclose($f);
  228. return $idtag;
  229. }
  230. }
  231. function str_padtrunc( $str, $len, $with = " " ) {
  232. $l = strlen( $str );
  233. if ( $len < $l ) {
  234. return substr( $str, 0, $len );
  235. } elseif ( $len > $l ) {
  236. $s = "";
  237. for ( $i = 0; $i < ($len - $l); $i++) {
  238. $s .= $with;
  239. }
  240. return $str . $s;
  241. } else
  242. return $str;
  243. }
  244. ?>

comments powered by Disqus