Skip to content

Instantly share code, notes, and snippets.

@sistematico
Created September 10, 2020 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sistematico/a046cbfd704db655f7b6cb963dae8f46 to your computer and use it in GitHub Desktop.
Save sistematico/a046cbfd704db655f7b6cb963dae8f46 to your computer and use it in GitHub Desktop.
youtube-dl with php
<?php
// Original: https://stackoverflow.com/a/15156424/1844007
if (isset($_POST['url']) && filter_var($_POST['url'], FILTER_VALIDATE_URL) ) {
$url = filter_var($_POST['url'], FILTER_SANITIZE_URL);
$template = '/var/www/html/uploads/%(title)s.%(ext)s';
$string = ('youtube-dl ' . escapeshellarg($url) . ' --audio-format mp3 -o ' . escapeshellarg($template));
$descriptorspec = [
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
];
$process = proc_open($string, $descriptorspec, $pipes);
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[2]);
$ret = proc_close($process);
echo json_encode(['status'=>$ret,'errors'=>$stderr,'url'=>$url,'output' => $stdout,'command' => $string]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment