7

我已经在我的 CentOS 6 / Plesk 10 专用服务器上安装了 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));
?>

当我执行 $string2 时,我得到状态:“0”和“url”:[youtube-dl 帮助文本行]

但是当我执行 $string 时,什么也没有发生,我得到“status”:“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));
?>

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

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"

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

4

1 回答 1

10

exec只读取标准输出,所以你错过了标准错误上的错误信息,不管它是什么。

使用以下代码也可以获取 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));

很可能,您没有写入该目录的权限。要检验该理论,请检查是否

touch /var/www/vhosts/my.virtual.host.net/httpdocs/downloader/downloads/test

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

于 2013-03-01T11:17:51.967 回答