[ 'name' => 'Audio Only (MP3)', 'group' => 'Audio Only', 'option' => '-k -x --audio-format mp3 --audio-quality 0', 'cache' => '*-%youtubeid%.mp3' ], "audio_flac" => [ 'name' => 'Audio Only (FLAC)', 'group' => 'Audio Only', 'option' => '-k -x --audio-format flac --audio-quality 0', 'cache' => '*-%youtubeid%.flac' ], "video_mp4" => [ 'name' => "Video (MP4)", 'group' => "Video", 'option' => '-k -f "bestvideo[ext=mp4]+bestaudio[ext=m4a]/mp4" --merge-output-format mp4', 'cache' => '*-%youtubeid%.mp4' ], "best_video_audio" => [ 'name' => 'Combine best Video+Audio', 'group' => NULL, 'option' => '-k', 'cache' => NULL ] ]; if(!chdir('files')) { die('could not enter files folder'); } if($_SERVER['REQUEST_METHOD'] == 'POST') { if(!isset($_POST['option'], $_POST['url'])) { http_response_code(400); die('not all required values set!'); } if(empty($option = trim($_POST['option'])) || empty($url = trim($_POST['url']))) { http_response_code(400); die('url empty!'); } if(!filter_var($url, FILTER_VALIDATE_URL)) { http_response_code(400); die('invalid url!'); } if(!isset($options[$option])) { http_response_code(400); die('invalid option!'); } $option = $options[$option]; preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $url, $matches); if(count($matches) != 1) { http_response_code(400); die('invalid youtube url!' . count($matches)); } $cached = false; if(!is_null($option['cache'])) { foreach(glob(str_replace("%youtubeid%", $matches[0], $option['cache'])) as $file) { echo '[cache] Destination: ' . $file . "\n"; $cached = true; } } if($cached) { die('exit code: 0'); } ini_set('output_buffering', 'off'); ini_set('zlib.output_compression', false); ini_set('implicit_flush', true); ob_implicit_flush(true); while (ob_get_level() > 0) { $level = ob_get_level(); ob_end_clean(); if (ob_get_level() == $level) break; } if (function_exists('apache_setenv')) { apache_setenv('no-gzip', '1'); apache_setenv('dont-vary', '1'); } set_time_limit(0); echo '[system] Executing' . 'youtube-dl ' . $option['option']. ' ' . $url . "\r\n"; system('youtube-dl ' . $option['option']. ' ' . $url . ' 2>&1', $exitCode); die('exit code: ' . $exitCode); } ?>