Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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');
- private $_percentVidDownloaded = 0;
- // Constants
- const _TEMPVIDDIR = 'videos/';
- const _SONGFILEDIR = 'mp3/';
- const _FFMPEG = '/usr/bin/ffmpeg';
- #region Public Methods
- function __construct()
- {
- }
- function DownloadVideo($youTubeUrl)
- {
- $file_contents = file_get_contents($youTubeUrl);
- if ($file_contents !== false)
- {
- $this->SetSongFileName($file_contents);
- $this->SetFlvUrl($file_contents);
- if ($this->GetSongFileName() != '' && $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().' -y -acodec libmp3lame -ab '.$quality.'k '.$this->GetSongFileName();
- exec($exec_string);
- $this->DeleteTempVid();
- return is_file($this->GetSongFileName());
- }
- function DownloadMP3($file)
- {
- $filepath = self::_SONGFILEDIR . urldecode($file);
- if (is_file($filepath))
- {
- header('Content-Type: audio/mpeg3');
- header('Content-Length: ' . filesize($filepath));
- header('Content-Disposition: attachment; filename="'.urldecode($file).'"');
- ob_clean();
- flush();
- readfile($filepath);
- die();
- }
- else
- {
- $redirect = explode("?", $_SERVER['REQUEST_URI']);
- header('Location: ' . $redirect[0]);
- }
- }
- function ExtractSongTrackName($vidSrc, $srcType)
- {
- $name = '';
- $vidSrcTypes = $this->GetVidSrcTypes();
- if (in_array($srcType, $vidSrcTypes))
- {
- $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;
- }
- function ExtractVideoId($youTubeUrl)
- {
- $v = '';
- $urlQueryStr = parse_url(trim($youTubeUrl), PHP_URL_QUERY);
- if ($urlQueryStr !== false && !empty($urlQueryStr))
- {
- parse_str($urlQueryStr);
- }
- return $v;
- }
- function UpdateVideoDownloadProgress($downloadSize, $downloaded, $uploadSize, $uploaded)
- {
- $percent = round($downloaded/$downloadSize, 2) * 100;
- if ($percent > $this->_percentVidDownloaded)
- {
- $this->_percentVidDownloaded++;
- echo '<script type="text/javascript">updateVideoDownloadProgress("'. $percent .'");</script>';
- flush();
- }
- }
- #endregion
- #region Private "Helper" Methods
- private function SaveVideo($url)
- {
- $this->_percentVidDownloaded = 0;
- $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_NOPROGRESS, false);
- curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'UpdateVideoDownloadProgress'));
- curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096000);
- 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('"url_encoded_fmt_stream_map": "url=',$file_contents))
- {
- $vidUrl = end(explode('"url_encoded_fmt_stream_map": "url=',$file_contents));
- $vidUrl = urldecode(urldecode(current(explode('\u0026',$vidUrl))));
- }
- //die($vidUrl);
- $this->_flvUrl = $vidUrl;
- }
- 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
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement