加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

youtube-dl和php exec

发布时间:2020-12-13 17:44:22 所属栏目:PHP教程 来源:网络整理
导读:我在我的CentOS 6 / Plesk 10 Dedicated服务器上安装了youtube-dl,通过SSH,一切都像魅力一样! 问题是我正在尝试编写一个php脚本,它将POST参数包含在我要复制到服务器的视频的URL中,复制它然后用ffmpeg处理它. 我以为我会使用exec()函数. 如果我回显youtube-
我在我的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));
?>

当我执行$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,'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个第一行,我看不到任何指定路径中的视频文件…有什么想法吗?

解决方法

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",// 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,'output' => $stdout,'command' => $string));

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读