如何让 shell exec 在 php 继续运行时在后台运行
我有 ffmpeg
在 PHP 文件中进行视频转换,它可以正常工作。问题是这需要一分钟才能完成。我认为这可能很容易做到,但当我只使用一个命令(例如,没有 mp4box 的单通道转换)时,我只能让它在后台工作,就像这样
exec("nohup " . $ffmpegPath . " -i " . $srcFile . " -f mp4 -vcodec libx264 -crf 27 -s " . $srcWidth . "x" . $srcHeight . " -an -r 20 " . $destFile . ".mp4 > /dev/null 2>&1 &");
问题是我需要使用三个不同的命令来进行正确的转换尽管。到目前为止,我的命令在 PHP 文件中看起来像这样,并且它可以工作,但是有很大的延迟:
exec($ffmpegPath . " -y -i " . $srcFile . " -f mp4 -pass 1 -passlogfile " . $video_pass_log . " -vcodec libx264 -vpre ipod640 -b:v 2M -bt 4M -an " . $destFile . ".mp4");
exec($ffmpegPath . " -y -i " . $srcFile . " -f mp4 -pass 2 -passlogfile " . $video_pass_log . " -vcodec libx264 -vpre ipod640 -b:v 2M -bt 4M -acodec libfaac -ac 2 -ar 44100 -ab 96k " . $destFile . ".mp4");
exec($mp4boxpath . " -tmp /tmp -hint " . $destFile . ".mp4");
How can I make the shell exec run in back while PHP continue?
I have ffmpeg
doing a video conversion in a PHP file and it works as it should. The problem is it takes up to a minute for this to finish. I thought it might be easy to do but I can only get it to work in the background when I use just one command (eg single pass conversion without mp4box) like this
exec("nohup " . $ffmpegPath . " -i " . $srcFile . " -f mp4 -vcodec libx264 -crf 27 -s " . $srcWidth . "x" . $srcHeight . " -an -r 20 " . $destFile . ".mp4 > /dev/null 2>&1 &");
The problem is I need to use three different commands to do a proper conversion though. So far my commands look like this in the PHP file and it works, but with a massive delay:
exec($ffmpegPath . " -y -i " . $srcFile . " -f mp4 -pass 1 -passlogfile " . $video_pass_log . " -vcodec libx264 -vpre ipod640 -b:v 2M -bt 4M -an " . $destFile . ".mp4");
exec($ffmpegPath . " -y -i " . $srcFile . " -f mp4 -pass 2 -passlogfile " . $video_pass_log . " -vcodec libx264 -vpre ipod640 -b:v 2M -bt 4M -acodec libfaac -ac 2 -ar 44100 -ab 96k " . $destFile . ".mp4");
exec($mp4boxpath . " -tmp /tmp -hint " . $destFile . ".mp4");
How can I make the shell exec run in background while PHP continues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 是有没有办法在不等待命令完成的情况下使用 shell_exec?
添加
> /dev/null 2>/dev/null &
将删除输出并在另一个进程中运行该命令(&
创建新进程,>< /code> 和
2>
重定向正常和错误输出)See Is there a way to use shell_exec without waiting for the command to complete?
Adding
> /dev/null 2>/dev/null &
will remove the output and runs the command in another process (the&
creates the new process, the>
and2>
redirects the normal and error output)