如何让 shell exec 在 php 继续运行时在后台运行

发布于 2024-12-28 02:57:04 字数 958 浏览 3 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没︽人懂的悲伤 2025-01-04 02:57:04

请参阅 是有没有办法在不等待命令完成的情况下使用 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 > and 2> redirects the normal and error output)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文