youtube-dl和php exec

Cha*_*haa 5 php exec youtube-dl

我在我的CentOS 6/Plesk 10 Dedicated服务器上安装了youtube-dl,通过SSH,一切都像魅力一样!

问题是我正在尝试编写一个php脚本,它将POST参数包含在我想要复制到服务器的视频的URL中,复制它然后用ffmpeg处理它.

我以为我会使用exec()函数.

如果我回显结果,我可以获得输出youtube-dl --help,但每次我要求php执行一个实际上对视频执行某些操作的命令时,它返回状态"1",并且不输出任何内容.

关于我做错了什么的任何想法?

这是我的PHP代码:

<?php 
    $result = array();
    $status;
    $url = $_POST['src'];
    $string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
    $string2 = 'youtube-dl --help';
    exec($string, $result, $status);
    echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result));
?>
Run Code Online (Sandbox Code Playgroud)

当我执行$ string2时,我得到状态:"0"和"url":[youtube-dl help text lines]

但是当我执行$ string时,没有任何反应,我得到"状态":"1",没有别的,没有下载视频.我还尝试使用"-g"参数和变体进行模拟,但只要youtube-dl必须获取视频,它就会中断.

先感谢您 !

编辑

我编辑了我的代码所以它看起来像这样:

<?php 
    $result = array();
    $status;
    $url = $_POST['src'];
    $string = 'youtube-dl "'.$url.'" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s"';
    $string2 = 'youtube-dl --help';
    exec($string, $result, $status);
    echo json_encode(array('status' => $status, 'url_orginal'=>$url, 'url' => $result, 'command' => $string));
?>
Run Code Online (Sandbox Code Playgroud)

而我昨天没有得到的结果是:

command: "youtube-dl "http://www.youtube.com/watch?v=coq9klG41R8" -f 18 -o "/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/%(id)s.%(ext)s""
status: 1
url: 
    0: "[youtube] Setting language"
    1: "[youtube] coq9klG41R8: Downloading video info webpage"
    2: "[youtube] coq9klG41R8: Extracting video information"
url_orginal: "http://www.youtube.com/watch?v=coq9klG41R8"
Run Code Online (Sandbox Code Playgroud)

这很奇怪,考虑到a)昨天我得到了一个空的url [],而且b)即使现在我得到的看起来像普通的youtube-dl返回,它只包含3个第一行,我看不到任何指定路径中的视频文件...有什么想法吗?

phi*_*hag 7

exec 只读stdout,所以你错过了stderr上的错误信息,无论是什么.

使用以下代码来获取stderr:

$url = 'http://www.youtube.com/watch?v=coq9klG41R8';
$template = '/var/www/vhosts/my.virtual.host.net/httpdocs/downloader/' .
            'downloads/%(id)s.%(ext)s';
$string = ('youtube-dl ' . escapeshellarg($url) . ' -f 18 -o ' .
          escapeshellarg($template));

$descriptorspec = array(
   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(array('status' => $ret, 'errors' => $stderr,
                       'url_orginal'=>$url, 'output' => $stdout,
                       'command' => $string));
Run Code Online (Sandbox Code Playgroud)

很可能,您没有权限写入该目录.要测试该理论,请检查是否

touch /var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/test
Run Code Online (Sandbox Code Playgroud)

作品.您可以使用chmodchown更改权限.