ballhogjoni Posted August 9, 2011 Share Posted August 9, 2011 Hey all I was using this class to download and convert my youtube videos. <?php // Conversion Class class YouTubeToMp3Converter { // Private Fields private $_songFileName = ''; private $_flvUrl = ''; private $_audioQualities = array(64, 128, 320); private $_tempVidFileName; private $_vidSrcTypes = array('source_code', 'url'); // Constants const _TEMPVIDDIR = 'videos/'; const _SONGFILEDIR = 'mp3/'; //const _FFMPEG = 'nice -n 20 /usr/bin/ffmpeg'; const _FFMPEG = '/usr/bin/ffmpeg'; #region Public Methods function __construct() { } function CheckDownloadCount(){ require("../get_video_ads/Database.class.php"); $db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE); $db->connect(); $ip = addslashes($_SERVER['REMOTE_ADDR']); $sql = sprintf("SELECT count(*) c FROM downloaded WHERE ipadress = '%s' and last_access between '".date('Y-m-d')." 00:00:00' and '".date('Y-m-d')." 23:59:59' ORDER BY last_access DESC", $ip); $count = $db->query_first($sql); if ($count[c] > 7) { $db->close(); return false; }else{ $db->query_insert('downloaded', array("ipadress" => $ip)); $db->close(); } return true; } function DownloadVideo($youTubeUrl) { $file_contents = file_get_contents($youTubeUrl); if ($file_contents !== false && !empty($file_contents)) { $this->SetSongFileName($file_contents); if( file_exists( $this->_songFileName ) ) { return true; }else{ $this->SetFlvUrl($file_contents); if ($this->GetSongFileName() != '' && $this->GetFlvUrl() != '') { echo $this->GetFlvUrl(); return $this->SaveVideo($this->GetFlvUrl()); } } } return false; } function GenerateMP3($audioQuality) { $qualities = $this->GetAudioQualities(); $quality = (in_array($audioQuality, $qualities)) ? $audioQuality : $qualities[1]; $exec_string = self::_FFMPEG.' -i '.$this->GetTempVidFileName().' -ab '.$quality.'k '.$this->GetSongFileName(); exec($exec_string); $this->DeleteTempVid(); return is_file($this->GetSongFileName()); } function ExtractSongTrackName($vidSrc, $srcType) { $name = ''; $vidSrcTypes = $this->GetVidSrcTypes(); if (in_array($srcType, $vidSrcTypes) && !empty($vidSrc)) { $vidSrc = ($srcType == $vidSrcTypes[1]) ? file_get_contents($vidSrc) : $vidSrc; if ($vidSrc !== false && eregi('eow-title',$vidSrc)) { $name = end(explode('eow-title',$vidSrc)); $name = current(explode('">',$name)); $name = ereg_replace('[^-_a-zA-Z,"\' :0-9]',"",end(explode('title="',$name))); } } return $name; } #endregion #region Private "Helper" Methods private function SaveVideo($url) { $this->SetTempVidFileName(time()); $file = fopen($this->GetTempVidFileName(), 'w'); $ch = curl_init(); curl_setopt($ch, CURLOPT_FILE, $file); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE); curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE); curl_exec($ch); curl_close($ch); fclose($file); return is_file($this->GetTempVidFileName()); } private function DeleteTempVid() { if (is_file($this->GetTempVidFileName())) { unlink($this->GetTempVidFileName()); } } #endregion #region Properties public function GetSongFileName() { return $this->_songFileName; } private function SetSongFileName($file_contents) { $vidSrcTypes = $this->GetVidSrcTypes(); $trackName = $this->ExtractSongTrackName($file_contents, $vidSrcTypes[0]); $this->_songFileName = (!empty($trackName)) ? self::_SONGFILEDIR . preg_replace('/_{2,}/','_',preg_replace('/ /','_',preg_replace('/[^A-Za-z0-9 _-]/','',$trackName))) . '.mp3' : ''; } public function GetFlvUrl() { return $this->_flvUrl; } private function SetFlvUrl($file_contents) { $vidUrl = ''; if (eregi('fmt_url_map',$file_contents)) { echo "HERE"; $vidUrl = end(explode('fmt_url_map=',$file_contents)); $vidUrl = current(explode('&',$vidUrl)); $vidUrl = current(explode('%2C',$vidUrl)); $vidUrl = urldecode(end(explode('%7C',$vidUrl))); } $this->_flvUrl = $vidUrl."&hd=0"; } public function GetAudioQualities() { return $this->_audioQualities; } private function GetTempVidFileName() { return $this->_tempVidFileName; } private function SetTempVidFileName($timestamp) { $this->_tempVidFileName = self::_TEMPVIDDIR . $timestamp .'.flv'; } public function GetVidSrcTypes() { return $this->_vidSrcTypes; } #endregion } It seems as though youtube changed something, so now I can't download the videos anymore. It has something to do with the SetFlvUrl function. I think they changed the embed code or something that grabs the actual video url. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/244329-youtube-video-download-changes/ Share on other sites More sharing options...
JasonLewis Posted August 9, 2011 Share Posted August 9, 2011 I take it you did not write this yourself. I doubt someone is going to fix it for you if it's broken, perhaps finding the original author and seeing if they can fix it? But anyway, do you get any errors or anything? Or does it just not work at all? Quote Link to comment https://forums.phpfreaks.com/topic/244329-youtube-video-download-changes/#findComment-1254994 Share on other sites More sharing options...
ballhogjoni Posted August 10, 2011 Author Share Posted August 10, 2011 I did write some of it. It used to work as of Sunday (a few days ago). The problem is this line if (eregi('fmt_url_map',$file_contents)) it no longer finds the match. This leads me to believe that Youtube changed something on their site. I am not looking for a rewrite of the class, I am looking to see if anyone has knowledge of the change and how to fix this line/function in the class to make it work again. Quote Link to comment https://forums.phpfreaks.com/topic/244329-youtube-video-download-changes/#findComment-1255296 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.