异步运行PHP exec(),但检查完成情况?

时间:2015-09-01 15:42:14

标签: php video asynchronous ffmpeg avconv

我正在编写一个包含用户上传视频的网站后端。为了确保最大程度的可访问性,我正在压缩上传的视频,并将它们重新保存为.mp4和.webm格式,以涵盖所有浏览器(或者尽可能多的浏览器)。为此,我在PHP exec()函数中运行avconv命令。

我不想让用户在页面加载之前等待脚本完成,所以我正在异步运行代码。到目前为止,我的代码如下。

exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"');
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');

除了运行exec功能外,我还将视频保存到数据库并向用户发送电子邮件,感谢他们上传视频。

这就是问题:我希望服务器在视频转换完成之前等待,然后将其添加到数据库并向用户发送电子邮件。基本上,程序流程将是:

用户上传视频。 视频放在临时文件夹中。 用户被带到感谢页面,表明他们的视频很快就会播出。 服务器执行两个avconv命令来转换和压缩视频以供Web使用。 完成BOTH转换后,视频信息将添加到MySQL数据库,电子邮件将发送给用户,并删除原始上传的视频。

这可能只是我对命令行的无知(事实上它几乎肯定是),但我怎么能'排队'这些命令呢?首先进行两次转换,然后调用PHP脚本添加到数据库,然后删除原始视频,同时与原始PHP脚本异步?

编辑:我已经尝试用'&&'排队了运算符,如下所示:

exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 && avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');

但是,这似乎取消了我异步运行它的事实,因为页面现在似乎等待命令完成。

3 个答案:

答案 0 :(得分:5)

您应该启动一个异步命令行php脚本,对两个视频进行编码,然后发送电子邮件:

upload.php:

exec('/usr/bin/php -f encode_files.php > /dev/null 2>/dev/null &"');
echo "Files will be encoded, come back later !";

<强> encode_files.php

exec('avconv ...'); // Synchronously ! Without > /dev/null etc ...
exec('avconv ...'); // webm ...

mail('user@user.com', 'Encoding complete ! ', 'Great ! ');

我将调用留作“bash -c exec ...”,但我认为有更短的方法可以异步调用php脚本: Asynchronous shell exec in PHP 你甚至可以传递params(比如用户/视频ID,......)

$cmd = 'nohup /usr/bin/php -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & printf "%u" $!';
$pid = shell_exec($cmd);

答案 1 :(得分:1)

您可以断开PHP脚本与客户端的连接,但保持其运行以完成任务。

// Your preliminary stuff here ...
/// then send the user elsewhere but carry on in the background
ignore_user_abort(true);
set_time_limit(0); // i.e. forever

header("Location: thankyoubutwait.php", true);
header("Connection: close", true);
header("Content-Encoding: none\r\n");
header("Content-Length: 0", true);

flush();
ob_flush();

session_write_close();
// more of your video stuff here including database writes
// and clean up bits
// (you may end up with zombie processes though so check your logs or write statuses to files etc.)

答案 2 :(得分:1)

您只需检查命令行的良好执行情况就很容易:

// Your code before...
$command = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"'
exec($command, $return, $status);
if($status == 0 ) {
    $command2 = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"';
    exec($command2, $return2, $status2);

    if($status2==0){
        // let your user know your video traitement has been done
        // lauch a new function for alert him
    }
}


// Kill your process at end
die();
相关问题