웹마스터 팁
page_full_width" class="col-xs-12" |cond="$__Context->page_full_width">
폴더에 올린 mp3를 자동으로 podcast로 만들어주는 소스... ^^
2010.09.11 00:16
말그대로 폴더에 올린 mp3를 자동으로 podcast로 만들어주는 소스입니다.이번에 아이폰4 출시를 기념으로 이것저것 소스 수정해서 만들었습니다.
특정 폴더에 이 소스올리시고, 해당경로에 mp3를 태그먹여서 올려주시면 자동으로 podcast용 rss 피드를 생성해줍니다.페이지 방식과 xml 파일생성 방식을 병행할 수 있는데 저는 페이지 방식을 선호합니다.바로바로 피드가 생성되거든요..
버그가 있는데요, 1. 한글태그값을 가져오지 못합니다. 제가 문자인코딩부분을 잘 처리못해서 그런것 같습니다.2. 한글파일처리가 미흡합니다. 링크는 나오는데 다운이 안됩니다. ㅠㅠ 영문파일명으로 사용해야합니다.
수정해서 올려주실 분이 있으시다면 감사하겠습니다.
아래 소스...
<?php
/* Script: podcastxml_libs.php * Description: Lib's used for podcastxml.php script * History: * Author Date Description * Sherif Mansour 5/31/2008 Initial Version */
// Functions and Classes function stripJunk ($text) { $outText = ""; // Strip non-text characters for ($c=0; $c<strlen($text); $c++) { if (ord($text[$c]) >= 32 AND ord($text[$c]) <= 122) $outText.=$text[$c]; }
return $outText; }
class CMP3File { //properties var $title; var $artist; var $album; var $year; var $comment; var $genre; function getid3 ($file) { // read the ID3 tag from an MP3 file if (file_exists($file)) { //after verifying the file exists, $id_start = filesize($file) - 128; $fp = fopen($file, "r"); fseek($fp, $id_start); $tag = fread($fp,3); if ($tag == "TAG") { $this->title = stripJunk(trim(fread($fp, 30))); $this->artist = stripJunk(trim(fread($fp, 30))); $this->album = stripJunk(trim(fread($fp, 30))); $this->year = stripJunk(trim(fread($fp, 4))); $this->comment = stripJunk(trim(fread($fp, 30))); $this->genre = stripJunk(trim(fread($fp, 1))); fclose($fp); return true; } else { //there is no ID3 tag in the file fclose($fp); return false; } } else //the file doesn't exist return false; } }
function getDir($mp3Dir) { // Returns directory as array[file]=date in newest to oldest order $dirArray = array(); $diskdir = "./$mp3Dir/"; if (is_dir($diskdir)) { $dh = opendir($diskdir); while (($file = readdir($dh)) != false ) { if (filetype($diskdir . $file) == "file" && $file[0] != ".") { if (stristr($file, ".") == ".mp3") { $ftime = filemtime($mp3Dir."/".$file); $dirArray[$file] = $ftime; } } } closedir($dh); } //asort($dirArray); Use this if you want sorted by last modified date ksort($dirArray); //use this if you want sorted by the key (IE the filename) $dirArray = array_reverse($dirArray); return $dirArray; }
/* Script: podcastxml.php * Description: Create an XML feed for podcast by reading a folder of MP3's and reading the ID3 tag for each file. * History: * Author Date Description * Sherif Mansour 05/11/2008 Initial Version * Sherif Mansour 3/10/2009 Fixed HTML Entities Bug */
/* START: Script Configuration */ error_reporting(E_ALL); //Error reporting flag $cfg = array(); //The Main Config Array
/* START: Method of operation.*/ /* This defines how you want the script to work. You have two options: * 'file' OR 'stdout' * If you chose 'file': This means every time this script runs, it will write the podcast to a file you specify. Use this if there are many podcasts (better perfomance, and schedule a cron job) * If you choose 'stdout': This will write the podcast XML to the browser using this file itself. Us this if there arent many podcasts, and can affort to have this script run every time someone access it. */ $cfg['operation'] = "stdout"; //Values can be 'file' OR 'stdout' $cfg['file_name'] = "podcast.xml"; //If you have selected file, enter the name of the file $cfg['file_location'] = "./"; //If you have selected file, enter the relative location of where you want to write the xml file. Dont forget trailing slash! //NOTE: If you have selected the 'file' mode of operation, make sure you CHMOD that folder to 777 where you want to write to! /* END: Method of operation. */
/* START: Config values */ $cfg['max_feed'] = 10; //Maximum number of files to display in the feed $cfg['tag_title'] = "수원중앙침례교회 PODCAST"; //The Podcast Title $cfg['tag_description']= "과연! 그 교회, 중앙교회 PODCAST 입니다."; //The Poddcast Description $cfg['tag_link'] = "http://central.or.kr/podcasts"; //The URL for the person providing the podcast $cfg['root_url'] = "http://central.or.kr/podcasts"; //The URL for the person providing the podcast $cfg['tag_lang'] = "kr"; //Language Tag $cfg['tag_copyright'] = "수원중앙침례교회"; //Copyright $cfg['tag_docs'] = "http://central.or.kr/podcasts/"; //Language Tag $cfg['tag_webMaster'] = "admin@central.or.kr"; //Author's email $cfg['tag_generator']= "PHP Podcast Generator - blog.sherifmansour.com"; //Generator name (This script!) $cfg['tag_ttl']= 60; //Podcast Time to Live $cfg['abs_url'] = "./"; //Absolute reference of where the MP3's are stored on the server $cfg['tag_author'] = "수원중앙침례교회 음향실"; //Lib files used for this script $cfg['tag_subtitle'] = "수원중앙침례교회 PODCAST"; //Lib files used for this script $cfg['tag_summary'] = "수원중앙침례교회의 각종 예배와 행사, 광고, 기사등을 방송합니다."; //Lib files used for this script $cfg['tag_owner_name'] = "수원중앙침례교회"; //Lib files used for this script $cfg['tag_owner_email'] = "admin@central.or.kr"; $cfg['tag_image'] = ""; //Lib files used for this script $cfg['tag_category'] = "종교/기독교"; //Lib files used for this script
/* END: Config values */
/* END: Script Configuration */
/***************************************************************** * DO NOT EDIT PAST THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING *****************************************************************/
/* START: Script, do not edit past this point */ $output_string = "";
/* Compile header string */ $cfg['root_url'] = substr($cfg['root_url'], 0, strrpos ($cfg['root_url'], "/")); // Trim off script name itself $output_string .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $output_string .= "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">\n"; $output_string .=" \n"; $output_string .=" <channel>\n\n"; $output_string .=" <title>".$cfg['tag_title']."</title>\n"; $output_string .=" <description>".$cfg['tag_description']."</description>\n"; $output_string .=" <link>".$cfg['tag_link']."</link>\n"; $output_string .=" <language>".$cfg['tag_lang']."</language>\n"; $output_string .=" <copyright>".$cfg['tag_copyright']."</copyright>\n"; $output_string .=" <lastBuildDate>" . date("r") ."</lastBuildDate>\n"; $output_string .=" <pubDate>" . date("r") ."</pubDate>\n"; $output_string .=" <docs>".$cfg['tag_docs']."</docs>\n"; $output_string .=" <webMaster>".$cfg['tag_webMaster'+ '+ ']."</webMaster>\n"; $output_string .=" \n"; $output_string .=" <itunes:author>".$cfg['tag_author']."</itunes:author>\n"; $output_string .=" <itunes:subtitle>".$cfg['tag_subtitle']."</itunes:subtitle>\n"; $output_string .=" <itunes:summary>".$cfg['tag_summary']."</itunes:summary>\n"; $output_string .=" \n\n"; $output_string .=" <itunes:owner>\n"; $output_string .=" <itunes:name>".$cfg['tag_owner_name']."</itunes:name>\n"; $output_string .=" <itunes:email>".$cfg['tag_owner_email']."</itunes:email>\n"; $output_string .=" </itunes:owner>\n"; $output_string .=" \n"; $output_string .=" \n"; $output_string .=" <itunes:explicit>No</itunes:explicit>\n\n"; $output_string .=" <itunes:image herf=\"".$cfg['tag_image']."\" />\n\n"; $output_string .=" <itunes:category>".$cfg['tag_category']."</itunes:category>\n\n";
/* Loop through the folder and compile the body tags in a string for each MP3 */ $dirArray = getDir($cfg['abs_url']); // Get a list of the current directory
while (list($filename, $filedate) = each($dirArray) AND $cfg['max_feed'] > 0) { $mp3file = new CMP3File; $mp3file->getid3 ($filename); $output_string .= " <item>\n"; $output_string .= " <title>".htmlentities($mp3file->title)."</title>\n"; $output_string .= " <title>".iconv("euc-kr","utf-8","$mp3file->title")."</title>\n"; $output_string .= " <title>".iconv("utf-8","euc-kr","$mp3file->title")."</title>\n"; $output_string .= " <link>".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."</link>\n"; $output_string .= " <guid>".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."</guid>\n"; $output_string .= " <description>by ".htmlentities($mp3file->artist). htmlentities($mp3file->album). htmlentities($mp3file->comment)."</description>\n"; $output_string .= " <enclosure url=\"".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."\" length=\""; $output_string .= filesize($filename); $output_string .= "\" type=\"audio/mpeg\"/>\n"; // Training slash for XML $output_string .=" <category>".$cfg['tag_category']."</category>\n"; $output_string .= " <pubDate>".date("r",$filedate)."</pubDate>\n"; $output_string .= " </item>\n\n"; $cfg['max_feed']--; }
/* Compile the footer string*/ $output_string .= " </channel>\n</rss>\n";
/* START: Writing string to desired output */ switch ($cfg['operation']){ case 'stdout': header('Content-type: text/xml; charset=UTF-8', true); echo $output_string; break; case 'file': $output_file = $cfg['+ 'file_location'].$cfg['file_name']; $fh = fopen($output_file, 'w') or die("Error: Cant open file for writing. Check you have the right file, file path and directory is writable.");; ftruncate($fh, 0); fwrite($fh, $output_string); fclose($fh); break; default: echo "Error: You havent selected a valid mode of operation in the cfg['operation'] array. Possible values are 'file'|'stdout'"; } /* END: Writing to desired output *//* END: Script */?>
특정 폴더에 이 소스올리시고, 해당경로에 mp3를 태그먹여서 올려주시면 자동으로 podcast용 rss 피드를 생성해줍니다.페이지 방식과 xml 파일생성 방식을 병행할 수 있는데 저는 페이지 방식을 선호합니다.바로바로 피드가 생성되거든요..
버그가 있는데요, 1. 한글태그값을 가져오지 못합니다. 제가 문자인코딩부분을 잘 처리못해서 그런것 같습니다.2. 한글파일처리가 미흡합니다. 링크는 나오는데 다운이 안됩니다. ㅠㅠ 영문파일명으로 사용해야합니다.
수정해서 올려주실 분이 있으시다면 감사하겠습니다.
아래 소스...
<?php
/* Script: podcastxml_libs.php * Description: Lib's used for podcastxml.php script * History: * Author Date Description * Sherif Mansour 5/31/2008 Initial Version */
// Functions and Classes function stripJunk ($text) { $outText = ""; // Strip non-text characters for ($c=0; $c<strlen($text); $c++) { if (ord($text[$c]) >= 32 AND ord($text[$c]) <= 122) $outText.=$text[$c]; }
return $outText; }
class CMP3File { //properties var $title; var $artist; var $album; var $year; var $comment; var $genre; function getid3 ($file) { // read the ID3 tag from an MP3 file if (file_exists($file)) { //after verifying the file exists, $id_start = filesize($file) - 128; $fp = fopen($file, "r"); fseek($fp, $id_start); $tag = fread($fp,3); if ($tag == "TAG") { $this->title = stripJunk(trim(fread($fp, 30))); $this->artist = stripJunk(trim(fread($fp, 30))); $this->album = stripJunk(trim(fread($fp, 30))); $this->year = stripJunk(trim(fread($fp, 4))); $this->comment = stripJunk(trim(fread($fp, 30))); $this->genre = stripJunk(trim(fread($fp, 1))); fclose($fp); return true; } else { //there is no ID3 tag in the file fclose($fp); return false; } } else //the file doesn't exist return false; } }
function getDir($mp3Dir) { // Returns directory as array[file]=date in newest to oldest order $dirArray = array(); $diskdir = "./$mp3Dir/"; if (is_dir($diskdir)) { $dh = opendir($diskdir); while (($file = readdir($dh)) != false ) { if (filetype($diskdir . $file) == "file" && $file[0] != ".") { if (stristr($file, ".") == ".mp3") { $ftime = filemtime($mp3Dir."/".$file); $dirArray[$file] = $ftime; } } } closedir($dh); } //asort($dirArray); Use this if you want sorted by last modified date ksort($dirArray); //use this if you want sorted by the key (IE the filename) $dirArray = array_reverse($dirArray); return $dirArray; }
/* Script: podcastxml.php * Description: Create an XML feed for podcast by reading a folder of MP3's and reading the ID3 tag for each file. * History: * Author Date Description * Sherif Mansour 05/11/2008 Initial Version * Sherif Mansour 3/10/2009 Fixed HTML Entities Bug */
/* START: Script Configuration */ error_reporting(E_ALL); //Error reporting flag $cfg = array(); //The Main Config Array
/* START: Method of operation.*/ /* This defines how you want the script to work. You have two options: * 'file' OR 'stdout' * If you chose 'file': This means every time this script runs, it will write the podcast to a file you specify. Use this if there are many podcasts (better perfomance, and schedule a cron job) * If you choose 'stdout': This will write the podcast XML to the browser using this file itself. Us this if there arent many podcasts, and can affort to have this script run every time someone access it. */ $cfg['operation'] = "stdout"; //Values can be 'file' OR 'stdout' $cfg['file_name'] = "podcast.xml"; //If you have selected file, enter the name of the file $cfg['file_location'] = "./"; //If you have selected file, enter the relative location of where you want to write the xml file. Dont forget trailing slash! //NOTE: If you have selected the 'file' mode of operation, make sure you CHMOD that folder to 777 where you want to write to! /* END: Method of operation. */
/* START: Config values */ $cfg['max_feed'] = 10; //Maximum number of files to display in the feed $cfg['tag_title'] = "수원중앙침례교회 PODCAST"; //The Podcast Title $cfg['tag_description']= "과연! 그 교회, 중앙교회 PODCAST 입니다."; //The Poddcast Description $cfg['tag_link'] = "http://central.or.kr/podcasts"; //The URL for the person providing the podcast $cfg['root_url'] = "http://central.or.kr/podcasts"; //The URL for the person providing the podcast $cfg['tag_lang'] = "kr"; //Language Tag $cfg['tag_copyright'] = "수원중앙침례교회"; //Copyright $cfg['tag_docs'] = "http://central.or.kr/podcasts/"; //Language Tag $cfg['tag_webMaster'] = "admin@central.or.kr"; //Author's email $cfg['tag_generator']= "PHP Podcast Generator - blog.sherifmansour.com"; //Generator name (This script!) $cfg['tag_ttl']= 60; //Podcast Time to Live $cfg['abs_url'] = "./"; //Absolute reference of where the MP3's are stored on the server $cfg['tag_author'] = "수원중앙침례교회 음향실"; //Lib files used for this script $cfg['tag_subtitle'] = "수원중앙침례교회 PODCAST"; //Lib files used for this script $cfg['tag_summary'] = "수원중앙침례교회의 각종 예배와 행사, 광고, 기사등을 방송합니다."; //Lib files used for this script $cfg['tag_owner_name'] = "수원중앙침례교회"; //Lib files used for this script $cfg['tag_owner_email'] = "admin@central.or.kr"; $cfg['tag_image'] = ""; //Lib files used for this script $cfg['tag_category'] = "종교/기독교"; //Lib files used for this script
/* END: Config values */
/* END: Script Configuration */
/***************************************************************** * DO NOT EDIT PAST THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING *****************************************************************/
/* START: Script, do not edit past this point */ $output_string = "";
/* Compile header string */ $cfg['root_url'] = substr($cfg['root_url'], 0, strrpos ($cfg['root_url'], "/")); // Trim off script name itself $output_string .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; $output_string .= "<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">\n"; $output_string .=" \n"; $output_string .=" <channel>\n\n"; $output_string .=" <title>".$cfg['tag_title']."</title>\n"; $output_string .=" <description>".$cfg['tag_description']."</description>\n"; $output_string .=" <link>".$cfg['tag_link']."</link>\n"; $output_string .=" <language>".$cfg['tag_lang']."</language>\n"; $output_string .=" <copyright>".$cfg['tag_copyright']."</copyright>\n"; $output_string .=" <lastBuildDate>" . date("r") ."</lastBuildDate>\n"; $output_string .=" <pubDate>" . date("r") ."</pubDate>\n"; $output_string .=" <docs>".$cfg['tag_docs']."</docs>\n"; $output_string .=" <webMaster>".$cfg['tag_webMaster'+ '+ ']."</webMaster>\n"; $output_string .=" \n"; $output_string .=" <itunes:author>".$cfg['tag_author']."</itunes:author>\n"; $output_string .=" <itunes:subtitle>".$cfg['tag_subtitle']."</itunes:subtitle>\n"; $output_string .=" <itunes:summary>".$cfg['tag_summary']."</itunes:summary>\n"; $output_string .=" \n\n"; $output_string .=" <itunes:owner>\n"; $output_string .=" <itunes:name>".$cfg['tag_owner_name']."</itunes:name>\n"; $output_string .=" <itunes:email>".$cfg['tag_owner_email']."</itunes:email>\n"; $output_string .=" </itunes:owner>\n"; $output_string .=" \n"; $output_string .=" \n"; $output_string .=" <itunes:explicit>No</itunes:explicit>\n\n"; $output_string .=" <itunes:image herf=\"".$cfg['tag_image']."\" />\n\n"; $output_string .=" <itunes:category>".$cfg['tag_category']."</itunes:category>\n\n";
/* Loop through the folder and compile the body tags in a string for each MP3 */ $dirArray = getDir($cfg['abs_url']); // Get a list of the current directory
while (list($filename, $filedate) = each($dirArray) AND $cfg['max_feed'] > 0) { $mp3file = new CMP3File; $mp3file->getid3 ($filename); $output_string .= " <item>\n"; $output_string .= " <title>".htmlentities($mp3file->title)."</title>\n"; $output_string .= " <title>".iconv("euc-kr","utf-8","$mp3file->title")."</title>\n"; $output_string .= " <title>".iconv("utf-8","euc-kr","$mp3file->title")."</title>\n"; $output_string .= " <link>".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."</link>\n"; $output_string .= " <guid>".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."</guid>\n"; $output_string .= " <description>by ".htmlentities($mp3file->artist). htmlentities($mp3file->album). htmlentities($mp3file->comment)."</description>\n"; $output_string .= " <enclosure url=\"".$cfg['tag_link']."/". str_replace(" ", "%20", urlencode($filename)) ."\" length=\""; $output_string .= filesize($filename); $output_string .= "\" type=\"audio/mpeg\"/>\n"; // Training slash for XML $output_string .=" <category>".$cfg['tag_category']."</category>\n"; $output_string .= " <pubDate>".date("r",$filedate)."</pubDate>\n"; $output_string .= " </item>\n\n"; $cfg['max_feed']--; }
/* Compile the footer string*/ $output_string .= " </channel>\n</rss>\n";
/* START: Writing string to desired output */ switch ($cfg['operation']){ case 'stdout': header('Content-type: text/xml; charset=UTF-8', true); echo $output_string; break; case 'file': $output_file = $cfg['+ 'file_location'].$cfg['file_name']; $fh = fopen($output_file, 'w') or die("Error: Cant open file for writing. Check you have the right file, file path and directory is writable.");; ftruncate($fh, 0); fwrite($fh, $output_string); fclose($fh); break; default: echo "Error: You havent selected a valid mode of operation in the cfg['operation'] array. Possible values are 'file'|'stdout'"; } /* END: Writing to desired output *//* END: Script */?>
Open in Google Docs ViewerOpen link in new tabOpen link in new windowOpen link in new incognito windowDownload fileCopy link addressEdit PDF File on PDFescape.com