3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Conversion Class include 'config.class.php'; class YouTubeToMp3Converter extends Config { // Private Fields private $_songFileName = ''; private $_flvUrls = array(); private $_tempVidFileName; private $_uniqueID = ''; private $_vidSrcTypes = array('source_code', 'url'); private $_percentVidDownloaded = 0; #region Public Methods function __construct() { $this->_uniqueID = time() . "_" . uniqid('', true); } function DownloadVideo($youTubeUrl) { $file_contents = file_get_contents($youTubeUrl); if ($file_contents !== false) { $this->SetSongFileName($file_contents); $this->SetFlvUrls($file_contents); if ($this->GetSongFileName() != '' && count($this->GetFlvUrls()) > 0) { return $this->SaveVideo($this->GetFlvUrls()); } } return false; } function GenerateMP3($audioQuality) { $qualities = $this->GetAudioQualities(); $quality = (in_array($audioQuality, $qualities)) ? $audioQuality : $qualities[1]; $exec_string = parent::_FFMPEG.' -i '.$this->GetTempVidFileName().' -vol '.parent::_VOLUME.' -y -acodec libmp3lame -ab '.$quality.'k '.$this->GetSongFileName() . ' 2> logs/' . $this->_uniqueID . '.txt'; $ffmpegExecUrl = preg_replace('/(([^\/]+?)(\.php))$/', "exec_ffmpeg.php", "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); $postData = "cmd=".urlencode($exec_string); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ffmpegExecUrl); curl_setopt($ch, CURLOPT_POST, TRUE); curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_TIMEOUT, 1); curl_exec($ch); curl_close($ch); } function DownloadMP3($file) { $filepath = parent::_SONGFILEDIR . urldecode($file); $filename = urldecode($file); if (parent::_ENABLE_CONCURRENCY_CONTROL) { $filename = preg_replace('/((_uuid-)(\w{13})(\.mp3))$/', "$4", $filename); } if (is_file($filepath)) { header('Content-Type: audio/mpeg3'); header('Content-Length: ' . filesize($filepath)); header('Content-Disposition: attachment; filename="'.$filename.'"'); 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) { if (preg_match('/(<title>)(.+?)( - YouTube)(<\/title>)/', $vidSrc, $matches) == 1) { $name = trim($matches[2]); $name = iconv("UTF-8", "ISO-8859-1//TRANSLIT", $name); $name = (!empty($name)) ? html_entity_decode($name) : 'unknown_'.time(); } } } 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>'; ob_end_flush(); ob_flush(); flush(); } } #endregion #region Private "Helper" Methods private function SaveVideo(array $urls) { $success = false; $vidCount = -1; while (!$success && ++$vidCount < count($urls)) { $this->_percentVidDownloaded = 0; $this->SetTempVidFileName(); $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, $urls[$vidCount]); 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); if (is_file($this->GetTempVidFileName())) { if (!filesize($this->GetTempVidFileName()) || filesize($this->GetTempVidFileName()) < 10000) { unlink($this->GetTempVidFileName()); } else { $success = true; } } } return $success; } #endregion #region Properties public function GetSongFileName() { return $this->_songFileName; } private function SetSongFileName($file_contents) { $vidSrcTypes = $this->GetVidSrcTypes(); $trackName = $this->ExtractSongTrackName($file_contents, $vidSrcTypes[0]); if (!empty($trackName)) { $fname = parent::_SONGFILEDIR . preg_replace('/_{2,}/','_',preg_replace('/ /','_',preg_replace('/[^A-Za-z0-9 _-]/','',$trackName))); $fname .= (parent::_ENABLE_CONCURRENCY_CONTROL) ? uniqid('_uuid-') : ''; $this->_songFileName = $fname . '.mp3'; } } public function GetFlvUrls() { return $this->_flvUrls; } private function SetFlvUrls($file_contents) { $vidUrls = array(); $vidSrcTypes = $this->GetVidSrcTypes(); if (preg_match('/(ytplayer\.config = )([^\r\n]+?)(;<\/script>)/', $file_contents, $matches) == 1) { $jsonObj = json_decode(trim($matches[2], ';')); if (isset($jsonObj->args->url_encoded_fmt_stream_map)) { $urls = urldecode(urldecode($jsonObj->args->url_encoded_fmt_stream_map)); //die($urls); if (preg_match('/^((.+?)(=))/', $urls, $matches) == 1) { $urlsArr = preg_split('/,'.preg_quote($matches[0], '/').'/', $urls, -1, PREG_SPLIT_NO_EMPTY); foreach ($urlsArr as $url) { if ($matches[0] != 'url=') { $url = ($url != $urlsArr[0]) ? $matches[0].$url : $url; $urlBase = preg_replace('/(.+?)(url=)(.+?)(\?)(.+)/', "$3$4", $url); $urlParams = preg_replace('/(.+?)(url=)(.+?)(\?)(.+)/', "$1$5", $url); $url = $urlBase . "&" . $urlParams; } else { $url = preg_replace('/^(url=)/', "", $url); } $url = preg_replace('/(.*)(itag=\d+&)(.*?)/', '$1$3', $url, 1); if (preg_match('/quality=small/',$url) != 1) { $url = preg_replace('/sig=/', "signature=", $url); $url = trim($url, ','); $url .= '&title=' . urlencode($this->ExtractSongTrackName($file_contents, $vidSrcTypes[0])); $url = preg_replace_callback('/(&type=)(.+?)(&)/', function($match){return $match[1].urlencode($match[2]).$match[3];}, $url); $vidUrls[] = $url; } } $vidUrls = array_reverse($vidUrls); //die(print_r($vidUrls)); } } } $this->_flvUrls = $vidUrls; } public function GetAudioQualities() { return $this->_audioQualities; } public function GetTempVidFileName() { return $this->_tempVidFileName; } private function SetTempVidFileName() { $this->_tempVidFileName = parent::_TEMPVIDDIR . $this->_uniqueID .'.flv'; } public function GetVidSrcTypes() { return $this->_vidSrcTypes; } public function GetUniqueID() { return $this->_uniqueID; } #endregion } ?>

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.0.120.0100.00313.45
8.0.110.0040.00813.60
8.0.100.0000.01213.47
8.0.90.0000.01313.50
8.0.80.0100.00213.43
8.0.70.0090.00313.60
8.0.60.0130.00013.55
8.0.50.0100.00313.48
8.0.30.0130.00013.44
8.0.20.0000.01313.55
8.0.10.0090.00413.51
8.0.00.0070.00013.50
7.4.250.0040.00413.46
7.4.240.0140.00013.31
7.4.230.0110.00413.43
7.4.220.0040.00813.39
7.4.210.0100.00213.30
7.4.200.0120.00013.48
7.4.190.0130.00013.46
7.4.180.0080.00513.53
7.4.160.0130.00013.44
7.4.150.0060.00613.41
7.4.140.0000.01213.30
7.4.130.0060.00613.41
7.4.120.0030.00513.35
7.4.110.0050.00513.18
7.4.100.0130.00013.36
7.4.90.0120.00013.26
7.4.80.0100.00313.34
7.4.70.0130.00614.66
7.4.60.0140.01014.60
7.4.50.0120.00614.54
7.4.40.0120.00514.40
7.4.30.0090.00814.72
7.4.20.0160.00414.69
7.4.10.0100.00914.75
7.4.00.0080.01515.03
7.3.310.0040.00713.28
7.3.300.0050.00313.18
7.3.290.0050.00513.26
7.3.280.0000.01013.34
7.3.270.0120.00013.25
7.3.260.0030.00813.20
7.3.250.0100.00013.45
7.3.240.0080.00013.48
7.3.230.0050.00513.28
7.3.220.0090.00013.22
7.3.210.0080.00013.38
7.3.200.0030.00713.28
7.3.190.0080.00614.75
7.3.180.0100.00514.80
7.3.170.0110.00414.73
7.3.160.0150.01314.77
7.3.150.0090.00614.80
7.3.140.0080.00814.87
7.3.130.0070.01015.04
7.3.120.0060.01114.76
7.3.110.0100.00514.66
7.3.100.0100.00514.82
7.3.90.0070.00714.82
7.3.80.0080.00514.75
7.3.70.0070.00714.79
7.3.60.0110.00314.74
7.3.50.0090.00414.78
7.3.40.0060.00714.82
7.3.30.0050.00914.96
7.3.20.0120.00616.64
7.3.10.0040.01016.45
7.3.00.0100.00616.58
7.2.340.0030.00313.61
7.2.330.0060.00613.57
7.2.320.0130.00013.48
7.2.310.0070.01114.73
7.2.300.0090.01114.83
7.2.290.0160.01015.06
7.2.280.0120.00614.79
7.2.270.0160.00615.00
7.2.260.0130.01615.05
7.2.250.0070.00915.05
7.2.240.0040.01414.90
7.2.230.0060.01114.87
7.2.220.0090.00815.03
7.2.210.0100.00914.96
7.2.200.0120.00614.99
7.2.190.0090.00914.84
7.2.180.0180.00714.90
7.2.170.0070.01214.91
7.2.160.0130.00614.86
7.2.150.0120.00516.82
7.2.140.0120.00616.60
7.2.130.0160.00516.78
7.2.120.0150.00616.63
7.2.110.0150.00616.71
7.2.100.0140.00816.62
7.2.90.0190.00216.64
7.2.80.0120.00816.79
7.2.70.0100.01016.78
7.2.60.0170.00316.63
7.2.50.0100.00916.60
7.2.40.0150.00316.72
7.2.30.0070.01216.64
7.2.20.0140.00516.50
7.2.10.0120.00816.76
7.2.00.0060.00917.61
7.1.330.0170.00615.88
7.1.320.0170.00515.81
7.1.310.0170.00615.68
7.1.300.0180.00415.74
7.1.290.0130.00815.70
7.1.280.0100.01015.80
7.1.270.0080.00815.72
7.1.260.0210.00415.60
7.1.250.0160.00715.64
7.1.240.0130.01215.71
7.1.230.0190.00315.80
7.1.220.0180.00615.65
7.1.210.0200.00315.77
7.1.200.0170.00415.78
7.1.190.0140.00815.79
7.1.180.0110.00715.72
7.1.170.0110.00515.74
7.1.160.0110.00515.85
7.1.150.0060.00915.76
7.1.140.0130.00315.77
7.1.130.0100.00915.68
7.1.120.0070.01015.68
7.1.110.0100.01315.78
7.1.100.0090.00915.66
7.1.90.0260.01315.54
7.1.80.0150.00715.85
7.1.70.0080.00816.12
7.1.60.0100.00816.87
7.1.50.0090.00816.04
7.1.40.0040.01215.90
7.1.30.0080.00815.76
7.1.20.0060.00915.66
7.1.10.0130.00215.67
7.1.00.0100.02617.90
7.0.330.0170.00615.57
7.0.320.0250.01215.45
7.0.310.0150.00715.49
7.0.300.0090.00815.47
7.0.290.0100.00515.58
7.0.280.0150.00815.39
7.0.270.0160.00815.53
7.0.260.0160.00615.55
7.0.250.0150.00715.48
7.0.240.0100.00815.57
7.0.230.0140.00915.61
7.0.220.0160.00515.54
7.0.210.0090.00715.47
7.0.200.0080.00515.84
7.0.190.0090.00615.53
7.0.180.0110.00315.52
7.0.170.0100.00715.46
7.0.160.0110.00315.48
7.0.150.0120.01015.56
7.0.140.0110.02517.76
7.0.130.0080.00815.48
7.0.120.0090.00715.56
7.0.110.0110.00515.48
7.0.100.0120.03417.09
7.0.90.0140.01916.99
7.0.80.0140.03117.04
7.0.70.0170.03616.97
7.0.60.0190.02216.93
7.0.50.0150.03117.16
7.0.40.0090.02416.95
7.0.30.0140.03117.06
7.0.20.0150.01816.88
7.0.10.0100.03316.90
7.0.00.0130.02916.94
5.6.400.0120.00814.36
5.6.390.0090.01614.38
5.6.380.0120.01114.28
5.6.370.0150.00714.15
5.6.360.0060.01114.37
5.6.350.0080.00814.48
5.6.340.0130.00714.23
5.6.330.0060.01614.14
5.6.320.0170.00814.37
5.6.310.0130.00414.27
5.6.300.0130.00314.22
5.6.290.0080.00814.30
5.6.280.0110.02616.49
5.6.270.0140.00314.38
5.6.260.0120.00714.27
5.6.250.0090.02316.37
5.6.240.0100.02016.31
5.6.230.0090.01916.44
5.6.220.0100.03416.34
5.6.210.0150.03016.52
5.6.200.0060.02916.37
5.6.190.0090.02716.60
5.6.180.0150.02816.50
5.6.170.0180.02816.54
5.6.160.0110.03516.64
5.6.150.0130.03116.51
5.6.140.0130.01816.66
5.6.130.0110.02016.45
5.6.120.0140.02216.58
5.6.110.0060.01816.49
5.6.100.0080.01916.49
5.6.90.0090.02016.47
5.6.80.0050.01916.31
5.6.70.0120.01416.38
5.6.60.0120.01416.28
5.6.50.0080.02116.35
5.6.40.0090.02116.25
5.6.30.0110.02016.29
5.6.20.0080.02016.29
5.6.10.0110.01816.24
5.6.00.0110.01616.28
5.5.380.0180.02116.32
5.5.370.0120.03216.32
5.5.360.0130.03416.24
5.5.350.0130.03016.17
5.5.340.0120.02816.41
5.5.330.0100.02816.41
5.5.320.0160.01816.38
5.5.310.0120.03516.51
5.5.300.0120.02716.39
5.5.290.0190.02916.34
5.5.280.0100.01716.29
5.5.270.0020.02616.34
5.5.260.0110.01816.34
5.5.250.0130.01916.32
5.5.240.0080.01616.19
5.5.230.0080.01516.20
5.5.220.0090.01616.12
5.5.210.0130.01716.27
5.5.200.0110.01815.95
5.5.190.0120.02016.23
5.5.180.0080.02116.07
5.5.170.0120.01114.04
5.5.160.0080.02216.17
5.5.150.0130.01716.10
5.5.140.0080.01916.11
5.5.130.0120.01416.02
5.5.120.0120.01616.10
5.5.110.0130.01616.05
5.5.100.0100.01916.12
5.5.90.0070.02916.22
5.5.80.0130.01716.07
5.5.70.0100.02016.20
5.5.60.0100.01816.20
5.5.50.0110.01816.02
5.5.40.0120.01615.94
5.5.30.0120.01716.09
5.5.20.0120.01416.12
5.5.10.0070.02316.03
5.5.00.0160.01316.07
5.4.450.0120.01914.86
5.4.440.0060.01814.79
5.4.430.0130.01114.69
5.4.420.0110.01414.87
5.4.410.0100.01314.82
5.4.400.0090.01414.62
5.4.390.0060.01714.67
5.4.380.0120.01314.67
5.4.370.0120.01514.73
5.4.360.0090.01714.62
5.4.350.0120.01314.65
5.4.340.0120.01714.68
5.4.330.0150.00212.44
5.4.320.0100.01314.64
5.4.310.0090.01814.62
5.4.300.0070.01814.64
5.4.290.0090.01714.77
5.4.280.0070.01914.65
5.4.270.0100.01414.81
5.4.260.0130.01014.60
5.4.250.0100.01714.59
5.4.240.0110.01714.72
5.4.230.0090.01614.71
5.4.220.0070.01814.66
5.4.210.0090.01514.59
5.4.200.0100.01614.71
5.4.190.0080.01814.62
5.4.180.0090.01514.60
5.4.170.0090.01614.67
5.4.160.0110.01614.59
5.4.150.0110.03114.63
5.4.140.0090.02413.86
5.4.130.0120.02513.84
5.4.120.0140.02313.88
5.4.110.0140.02413.81
5.4.100.0070.02513.88
5.4.90.0060.02313.84
5.4.80.0130.01713.88
5.4.70.0090.02813.84
5.4.60.0090.02813.86
5.4.50.0070.01713.80
5.4.40.0110.02713.76
5.4.30.0170.02413.80
5.4.20.0110.02613.90
5.4.10.0080.02813.85
5.4.00.0090.01813.66
5.3.290.0070.01513.65
5.3.280.0030.01913.43
5.3.270.0080.03613.60
5.3.260.0080.01613.53
5.3.250.0110.01413.64
5.3.240.0090.02113.50
5.3.230.0120.02913.49
5.3.220.0160.02613.49
5.3.210.0070.02513.55
5.3.200.0100.03113.47
5.3.190.0090.02213.56
5.3.180.0100.02813.61
5.3.170.0080.03013.57
5.3.160.0060.02413.72
5.3.150.0090.02613.61
5.3.140.0110.03013.53
5.3.130.0110.03013.60
5.3.120.0090.02313.58
5.3.110.0100.02813.52
5.3.100.0060.03113.30
5.3.90.0080.03113.40
5.3.80.0090.02813.31
5.3.70.0080.03013.32
5.3.60.0080.02813.32
5.3.50.0090.02413.24
5.3.40.0110.02713.29
5.3.30.0110.02813.26
5.3.20.0070.01913.09
5.3.10.0110.02513.06
5.3.00.0110.02413.06

preferences:
38.05 ms | 401 KiB | 5 Q