youtube-dl 和 php exec

问题描述 投票:0回答:2

我已经在我的 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 行,而且我看不到指定路径中的任何视频文件...有什么想法吗?

php exec youtube-dl
2个回答
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

有效。您可以使用

chmod
chown
更改权限。


0
投票

我能够在管理中使用我的 Wordpress 插件使用 out.log 执行 shell_exec 并显示其他 AJAX 函数的最后一行。

你可以去我的 Github 仓库看看它是什么样的,在这个文件的函数 my_url() 的第 516 行,my_dl() 将更新结果 DIV。

https://github.com/Jerl92/McPlayer/blob/master/admin/partials/McPlayer-admin-bulk-add-album.php

这是执行长 shell_exec 命令的一种方法,这将更新输出区域。

谢谢,

© www.soinside.com 2019 - 2024. All rights reserved.