Last active
April 17, 2017 17:41
-
-
Save EggieCode/38d5b49ee8423cd4295a8ab0da69d0e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="post" action=''> | |
<input name="url" type="text" placeholder="Youtube link" /> | |
<button action="submit">Submit</button> | |
</form> | |
<?php | |
if($_SERVER['REQUEST_METHOD'] == 'GET') { | |
exit; | |
} | |
echo '<pre>'; | |
define('DOWNLOAD_FOLDER', "../Downloads"); | |
define('MUSIC_FOLDER', "../Music"); | |
function check_yt_file_exists($id) { | |
$aFiles = glob(DOWNLOAD_FOLDER . '/*'.$id.'*'); | |
foreach($aFiles as $sFile) { | |
if(substr($sFile, 13, 11) == 'normalized-') { | |
print_r([$id, $sFile, substr($sFile, 13, 11)]); | |
echo 'Already found!'; | |
exit; | |
} | |
} | |
} | |
if(substr($_POST['url'], 0, 29) != 'https://www.youtube.com/watch') { | |
echo "Only Youtube watch allowed\n"; | |
exit; | |
} | |
print("Downloading: " . $_POST['url'] . "\n"); | |
$query = parse_url($_POST['url'], PHP_URL_QUERY); | |
parse_str($query, $params); | |
if(isset($params['v'])) | |
check_yt_file_exists($params['v']); | |
$youtube = "https://www.youtube.com/watch?" . http_build_query(['v' => $params['v']]); | |
$oJson = json_decode(trim(exec( | |
"youtube-dl -x -o " . escapeshellarg(DOWNLOAD_FILDER . "/%(id)s.%(ext)s") | |
." --print-json ".escapeshellarg($youtube) | |
))); | |
check_yt_file_exists($oJson->id); | |
$aFiles = glob(DOWNLOAD_FILDER . '/*'.$oJson->id.'*'); | |
$process = proc_open('ffmpeg-normalize -vf --format=ogg '. escapeshellarg($aFiles[0]), [ | |
0 => ["pipe", "r"], // stdin | |
1 => ["pipe", "w"], // stdout | |
2 => ["pipe", "w"], // stderr | |
], $pipes, DOWNLOAD_FILDER, null); | |
if (is_resource($process)) { | |
print stream_get_contents($pipes[1]); | |
print stream_get_contents($pipes[2]); | |
fclose($pipes[1]); | |
fclose($pipes[2]); | |
} else { | |
print('ffmpeg-normalize no resource'); | |
} | |
$aNewFiles = glob('../Downloads/normalized-'.$oJson->id . '*.ogg'); | |
echo "Copying file to `Music/$oJson->title.ogg`\n"; | |
copy(DOWNLOAD_FOLDER . '/' .$aNewFiles[0], MUSIC_FOLDER . '/' . $oJson->title . ".ogg"); | |
echo "Updating mpd database\n"; | |
exec('mpc update'); | |
echo "Add to queue\n"; | |
exec('mpc add '. escapeshellarg($oJson->title . ".ogg")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment