Advertisement
Guest User

youtube

a guest
Jan 18th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. <?php
  2.  
  3. // Conversion Class
  4. class YouTubeToMp3Converter
  5. {
  6. // Private Fields
  7. private $_songFileName = '';
  8. private $_flvUrl = '';
  9. private $_audioQualities = array(64, 128, 320);
  10. private $_tempVidFileName;
  11. private $_vidSrcTypes = array('source_code', 'url');
  12. private $_percentVidDownloaded = 0;
  13.  
  14. // Constants
  15. const _TEMPVIDDIR = 'videos/';
  16. const _SONGFILEDIR = 'mp3/';
  17. const _FFMPEG = '/usr/bin/ffmpeg';
  18.  
  19. #region Public Methods
  20. function __construct()
  21. {
  22. }
  23.  
  24. function DownloadVideo($youTubeUrl)
  25. {
  26. $file_contents = file_get_contents($youTubeUrl);
  27. if ($file_contents !== false)
  28. {
  29. $this->SetSongFileName($file_contents);
  30. $this->SetFlvUrl($file_contents);
  31. if ($this->GetSongFileName() != '' && $this->GetFlvUrl() != '')
  32. {
  33. return $this->SaveVideo($this->GetFlvUrl());
  34. }
  35. }
  36. return false;
  37. }
  38.  
  39. function GenerateMP3($audioQuality)
  40. {
  41. $qualities = $this->GetAudioQualities();
  42. $quality = (in_array($audioQuality, $qualities)) ? $audioQuality : $qualities[1];
  43. $exec_string = self::FFMPEG.' -i '.$this->GetTempVidFileName().' -y -acodec libmp3lame -ab '.$quality.'k '.$this->GetSongFileName();
  44. exec($exec_string);
  45. $this->DeleteTempVid();
  46. return is_file($this->GetSongFileName());
  47. }
  48.  
  49. function DownloadMP3($file)
  50. {
  51. $filepath = self::_SONGFILEDIR . urldecode($file);
  52. if (is_file($filepath))
  53. {
  54. header('Content-Type: audio/mpeg3');
  55. header('Content-Length: ' . filesize($filepath));
  56. header('Content-Disposition: attachment; filename="'.urldecode($file).'"');
  57. ob_clean();
  58. flush();
  59. readfile($filepath);
  60. die();
  61. }
  62. else
  63. {
  64. $redirect = explode("?", $_SERVER['REQUEST_URI']);
  65. header('Location: ' . $redirect[0]);
  66. }
  67. }
  68.  
  69. function ExtractSongTrackName($vidSrc, $srcType)
  70. {
  71. $name = '';
  72. $vidSrcTypes = $this->GetVidSrcTypes();
  73. if (in_array($srcType, $vidSrcTypes))
  74. {
  75. $vidSrc = ($srcType == $vidSrcTypes[1]) ? file_get_contents($vidSrc) : $vidSrc;
  76. if ($vidSrc !== false && eregi('eow-title',$vidSrc))
  77. {
  78. $name = end(explode('eow-title',$vidSrc));
  79. $name = current(explode('">',$name));
  80. $name = ereg_replace('[^-_a-zA-Z,"\' :0-9]',"",end(explode('title="',$name)));
  81. }
  82. }
  83. return $name;
  84. }
  85.  
  86. function ExtractVideoId($youTubeUrl)
  87. {
  88. $v = '';
  89. $urlQueryStr = parse_url(trim($youTubeUrl), PHP_URL_QUERY);
  90. if ($urlQueryStr !== false && !empty($urlQueryStr))
  91. {
  92. parse_str($urlQueryStr);
  93. }
  94. return $v;
  95. }
  96.  
  97. function UpdateVideoDownloadProgress($downloadSize, $downloaded, $uploadSize, $uploaded)
  98. {
  99. $percent = round($downloaded/$downloadSize, 2) * 100;
  100. if ($percent > $this->_percentVidDownloaded)
  101. {
  102. $this->_percentVidDownloaded++;
  103. echo '<script type="text/javascript">updateVideoDownloadProgress("'. $percent .'");</script>';
  104. flush();
  105. }
  106. }
  107. #endregion
  108.  
  109. #region Private "Helper" Methods
  110. private function SaveVideo($url)
  111. {
  112. $this->_percentVidDownloaded = 0;
  113. $this->SetTempVidFileName(time());
  114. $file = fopen($this->GetTempVidFileName(), 'w');
  115. $ch = curl_init();
  116. curl_setopt($ch, CURLOPT_FILE, $file);
  117. curl_setopt($ch, CURLOPT_HEADER, 0);
  118. curl_setopt($ch, CURLOPT_URL, $url);
  119. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  120. curl_setopt($ch, CURLOPT_NOPROGRESS, false);
  121. curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this, 'UpdateVideoDownloadProgress'));
  122. curl_setopt($ch, CURLOPT_BUFFERSIZE, 4096000);
  123. curl_exec($ch);
  124. curl_close($ch);
  125. fclose($file);
  126. return is_file($this->GetTempVidFileName());
  127. }
  128.  
  129. private function DeleteTempVid()
  130. {
  131. if (is_file($this->GetTempVidFileName()))
  132. {
  133. unlink($this->GetTempVidFileName());
  134. }
  135. }
  136. #endregion
  137.  
  138. #region Properties
  139. public function GetSongFileName()
  140. {
  141. return $this->_songFileName;
  142. }
  143. private function SetSongFileName($file_contents)
  144. {
  145. $vidSrcTypes = $this->GetVidSrcTypes();
  146. $trackName = $this->ExtractSongTrackName($file_contents, $vidSrcTypes[0]);
  147. $this->_songFileName = (!empty($trackName)) ? self::_SONGFILEDIR . preg_replace('/_{2,}/','_',preg_replace('/ /','_',preg_replace('/[^A-Za-z0-9 _-]/','',$trackName))) . '.mp3' : '';
  148. }
  149.  
  150. public function GetFlvUrl()
  151. {
  152. return $this->_flvUrl;
  153. }
  154. private function SetFlvUrl($file_contents)
  155. {
  156. $vidUrl = '';
  157. if (eregi('"url_encoded_fmt_stream_map": "url=',$file_contents))
  158. {
  159. $vidUrl = end(explode('"url_encoded_fmt_stream_map": "url=',$file_contents));
  160. $vidUrl = urldecode(urldecode(current(explode('\u0026',$vidUrl))));
  161. }
  162. //die($vidUrl);
  163. $this->_flvUrl = $vidUrl;
  164. }
  165.  
  166. public function GetAudioQualities()
  167. {
  168. return $this->_audioQualities;
  169. }
  170.  
  171. private function GetTempVidFileName()
  172. {
  173. return $this->_tempVidFileName;
  174. }
  175. private function SetTempVidFileName($timestamp)
  176. {
  177. $this->_tempVidFileName = self::_TEMPVIDDIR . $timestamp .'.flv';
  178. }
  179.  
  180. public function GetVidSrcTypes()
  181. {
  182. return $this->_vidSrcTypes;
  183. }
  184. #endregion
  185. }
  186.  
  187. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement