Created
September 10, 2020 03:46
-
-
Save sistematico/a046cbfd704db655f7b6cb963dae8f46 to your computer and use it in GitHub Desktop.
youtube-dl with php
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
<?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