Skip to content

Instantly share code, notes, and snippets.

@w0rm49
Last active May 19, 2016 09:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save w0rm49/843348a7c0ebba5df8a5 to your computer and use it in GitHub Desktop.
Save w0rm49/843348a7c0ebba5df8a5 to your computer and use it in GitHub Desktop.
simple web front for https://github.com/rg3/youtube-dl
<?php
$ytdlPath = '/whereever/your/youtube-dl'; //change this
ob_start();
$url = trim(rawurldecode($_SERVER['QUERY_STRING']));
$qStr = parse_url($url, PHP_URL_QUERY );
parse_str($qStr, $get);
if (!isset($get['v']) || !preg_match('~[a-zA-Z0-9_-]{11}~',$get['v']) ) {
die();
}
$titleCommand = escapeshellcmd('python2.7 -u ' . $ytdlPath . ' --youtube-skip-dash-manifest --skip-download --print-json http://www.youtube.com/watch?v=' . $get['v']);
$json = shell_exec($titleCommand);
$info = json_decode($json, true);
$title = $info['title'];
$downloadCommand = escapeshellcmd('python2.7 -u ' . $ytdlPath . ' --youtube-skip-dash-manifest -q -o - http://www.youtube.com/watch?v=' . $get['v']);
$descriptorspec = [
0 => ["pipe", "r"],
1 => ["pipe", "w"],
2 => ["pipe", "w"]
];
ob_end_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $title .'.mp4"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
$process = proc_open($downloadCommand, $descriptorspec, $pipes, realpath('./'), array());
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
proc_close($process);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment