-1

我想要做的是youtube-dl -x --audio-format mp3 "token"使用 php 执行并获取以下参数的 json:

  • 状态(错误 = 0/成功 = 1)
  • 保存的文件 url(您将在控制台结果示例中看到它[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3:)

这是我编写的 PHP 代码。

<?php
if ($_GET["token"]) {
            $url =  $_GET["token"];
            $template = '/home/website/public_html/%(id)s.%(ext)s';
            $string = ('youtube-dl -x --audio-format mp3 ' . escapeshellarg($url) . ' ' . 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));
        }
?>

基本上它执行

youtube-dl -x --audio-format mp3 "token"

当我在控制台(通过 SSH)中执行相同的命令时,我得到了这样的结果

[youtube] Setting language
[youtube] Confirming age
[youtube] nxtIRArhVD4: Downloading webpage
[youtube] nxtIRArhVD4: Downloading video info webpage
[youtube] nxtIRArhVD4: Extracting video information
[youtube] nxtIRArhVD4: Encrypted signatures detected.
[youtube] nxtIRArhVD4: Downloading js player vflE7vgXe
[download] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a
[download] 100% of 3.93MiB in 00:00
[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3
Deleting original file Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a (pass -k to keep)

当我在 php 环境中执行相同的命令时得到以下 json 结果

    {
   "status":1,
   "errors":"WARNING: The url doesn't specify the protocol, trying with http\nWARNING: Could not send HEAD request to http:\/\/\/home\/website\/public_html\/%(id)s.%(ext)s: \nWARNING: Falling back on generic information extractor.\nERROR: Unable to download webpage: \n",
   "url_orginal":"nxtIRArhVD4",
   "output":"[youtube] Setting language\n[youtube] Confirming age\n[youtube] nxtIRArhVD4: Downloading webpage\n[youtube] nxtIRArhVD4: Downloading video info webpage\n[youtube] nxtIRArhVD4: Extracting video information\n[youtube] nxtIRArhVD4: Encrypted signatures detected.\n[youtube] nxtIRArhVD4: Downloading js player vflE7vgXe\n[download] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a\n\r[download] 0.0% of 3.93MiB at 729.44KiB\/s ETA 00:05\r[download] 0.1% of 3.93MiB at 1.86MiB\/s ETA 00:02\r[download] 0.2% of 3.93MiB at 3.86MiB\/s ETA 00:01\r[download] 0.4% of 3.93MiB at 7.69MiB\/s ETA 00:00\r[download] 0.8% of 3.93MiB at 13.79MiB\/s ETA 00:00\r[download] 1.6% of 3.93MiB at 17.80MiB\/s ETA 00:00\r[download] 3.2% of 3.93MiB at 17.64MiB\/s ETA 00:00\r[download] 6.3% of 3.93MiB at 17.79MiB\/s ETA 00:00\r[download] 12.7% of 3.93MiB at 20.88MiB\/s ETA 00:00\r[download] 25.4% of 3.93MiB at 25.21MiB\/s ETA 00:00\r[download] 50.8% of 3.93MiB at 28.47MiB\/s ETA 00:00\r[download] 100.0% of 3.93MiB at 41.23MiB\/s ETA 00:00\r[download] 100% of 3.93MiB in 00:00\n[avconv] Destination: Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.mp3\nDeleting original file Jennifer Lopez - Booty ft. Iggy Azalea-nxtIRArhVD4.m4a (pass -k to keep)\n[generic] %(id)s: Requesting header\n[generic] %(id)s: Downloading webpage\n",
   "command":"youtube-dl --extract-audio --audio-format mp3 'nxtIRArhVD4' '\/home\/website\/public_html\/%(id)s.%(ext)s'"
}

事实上 php 完成工作并保存文件,但输出不一样,我无法获取目标文件 url。

我究竟做错了什么?有什么建议么?

4

1 回答 1

0

看起来您的命令$string没有正确生成。您的$url参数可能为空。

如果您查看错误,它正在尝试加载网页http:///home/website/public_html/%(id)s.%(ext)s

我猜$url必须是空的,这导致第 4 个参数(您的输出文件)成为第 3 个参数(视频 URL),因此它试图将其作为 URL 加载,因此出现错误。

启动后尝试查看内容$url,查看值是什么,确保它实际上是一个 URL。如果正确,则检查内容$string以确保命令确实完整,甚至尝试手动运行该命令并查看会发生什么,escapeshellarg()可能会导致比它要解决的问题更多的问题;我建议您对输入进行一些不同的验证,例如preg_match()对 YouTube URL 进行验证。

于 2014-09-21T19:59:48.480 回答